History

Lua 5.2.0-beta-rc1

Lua Team committed 15 years ago

Commit 30837cc1 parent 9bce00553 files · +2418 −1456Browse files

Unified · Split

53 files
  1. README +1 −1
  2. doc/contents.html +12 −8
  3. doc/lua.css +20 −1
  4. doc/manual.html +601 −337
  5. doc/readme.html +16 −9
  6. src/Makefile +2 −2
  7. src/lapi.c +88 −74
  8. src/lauxlib.c +77 −13
  9. src/lauxlib.h +5 −2
  10. src/lbaselib.c +76 −108
  11. src/lbitlib.c +67 −21
  12. src/lcode.c +42 −38
  13. src/lcode.h +3 −4
  14. src/ldblib.c +8 −19
  15. src/ldebug.c +74 −36
  16. src/ldebug.h +4 −1
  17. src/ldo.c +64 −66
  18. src/ldump.c +3 −3
  19. src/lgc.c +79 −58
  20. src/lgc.h +8 −9
  21. src/linit.c +5 −5
  22. src/liolib.c +34 −57
  23. src/llex.c +13 −17
  24. src/llex.h +7 −7
  25. src/llimits.h +15 −10
  26. src/lmem.c +5 −9
  27. src/loadlib.c +43 −30
  28. src/lobject.c +86 −38
  29. src/lobject.h +241 −87
  30. src/lopcodes.c +5 −5
  31. src/lopcodes.h +6 −7
  32. src/loslib.c +11 −20
  33. src/lparser.c +320 −145
  34. src/lparser.h +34 −11
  35. src/lstate.c +12 −8
  36. src/lstate.h +11 −7
  37. src/lstring.c +4 −3
  38. src/lstrlib.c +51 −22
  39. src/ltable.c +10 −8
  40. src/ltablib.c +4 −4
  41. src/ltm.c +4 −4
  42. src/ltm.h +2 −2
  43. src/lua.c +10 −11
  44. src/lua.h +4 −4
  45. src/luac.c +17 −12
  46. src/luaconf.h +57 −6
  47. src/lualib.h +2 −2
  48. src/lundump.c +30 −23
  49. src/lundump.h +7 −4
  50. src/lvm.c +97 −53
  51. src/lvm.h +7 −4
  52. src/lzio.c +12 −18
  53. src/lzio.h +2 −3
README+1−1
@@ -1,5 +1,5 @@
11
2−This is Lua 5.2 (alpha), released on 23 Nov 2010.
2+This is Lua 5.2 (beta), released on 13 Jun 2011.
33
44 For installation instructions, license details, and
55 further information about Lua, see doc/readme.html.
doc/contents.html+12−8
@@ -22,7 +22,7 @@ Lua 5.2 Reference Manual · 21 unmodified lines
2222
2323 <P>
2424 <IMG SRC="alert.png" ALIGN="absbottom" ALT="[!]">
25−<EM>This is an alpha version of Lua 5.2.
25+<EM>This is a beta version of Lua 5.2.
2626 Some details may change in the final version.</EM>
2727
2828 <P>
@@ -38,7 +38,7 @@ For a complete introduction to Lua programming, see the book · 9 unmodified lines
3838 <A HREF="#index">index</A>
3939 <HR>
4040 <SMALL>
41−Copyright &copy; 2010 Lua.org, PUC-Rio.
41+Copyright &copy; 2011 Lua.org, PUC-Rio.
4242 Freely available under the terms of the
4343 <a href="http://www.lua.org/license.html#5">Lua license</a>.
4444 </SMALL>
@@ -120,7 +120,7 @@ Freely available under the terms of the · 75 unmodified lines
120120 </UL>
121121 <LI><A HREF="manual.html#6.5">6.5 &ndash; Table Manipulation</A>
122122 <LI><A HREF="manual.html#6.6">6.6 &ndash; Mathematical Functions</A>
123−<LI><A HREF="manual.html#6.7">6.7 &ndash; Bitwise operations</A>
123+<LI><A HREF="manual.html#6.7">6.7 &ndash; Bitwise Operations</A>
124124 <LI><A HREF="manual.html#6.8">6.8 &ndash; Input and Output Facilities</A>
125125 <LI><A HREF="manual.html#6.9">6.9 &ndash; Operating System Facilities</A>
126126 <LI><A HREF="manual.html#6.10">6.10 &ndash; The Debug Library</A>
@@ -154,14 +154,13 @@ Freely available under the terms of the · 27 unmodified lines
154154 <A HREF="manual.html#pdf-ipairs">ipairs</A><BR>
155155 <A HREF="manual.html#pdf-load">load</A><BR>
156156 <A HREF="manual.html#pdf-loadfile">loadfile</A><BR>
157−<A HREF="manual.html#pdf-loadin">loadin</A><BR>
158−<A HREF="manual.html#pdf-loadstring">loadstring</A><BR>
159157 <A HREF="manual.html#pdf-next">next</A><BR>
160158 <A HREF="manual.html#pdf-pairs">pairs</A><BR>
161159 <A HREF="manual.html#pdf-pcall">pcall</A><BR>
162160 <A HREF="manual.html#pdf-print">print</A><BR>
163161 <A HREF="manual.html#pdf-rawequal">rawequal</A><BR>
164162 <A HREF="manual.html#pdf-rawget">rawget</A><BR>
163+<A HREF="manual.html#pdf-rawlen">rawlen</A><BR>
165164 <A HREF="manual.html#pdf-rawset">rawset</A><BR>
166165 <A HREF="manual.html#pdf-require">require</A><BR>
167166 <A HREF="manual.html#pdf-select">select</A><BR>
@@ -178,8 +177,10 @@ Freely available under the terms of the · 10 unmodified lines
178177 <A HREF="manual.html#pdf-bit32.bor">bit32.bor</A><BR>
179178 <A HREF="manual.html#pdf-bit32.btest">bit32.btest</A><BR>
180179 <A HREF="manual.html#pdf-bit32.bxor">bit32.bxor</A><BR>
180+<A HREF="manual.html#pdf-bit32.extract">bit32.extract</A><BR>
181181 <A HREF="manual.html#pdf-bit32.lrotate">bit32.lrotate</A><BR>
182182 <A HREF="manual.html#pdf-bit32.lshift">bit32.lshift</A><BR>
183+<A HREF="manual.html#pdf-bit32.replace">bit32.replace</A><BR>
183184 <A HREF="manual.html#pdf-bit32.rrotate">bit32.rrotate</A><BR>
184185 <A HREF="manual.html#pdf-bit32.rshift">bit32.rshift</A><BR>
185186
@@ -475,15 +476,18 @@ Freely available under the terms of the · 289 unmodified lines
475476 <A HREF="manual.html#luaL_dofile">luaL_dofile</A><BR>
476477 <A HREF="manual.html#luaL_dostring">luaL_dostring</A><BR>
477478 <A HREF="manual.html#luaL_error">luaL_error</A><BR>
478−<A HREF="manual.html#luaL_findtable">luaL_findtable</A><BR>
479+<A HREF="manual.html#luaL_execresult">luaL_execresult</A><BR>
480+<A HREF="manual.html#luaL_fileresult">luaL_fileresult</A><BR>
479481 <A HREF="manual.html#luaL_getmetafield">luaL_getmetafield</A><BR>
480482 <A HREF="manual.html#luaL_getmetatable">luaL_getmetatable</A><BR>
483+<A HREF="manual.html#luaL_getsubtable">luaL_getsubtable</A><BR>
481484 <A HREF="manual.html#luaL_gsub">luaL_gsub</A><BR>
482485 <A HREF="manual.html#luaL_len">luaL_len</A><BR>
483486 <A HREF="manual.html#luaL_loadbuffer">luaL_loadbuffer</A><BR>
484487 <A HREF="manual.html#luaL_loadfile">luaL_loadfile</A><BR>
485488 <A HREF="manual.html#luaL_loadstring">luaL_loadstring</A><BR>
486489 <A HREF="manual.html#luaL_newlib">luaL_newlib</A><BR>
490+<A HREF="manual.html#luaL_newlibtable">luaL_newlibtable</A><BR>
487491 <A HREF="manual.html#luaL_newmetatable">luaL_newmetatable</A><BR>
488492 <A HREF="manual.html#luaL_newstate">luaL_newstate</A><BR>
489493 <A HREF="manual.html#luaL_openlibs">luaL_openlibs</A><BR>
@@ -516,10 +520,10 @@ Freely available under the terms of the · 26 unmodified lines
516520 <HR>
517521 <SMALL>
518522 Last update:
519−Mon Nov 22 16:38:54 BRST 2010
523+Mon Jun 13 15:09:33 BRT 2011
520524 </SMALL>
521525 <!--
522−Last change: revised for Lua 5.2.0 (alpha)
526+Last change: revised for Lua 5.2.0 (beta)
523527 -->
524528
525529 </BODY>
doc/lua.css+20−1
@@ -8,6 +8,7 @@ body { · 7 unmodified lines
88 }
99
1010 h1, h2, h3, h4 {
11+ font-family: Verdana, sans-serif ;
1112 font-weight: normal ;
1213 font-style: italic ;
1314 }
@@ -27,7 +28,7 @@ h3 { · 13 unmodified lines
2728
2829 table h3 {
2930 padding-left: 0px ;
30− border-left: none ;
31+ border-left: none ;
3132 }
3233
3334 a:link {
@@ -62,3 +63,21 @@ hr { · 28 unmodified lines
6263 padding: 8px ;
6364 border: solid #a0a0a0 2px ;
6465 }
66+
67+.footer {
68+ color: gray ;
69+ font-size: small ;
70+}
71+
72+input[type=text] {
73+ border: solid #a0a0a0 2px ;
74+ border-radius: 2em ;
75+ -moz-border-radius: 2em ;
76+ background-image: url('images/search.png') ;
77+ background-repeat: no-repeat;
78+ background-position: left center ;
79+ background-position: 4px center ;
80+ padding-left: 20px ;
81+ height: 2em ;
82+}
83+
doc/manual.html+601−337
@@ -17,14 +17,14 @@ Lua 5.2 Reference Manual · 16 unmodified lines
1717 </h1>
1818
1919 <IMG SRC="alert.png" ALIGN="absbottom" ALT="[!]">
20−<EM>This is an alpha version of Lua 5.2.
20+<EM>This is a beta version of Lua 5.2.
2121 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; 2010 Lua.org, PUC-Rio.
27+Copyright &copy; 2011 Lua.org, PUC-Rio.
2828 Freely available under the terms of the
2929 <a href="http://www.lua.org/license.html#5">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.66 2010/11/22 18:07:40 roberto Exp $ -->
41+<!-- $Id: manual.of,v 1.77 2011/06/13 16:27:54 roberto Exp $ -->
4242
4343
4444
@@ -53,7 +53,7 @@ It also offers good support for object-oriented programming, · 8 unmodified lines
5353 functional programming, and data-driven programming.
5454 Lua is intended to be used as a powerful, light-weight
5555 scripting language for any program that needs one.
56−Lua is implemented as a library, written in <em>clean</em> C,
56+Lua is implemented as a library, written in <em>clean C</em>,
5757 the common subset of ANSI&nbsp;C and C++.
5858
5959
@@ -125,11 +125,14 @@ it usually represents the absence of a useful value. · 65 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 real (double-precision floating-point) numbers.
128+Operations on numbers follow the same rules of
129+the underlying C&nbsp;implementation,
130+which, in turn, usually follows the IEEE 754 standard.
128131 (It is easy to build Lua interpreters that use other
129132 internal representations for numbers,
130133 such as single-precision floats or long integers;
131134 see file <code>luaconf.h</code>.)
132−<em>String</em> represents arrays of characters.
135+<em>String</em> represents immutable sequences of bytes.
133136
134137 Lua is 8-bit clean:
135138 strings can contain any 8-bit value,
@@ -167,11 +170,19 @@ even those that do not support threads. · 31 unmodified lines
167170 <p>
168171 The type <em>table</em> implements associative arrays,
169172 that is, arrays that can be indexed not only with numbers,
170−but with any value (except <b>nil</b>).
173+but with any Lua value except <b>nil</b> and NaN
174+(<em>Not a Number</em>, a special numeric value used to represent
175+undefined or unrepresentable results, such as <code>0/0</code>).
171176 Tables can be <em>heterogeneous</em>;
172177 that is, they can contain values of all types (except <b>nil</b>).
178+Any key with value <b>nil</b> is not considered part of the table.
179+Conversely, any key that is not part of a table has
180+an associated value <b>nil</b>.
181+
182+
183+<p>
173184 Tables are the sole data structuring mechanism in Lua;
174−they can be used to represent ordinary arrays,
185+they can be used to represent ordinary arrays, sequences,
175186 symbol tables, sets, records, graphs, trees, etc.
176187 To represent records, Lua uses the field name as an index.
177188 The language supports this representation by
@@ -180,15 +191,29 @@ There are several convenient ways to create tables in Lua · 2 unmodified lines
180191 (see <a href="#3.4.8">&sect;3.4.8</a>).
181192
182193
194+<p>
195+We use the term <em>sequence</em> to denote a table where all
196+integer keys comprise the set <em>{1..n}</em> for some integer <em>n</em>,
197+which is called the length of the sequence (see <a href="#3.4.6">&sect;3.4.6</a>).
198+
199+
183200 <p>
184201 Like indices,
185−the value of a table field can be of any type (except <b>nil</b>).
202+the value of a table field can be of any type.
186203 In particular,
187204 because functions are first-class values,
188205 table fields can contain functions.
189206 Thus tables can also carry <em>methods</em> (see <a href="#3.4.10">&sect;3.4.10</a>).
190207
191208
209+<p>
210+The indexing of tables follows
211+the definition of equality in the language.
212+The expressions <code>a[i]</code> and <code>a[j]</code>
213+denote the same table element
214+if and only if <code>i == j</code>.
215+
216+
192217 <p>
193218 Tables, functions, threads, and (full) userdata values are <em>objects</em>:
194219 variables do not actually <em>contain</em> these values,
@@ -210,7 +235,7 @@ of a given value. · 15 unmodified lines
210235
211236 <p>
212237 As discussed in <a href="#3.2">&sect;3.2</a> and <a href="#3.3.3">&sect;3.3.3</a>,
213−any reference to a global name <code>var</code> is syntactically translated
238+any reference to a global name <code>var</code> is syntactically translated
214239 to <code>_ENV.var</code>.
215240 Moreover, every chunk is compiled in the scope of an external
216241 variable called <code>_ENV</code> (see <a href="#3.3.2">&sect;3.3.2</a>),
@@ -241,13 +266,13 @@ In Lua, the variable <a href="#pdf-_G"><code>_G</code></a> is initialized with t · 24 unmodified lines
241266
242267 <p>
243268 When Lua compiles a chunk,
244−it initializes the value of its <code>_ENV</code> variable
269+it initializes the value of its <code>_ENV</code> variable
245270 with the global environment (see <a href="#pdf-load"><code>load</code></a>).
246271 Therefore, by default,
247272 global variables in Lua code refer to entries in the global environment.
248273 Moreover, all standard libraries are loaded in the global environment,
249274 and several functions there operate on that environment.
250−You can use <a href="#pdf-loadin"><code>loadin</code></a> to load a chunk with a different environment.
275+You can use <a href="#pdf-load"><code>load</code></a> to load a chunk with a different environment.
251276 (In C, you have to load the chunk and then change the value
252277 of its first upvalue.)
253278
@@ -283,7 +308,32 @@ which can take appropriate measures · 29 unmodified lines
283308 Lua code can explicitly generate an error by calling the
284309 <a href="#pdf-error"><code>error</code></a> function.
285310 If you need to catch errors in Lua,
286−you can use the <a href="#pdf-pcall"><code>pcall</code></a> or the <a href="#pdf-xpcall"><code>xpcall</code></a> function.
311+you can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code></a>
312+to call a given function in <em>protected mode</em>.
313+
314+
315+<p>
316+Whenever there is an error,
317+an <em>error object</em> (also called an <em>error message</em>)
318+is propagated with information about the error.
319+Lua itself only generates errors where the error object is a string,
320+but programs may generate errors with
321+any value for the error object.
322+
323+
324+<p>
325+When you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall</code></a>,
326+you may give an <em>message handler</em>
327+to be called in case of errors.
328+This function is called with the original error message
329+and returns a new error message.
330+It is called before the error unwonds the stack,
331+so that it can gather more information about the error,
332+for instance by inspecting the stack and creating a stack traceback.
333+This message handler is still protected by the protected call;
334+so, an error inside the message handler
335+will call the message handler again.
336+If this loop goes on, Lua breaks it and returns an appropriate message.
287337
288338
289339
@@ -307,7 +357,7 @@ Lua calls this function to perform the addition. · 17 unmodified lines
307357 <p>
308358 The keys in a metatable are derived from the <em>event</em> names;
309359 the corresponding values are called <em>metamethods</em>.
310−In the previous example, the event is <code>"add"</code>
360+In the previous example, the event is <code>"add"</code>
311361 and the metamethod is the function that performs the addition.
312362
313363
@@ -623,6 +673,11 @@ equivalent to <code>not (b &lt; a)</code>. · 309 unmodified lines
623673
624674 <li><b>"index":</b>
625675 The indexing access <code>table[key]</code>.
676+Note that the metamethod is tried only
677+when <code>key</code> is not present in <code>table</code>.
678+(When <code>table</code> is not a table,
679+no key is ever present,
680+so the metamethod is always tried.)
626681
627682
628683 <pre>
@@ -630,6 +685,7 @@ The indexing access <code>table[key]</code>. · 1 unmodified line
630685 local h
631686 if type(table) == "table" then
632687 local v = rawget(table, key)
688+ -- if key is present, return raw value
633689 if v ~= nil then return v end
634690 h = metatable(table).__index
635691 if h == nil then return nil end
@@ -649,6 +705,8 @@ The indexing access <code>table[key]</code>. · 13 unmodified lines
649705
650706 <li><b>"newindex":</b>
651707 The indexing assignment <code>table[key] = value</code>.
708+Note that the metamethod is tried only
709+when <code>key</code> is not present in <code>table</code>.
652710
653711
654712 <pre>
@@ -656,6 +714,7 @@ The indexing assignment <code>table[key] = value</code>. · 1 unmodified line
656714 local h
657715 if type(table) == "table" then
658716 local v = rawget(table, key)
717+ -- if key is present, do raw assignment
659718 if v ~= nil then rawset(table, key, value); return end
660719 h = metatable(table).__newindex
661720 if h == nil then rawset(table, key, value); return end
@@ -758,7 +817,7 @@ memory usage. · 96 unmodified lines
758817 <p>
759818 You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C
760819 or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
761−With these functions you can also control
820+With these functions you can also control
762821 the collector directly (e.g., stop and restart it).
763822
764823
@@ -766,8 +825,9 @@ the collector directly (e.g., stop and restart it). · 1 unmodified line
766825 <h3>2.5.1 &ndash; <a name="2.5.1">Garbage-Collection Metamethods</a></h3>
767826
768827 <p>
769−Using the C&nbsp;API,
770−you can set garbage-collector metamethods for full userdata (see <a href="#2.4">&sect;2.4</a>).
828+You can set garbage-collector metamethods for tables
829+and, using the C&nbsp;API,
830+for full userdata (see <a href="#2.4">&sect;2.4</a>).
771831 These metamethods are also called <em>finalizers</em>.
772832 Finalizers allow you to coordinate Lua's garbage collection
773833 with external resource management
@@ -776,43 +836,43 @@ or freeing your own memory). · 2 unmodified lines
776836
777837
778838 <p>
779−For a userdata to be finalized when collected,
839+For an object (table or userdata) to be finalized when collected,
780840 you must <em>mark</em> it for finalization.
781841
782−You mark a userdata for finalization when you set its metatable
842+You mark an object for finalization when you set its metatable
783843 and the metatable has a field indexed by the string "<code>__gc</code>".
784844 Note that if you set a metatable without a <code>__gc</code> field
785845 and later create that field in the metatable,
786−the userdata will not be marked for finalization.
846+the object will not be marked for finalization.
787847 However, after a userdata is marked,
788848 you can freely change the <code>__gc</code> field of its metatable.
789849
790850
791851 <p>
792−When a marked userdata becomes garbage,
852+When a marked object becomes garbage,
793853 it is not collected immediately by the garbage collector.
794854 Instead, Lua puts it in a list.
795855 After the collection,
796856 Lua does the equivalent of the following function
797−for each userdata in that list:
857+for each object in that list:
798858
799859 <pre>
800− function gc_event (udata)
801− local h = metatable(udata).__gc
860+ function gc_event (obj)
861+ local h = metatable(obj).__gc
802862 if type(h) == "function" then
803− h(udata)
863+ h(obj)
804864 end
805865 end
806866 </pre>
807867
808868 <p>
809869 At the end of each garbage-collection cycle,
810−the finalizers for userdata are called in
870+the finalizers for objects are called in
811871 the reverse order that they were marked for collection,
812872 among those collected in that cycle;
813873 that is, the first finalizer to be called is the one associated
814−with the userdata marked last in the program.
815−The userdata memory is freed only in the next garbage-collection cycle.
874+with the object marked last in the program.
875+The object memory is freed only in the next garbage-collection cycle.
816876
817877
818878
@@ -873,15 +933,13 @@ Lua treats strings and light C functions as non-object values. · 54 unmodified lines
873933
874934
875935 <p>
876−Userdata marked for finalization have a special behavior in weak tables.
877−When a marked userdata is a value in a weak table,
878−it is removed from the table the first time it is collected,
879−before running its finalizer.
936+Objects marked for finalization have a special behavior in weak tables.
937+When a marked object is a value in a weak table,
938+it is removed from the table before running its finalizer.
880939 However, when it is a key,
881−it is removed from the table only when it is really freed,
882−after running its finalizer.
883−This behavior allows the finalizer to access values
884−associated with the userdata through weak tables.
940+it is removed from the table only after running its finalizer.
941+This behavior allows the finalizer to access properties
942+associated with the object through weak tables.
885943
886944
887945
@@ -980,7 +1038,7 @@ consider the following code: · 92 unmodified lines
9801038 print("co-body", r, s)
9811039 return b, "end"
9821040 end)
983−
1041+
9841042 print("main", coroutine.resume(co, 1, 10))
9851043 print("main", coroutine.resume(co, "r"))
9861044 print("main", coroutine.resume(co, "x", "y"))
@@ -1048,8 +1106,7 @@ except as delimiters between names and keywords. · 61 unmodified lines
10481106 in Lua can be any string of letters,
10491107 digits, and underscores,
10501108 not beginning with a digit.
1051−This coincides with the definition of names in most languages.
1052−Identifiers are used to name variables and table fields.
1109+Identifiers are used to name variables, table fields, and labels.
10531110
10541111
10551112 <p>
@@ -1058,10 +1115,10 @@ and cannot be used as names: · 2 unmodified lines
10581115
10591116
10601117 <pre>
1061− and break do else elseif
1062− end false for function if
1063− in local nil not or
1064− repeat return then true until while
1118+ and break do else elseif end
1119+ false for function goto if in
1120+ local nil not or repeat return
1121+ then true until while
10651122 </pre>
10661123
10671124 <p>
@@ -1102,7 +1159,7 @@ results in a newline in the string. · 34 unmodified lines
11021159 The escape sequence '<code>\*</code>' skips the following span
11031160 of white-space characters,
11041161 including line breaks;
1105−it is particularly useful to break and indent a long string
1162+it is particularly useful to break and indent a long string
11061163 into multiple lines without adding the newlines and spaces
11071164 into the string contents.
11081165
@@ -1140,9 +1197,18 @@ Any kind of end-of-line sequence · 31 unmodified lines
11401197 (carriage return, newline, carriage return followed by newline,
11411198 or newline followed by carriage return)
11421199 is converted to a simple newline.
1143−You should not use long strings for non-text data;
1144−Use instead a regular quoted literal with explicit escape sequences
1145−for control characters.
1200+
1201+
1202+<p>
1203+When parsing a from a string source,
1204+any byte in a literal string not
1205+explicitly affected by the previous rules represents itself.
1206+However, Lua opens files for parsing in text mode,
1207+and the system file functions may have problems with
1208+some control characters.
1209+So, it is safer to represent
1210+non-text data as a quoted literal with
1211+explicit escape sequences for non-text characters.
11461212
11471213
11481214 <p>
@@ -1166,14 +1232,19 @@ the five literal strings below denote the same string: · 17 unmodified lines
11661232 </pre>
11671233
11681234 <p>
1169−A <em>numerical constant</em> can be written with an optional decimal part
1170−and an optional decimal exponent.
1171−Lua also accepts integer hexadecimal constants,
1172−by prefixing them with <code>0x</code>.
1235+A <em>numerical constant</em> can be written with an optional fractional part
1236+and an optional decimal exponent,
1237+marked by a letter '<code>e</code>' or '<code>E</code>'.
1238+Lua also accepts hexadecimal constants,
1239+which start with <code>0x</code> or <code>0X</code>.
1240+Hexadecimal constants also accept an optional fractional part
1241+plus an optional binary exponent,
1242+marked by a letter '<code>e</code>' or '<code>E</code>'.
11731243 Examples of valid numerical constants are
11741244
11751245 <pre>
1176− 3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56
1246+ 3 3.0 3.1416 314.16e-2 0.31416E1
1247+ 0xff 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
11771248 </pre>
11781249
11791250 <p>
@@ -1194,7 +1265,6 @@ Long comments are frequently used to disable code temporarily. · 14 unmodified lines
11941265
11951266 <p>
11961267 Variables are places that store values.
1197−
11981268 There are three kinds of variables in Lua:
11991269 global variables, local variables, and table fields.
12001270
@@ -1293,7 +1363,7 @@ A block can be explicitly delimited to produce a single statement: · 92 unmodified lines
12931363 Explicit blocks are useful
12941364 to control the scope of variable declarations.
12951365 Explicit blocks are also sometimes used to
1296−add a <b>return</b> or <b>break</b> statement in the middle
1366+add a <b>return</b> statement in the middle
12971367 of another block (see <a href="#3.3.4">&sect;3.3.4</a>).
12981368
12991369
@@ -1312,7 +1382,7 @@ a chunk is simply a block: · 12 unmodified lines
13121382 </pre>
13131383
13141384 <p>
1315−Lua handles a chunk as the body of an anonymous function
1385+Lua handles a chunk as the body of an anonymous function
13161386 with a variable number of arguments
13171387 (see <a href="#3.4.10">&sect;3.4.10</a>).
13181388 As such, chunks can define local variables,
@@ -1452,38 +1522,60 @@ declared inside the loop block. · 133 unmodified lines
14521522
14531523
14541524 <p>
1455−The <b>return</b> statement is used to return values
1456−from a function or a chunk (which is just a function).
1525+The <b>goto</b> statement transfers the program control to a label.
1526+For syntactical reasons,
1527+labels in Lua are considered statements too:
1528+
14571529
1458−Functions can return more than one value,
1459−so the syntax for the <b>return</b> statement is
14601530
14611531 <pre>
1462− stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
1532+ stat ::= <b>goto</b> Name
1533+ stat ::= label
1534+ label ::= &lsquo;<b>@</b>&rsquo; Name &lsquo;<b>:</b>&rsquo;
14631535 </pre>
14641536
14651537 <p>
1466−The <b>break</b> statement is used to terminate the execution of a
1538+A label is visible in the entire block where it is defined
1539+(including nested blocks, but not nested functions).
1540+A goto may jump to any visible label as long as it does not
1541+enter into the scope of a local variable.
1542+
1543+
1544+<p>
1545+Both labels and empty statements are called <em>void statements</em>,
1546+as they perform no actions.
1547+
1548+
1549+<p>
1550+The <b>break</b> statement terminates the execution of a
14671551 <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
14681552 skipping to the next statement after the loop:
14691553
14701554
14711555 <pre>
1472− stat ::= <b>break</b> [&lsquo;<b>;</b>&rsquo;]
1556+ stat ::= <b>break</b>
14731557 </pre><p>
14741558 A <b>break</b> ends the innermost enclosing loop.
14751559
14761560
14771561 <p>
1478−The <b>return</b> and <b>break</b>
1479−statements can only be written as the <em>last</em> statement of a block.
1480−If it is really necessary to <b>return</b> or <b>break</b> in the
1481−middle of a block,
1562+The <b>return</b> statement is used to return values
1563+from a function or a chunk (which is a function in disguise).
1564+
1565+Functions can return more than one value,
1566+so the syntax for the <b>return</b> statement is
1567+
1568+<pre>
1569+ stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
1570+</pre>
1571+
1572+<p>
1573+The <b>return</b> statement can only be written
1574+as the last statement of a block.
1575+If it is really necessary to <b>return</b> in the middle of a block,
14821576 then an explicit inner block can be used,
1483−as in the idioms
1484−<code>do return end</code> and <code>do break end</code>,
1485−because now <b>return</b> and <b>break</b> are the last statements in
1486−their (inner) blocks.
1577+as in the idiom <code>do return end</code>,
1578+because now <b>return</b> is the last statement in its (inner) block.
14871579
14881580
14891581
@@ -1582,8 +1674,8 @@ is equivalent to the code: · 92 unmodified lines
15821674 local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em>
15831675 while true do
15841676 local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>)
1677+ if <em>var_1</em> == nil then break end
15851678 <em>var</em> = <em>var_1</em>
1586− if <em>var</em> == nil then break end
15871679 <em>block</em>
15881680 end
15891681 end
@@ -1649,7 +1741,6 @@ Otherwise, all variables are initialized with <b>nil</b>. · 59 unmodified lines
16491741 <p>
16501742 A chunk is also a block (see <a href="#3.3.2">&sect;3.3.2</a>),
16511743 and so local variables can be declared in a chunk outside any explicit block.
1652−The scope of such local variables extends until the end of the chunk.
16531744
16541745
16551746 <p>
@@ -1671,7 +1762,7 @@ The basic expressions in Lua are the following: · 15 unmodified lines
16711762 exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
16721763 exp ::= Number
16731764 exp ::= String
1674− exp ::= function
1765+ exp ::= functiondef
16751766 exp ::= tableconstructor
16761767 exp ::= &lsquo;<b>...</b>&rsquo;
16771768 exp ::= exp binop exp
@@ -1776,7 +1867,9 @@ the quotient towards minus infinity. · 98 unmodified lines
17761867 Lua provides automatic conversion between
17771868 string and number values at run time.
17781869 Any arithmetic operation applied to a string tries to convert
1779−this string to a number, following the usual conversion rules.
1870+this string to a number, following the rules of the Lua lexer.
1871+(The string may have leading and trailing spaces,
1872+plus an optional sign.)
17801873 Conversely, whenever a number is used where a string is expected,
17811874 the number is converted to a string, in a reasonable format.
17821875 For complete control over how numbers are converted to strings,
@@ -1802,8 +1895,8 @@ If the types are different, then the result is <b>false</b>. · 19 unmodified lines
18021895 Otherwise, the values of the operands are compared.
18031896 Numbers and strings are compared in the usual way.
18041897 Tables, userdata, and threads
1805−are compared by <em>reference</em>:
1806−two objects are considered equal only if they are the <em>same</em> object.
1898+are compared by reference:
1899+two objects are considered equal only if they are the same object.
18071900 Every time you create a new object
18081901 (a table, userdata, or thread),
18091902 this new object is different from any previously existing object.
@@ -1813,13 +1906,13 @@ Closures with any detectable difference · 3 unmodified lines
18131906
18141907
18151908 <p>
1816−You can change the way that Lua compares tables and userdata
1909+You can change the way that Lua compares tables and userdata
18171910 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>).
18181911
18191912
18201913 <p>
18211914 The conversion rules of <a href="#3.4.2">&sect;3.4.2</a>
1822−<em>do not</em> apply to equality comparisons.
1915+do not apply to equality comparisons.
18231916 Thus, <code>"0"==0</code> evaluates to <b>false</b>,
18241917 and <code>t[0]</code> and <code>t["0"]</code> denote different
18251918 entries in a table.
@@ -1902,25 +1995,27 @@ character is one byte). · 76 unmodified lines
19021995
19031996
19041997 <p>
1905−The length of a table <code>t</code> can be any integer index <code>n</code>
1906−such that <code>t[n]</code> is not <b>nil</b> and <code>t[n+1]</code> is <b>nil</b>;
1907−moreover, if <code>t[1]</code> is <b>nil</b>, <code>n</code> can be zero.
1908−(All accesses are assumed to be raw for this description.)
1909−For a regular array, where all non-nil values
1910−have keys from 1 to a given <code>n</code>,
1911−its length is exactly that <code>n</code>,
1912−the index of its last value.
1913−If the array has "holes"
1914−(that is, <b>nil</b> values between other non-nil values),
1915−then <code>#t</code> can be any of the indices that
1916−directly precedes a <b>nil</b> value
1917−(that is, it may consider any such <b>nil</b> value as the end of
1918−the array).
1998+The length of a table <code>t</code> is only defined if the
1999+table is a <em>sequence</em>,
2000+that is,
2001+all its numeric keys comprise the set <em>{1..n}</em> for some integer <em>n</em>.
2002+In that case, <em>n</em> is its length.
2003+Note that a table like
2004+
2005+<pre>
2006+ {10, 20, nil, 40}
2007+</pre><p>
2008+is not a sequence, because it has the key <code>4</code>
2009+but does not have the key <code>3</code>.
2010+(So, there is no <em>n</em> such that the set <em>{1..n}</em> is equal
2011+to the set of numeric keys of that table.)
2012+Note, however, that non-numeric keys do not interfere
2013+with whether a table is a sequence.
19192014
19202015
19212016 <p>
19222017 A program can modify the behavior of the length operator for
1923−any value but strings through metamethods (see <a href="#2.4">&sect;2.4</a>).
2018+any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">&sect;2.4</a>).
19242019
19252020
19262021
@@ -1999,9 +2094,6 @@ If the last field in the list has the form <code>exp</code> · 72 unmodified lines
19992094 and the expression is a function call or a vararg expression,
20002095 then all values returned by this expression enter the list consecutively
20012096 (see <a href="#3.4.9">&sect;3.4.9</a>).
2002−To avoid this,
2003−enclose the function call or the vararg expression
2004−in parentheses (see <a href="#3.4">&sect;3.4</a>).
20052097
20062098
20072099 <p>
@@ -2060,7 +2152,7 @@ that is, the argument list is a single literal string. · 52 unmodified lines
20602152
20612153
20622154 <p>
2063−A call of the form <code>return</code> <em>functioncall</em> is called
2155+A call of the form <code>return <em>functioncall</em></code> is called
20642156 a <em>tail call</em>.
20652157 Lua implements <em>proper tail calls</em>
20662158 (or <em>proper tail recursion</em>):
@@ -2093,7 +2185,7 @@ So, none of the following examples are tail calls: · 26 unmodified lines
20932185 The syntax for function definition is
20942186
20952187 <pre>
2096− function ::= <b>function</b> funcbody
2188+ functiondef ::= <b>function</b> funcbody
20972189 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
20982190 </pre>
20992191
@@ -2135,7 +2227,7 @@ translates to · 35 unmodified lines
21352227 <pre>
21362228 local f; f = function () <em>body</em> end
21372229 </pre><p>
2138−<em>not</em> to
2230+not to
21392231
21402232 <pre>
21412233 local f = function () <em>body</em> end
@@ -2153,8 +2245,6 @@ Then, whenever Lua executes the function definition, · 11 unmodified lines
21532245 the function is <em>instantiated</em> (or <em>closed</em>).
21542246 This function instance (or <em>closure</em>)
21552247 is the final value of the expression.
2156−Different instances of the same function
2157−can refer to different external local variables.
21582248
21592249
21602250 <p>
@@ -2249,9 +2339,9 @@ is syntactic sugar for · 88 unmodified lines
22492339 <p>
22502340
22512341 Lua is a lexically scoped language.
2252−The scope of variables begins at the first statement <em>after</em>
2253−their declaration and lasts until the end of the innermost block that
2254−includes the declaration.
2342+The scope of a local variable begins at the first statement after
2343+its declaration and lasts until the last non-void statement
2344+of the innermost block that includes the declaration.
22552345 Consider the following example:
22562346
22572347 <pre>
@@ -2356,9 +2446,9 @@ For convenience, · 98 unmodified lines
23562446 most query operations in the API do not follow a strict stack discipline.
23572447 Instead, they can refer to any element in the stack
23582448 by using an <em>index</em>:
2359−A positive index represents an <em>absolute</em> stack position
2449+A positive index represents an absolute stack position
23602450 (starting at&nbsp;1);
2361−a negative index represents an <em>offset</em> relative to the top of the stack.
2451+a negative index represents an offset relative to the top of the stack.
23622452 More specifically, if the stack has <em>n</em> elements,
23632453 then index&nbsp;1 represents the first element
23642454 (that is, the element that was pushed onto the stack first)
@@ -2594,7 +2684,7 @@ because its frame in the C stack was destroyed by the yield. · 229 unmodified lines
25942684 Instead, Lua calls a <em>continuation function</em>,
25952685 which was given as an argument to the callee function.
25962686 As the name implies,
2597−the continuation function should continue the task
2687+the continuation function should continue the task
25982688 of the original function.
25992689
26002690
@@ -2684,8 +2774,7 @@ is being allocated; · 83 unmodified lines
26842774
26852775 <p>
26862776 When <code>ptr</code> is not <code>NULL</code>,
2687−<code>osize</code> is the previous size of the block
2688−pointed by <code>ptr</code>,
2777+<code>osize</code> is the size of the block pointed by <code>ptr</code>,
26892778 that is, the size given when it was allocated or reallocated.
26902779
26912780
@@ -2705,18 +2794,14 @@ Lua assumes the following behavior from the allocator function: · 13 unmodified lines
27052794
27062795
27072796 <p>
2708−When <code>nsize</code> is zero:
2709−if <code>ptr</code> is not <code>NULL</code>,
2710−the allocator should free the block pointed to by <code>ptr</code>.
2711−Anyway, the allocator must return <code>NULL</code>.
2797+When <code>nsize</code> is zero,
2798+the allocator should behave like <code>free</code>
2799+and return <code>NULL</code>.
27122800
27132801
27142802 <p>
2715−When <code>nsize</code> is not zero:
2716−if <code>ptr</code> is <code>NULL</code>,
2717−the allocator should behave like <code>malloc</code>;
2718−when <code>ptr</code> is not <code>NULL</code>,
2719−the allocator behaves like <code>realloc</code>.
2803+When <code>nsize</code> is not zero,
2804+the allocator should behave like <code>realloc</code>.
27202805 The allocator returns <code>NULL</code>
27212806 if and only if it cannot fill the request.
27222807 Lua assumes that the allocator never fails when
@@ -2751,12 +2836,14 @@ but it seems a safe assumption. · 28 unmodified lines
27512836
27522837
27532838 <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
2754−<span class="apii">[-2, +1, <em>e</em>]</span>
2839+<span class="apii">[-(2/1), +1, <em>e</em>]</span>
27552840 <pre>int lua_arith (lua_State *L, int op);</pre>
27562841
27572842 <p>
2758−Performs an arithmetic operation over the two values at the top
2759−of the stack (with the value at the top being the second operand),
2843+Performs an arithmetic operation over the two values
2844+(or one, in the case of negation)
2845+at the top of the stack,
2846+with the value at the top being the second operand,
27602847 pops these values, and pushes the result of the operation.
27612848 The function follows the semantics of the corresponding Lua operator
27622849 (that is, it may call metamethods).
@@ -2824,7 +2911,7 @@ when the function is called. · 61 unmodified lines
28242911 The function results are pushed onto the stack when the function returns.
28252912 The number of results is adjusted to <code>nresults</code>,
28262913 unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
2827−In this case, <em>all</em> results from the function are pushed.
2914+In this case, all results from the function are pushed.
28282915 Lua takes care that the returned values fit into the stack space.
28292916 The function results are pushed onto the stack in direct order
28302917 (the first result is pushed first),
@@ -3028,10 +3115,13 @@ without shifting any element · 197 unmodified lines
30283115
30293116 <p>
30303117 Creates a new empty table and pushes it onto the stack.
3031−The new table has space pre-allocated
3032−for <code>narr</code> array elements and <code>nrec</code> non-array elements.
3033−This pre-allocation is useful when you know exactly how many elements
3118+Parameter <code>narr</code> is a hint for how many elements the table
3119+will have as a sequence;
3120+parameter <code>nrec</code> is a hint for how many other elements
30343121 the table will have.
3122+Lua may use these hints to preallocate memory for the new table.
3123+This pre-allocation is useful for performance when you know in advance
3124+how many elements the table will have.
30353125 Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
30363126
30373127
@@ -3147,6 +3237,11 @@ returns a boolean that tells whether the collector is running · 109 unmodified lines
31473237
31483238 </ul>
31493239
3240+<p>
3241+For more details about some options,
3242+see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
3243+
3244+
31503245
31513246
31523247
@@ -3655,7 +3750,7 @@ A typical traversal looks like this: · 502 unmodified lines
36553750 While traversing a table,
36563751 do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
36573752 unless you know that the key is actually a string.
3658−Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> <em>changes</em>
3753+Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
36593754 the value at the given index;
36603755 this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
36613756
@@ -3683,7 +3778,7 @@ Lua to operate with another type for numbers (e.g., float or long). · 21 unmodified lines
36833778
36843779 <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
36853780 <span class="apii">[-(nargs + 1), +(nresults|1), <em>-</em>]</span>
3686−<pre>int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);</pre>
3781+<pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
36873782
36883783 <p>
36893784 Calls a function in protected mode.
@@ -3704,19 +3799,20 @@ and its arguments from the stack. · 14 unmodified lines
37043799
37053800
37063801 <p>
3707−If <code>errfunc</code> is 0,
3802+If <code>msgh</code> is 0,
37083803 then the error message returned on the stack
37093804 is exactly the original error message.
3710−Otherwise, <code>errfunc</code> is the stack index of an
3711−<em>error handler function</em>.
3805+Otherwise, <code>msgh</code> is the stack index of a
3806+<em>message handler</em>.
37123807 (In the current implementation, this index cannot be a pseudo-index.)
37133808 In case of runtime errors,
37143809 this function will be called with the error message
3715−and its return value will be the message returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
3810+and its return value will be the message
3811+returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
37163812
37173813
37183814 <p>
3719−Typically, the error handler function is used to add more debug
3815+Typically, the message handler is used to add more debug
37203816 information to the error message, such as a stack traceback.
37213817 Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
37223818 since by then the stack has unwound.
@@ -3737,11 +3833,11 @@ a runtime error. · 14 unmodified lines
37373833
37383834 <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>:</b>
37393835 memory allocation error.
3740−For such errors, Lua does not call the error handler function.
3836+For such errors, Lua does not call the message handler.
37413837 </li>
37423838
37433839 <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>:</b>
3744−error while running the error handler function.
3840+error while running the message handler.
37453841 </li>
37463842
37473843 <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>:</b>
@@ -3944,7 +4040,8 @@ onto the stack. · 196 unmodified lines
39444040 Lua makes (or reuses) an internal copy of the given string,
39454041 so the memory at <code>s</code> can be freed or reused immediately after
39464042 the function returns.
3947−The string can contain embedded zeros.
4043+The string can contain any binary data,
4044+including embedded zeros.
39484045
39494046
39504047 <p>
@@ -3986,8 +4083,6 @@ onto the stack. · 35 unmodified lines
39864083 Lua makes (or reuses) an internal copy of the given string,
39874084 so the memory at <code>s</code> can be freed or reused immediately after
39884085 the function returns.
3989−The string should not contain embedded zeros;
3990−it is assumed to end at the first zero.
39914086
39924087
39934088 <p>
@@ -4249,14 +4344,11 @@ with user data <code>ud</code>. · 255 unmodified lines
42494344
42504345 <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p>
42514346 <span class="apii">[-1, +0, <em>-</em>]</span>
4252−<pre>int lua_setuservalue (lua_State *L, int index);</pre>
4347+<pre>void lua_setuservalue (lua_State *L, int index);</pre>
42534348
42544349 <p>
42554350 Pops a table or <b>nil</b> from the stack and sets it as
42564351 the new value associated to the userdata at the given index.
4257−If the value at the given index is not a userdata,
4258−<code>lua_setuservalue</code> returns 0.
4259−Otherwise it returns 1.
42604352
42614353
42624354
@@ -4296,7 +4388,7 @@ It is defined as a macro. · 33 unmodified lines
42964388
42974389 <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
42984390 <span class="apii">[-1, +0, <em>-</em>]</span>
4299−<pre>int lua_setmetatable (lua_State *L, int index);</pre>
4391+<pre>void lua_setmetatable (lua_State *L, int index);</pre>
43004392
43014393 <p>
43024394 Pops a table from the stack and
@@ -4716,7 +4808,7 @@ calling the writer again. · 413 unmodified lines
47164808 <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
47174809
47184810 <p>
4719−Exchange values between different threads of the <em>same</em> global state.
4811+Exchange values between different threads of the same global state.
47204812
47214813
47224814 <p>
@@ -5033,17 +5125,14 @@ In the first case, · 310 unmodified lines
50335125 the parameter <code>ar</code> must be a valid activation record that was
50345126 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
50355127 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5036−The index <code>n</code> selects which local variable to inspect
5037−(1 is the first parameter or active local variable, and so on,
5038−until the last active local variable).
5039−<a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
5040−and returns its name.
5128+The index <code>n</code> selects which local variable to inspect;
5129+see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
5130+and names.
50415131
50425132
50435133 <p>
5044−Variable names starting with '<code>(</code>' (open parentheses)
5045−represent internal variables
5046−(loop control variables, temporaries, and C&nbsp;function locals).
5134+<a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
5135+and returns its name.
50475136
50485137
50495138 <p>
@@ -5176,7 +5265,8 @@ before the function gets its arguments. · 126 unmodified lines
51765265
51775266 <li><b>The return hook:</b> is called when the interpreter returns from a function.
51785267 The hook is called just before Lua leaves the function.
5179−You have no access to the values to be returned by the function.
5268+There is no standard way to access the values
5269+to be returned by the function.
51805270 </li>
51815271
51825272 <li><b>The line hook:</b> is called when the interpreter is about to
@@ -5285,7 +5375,7 @@ refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>fidx2< · 102 unmodified lines
52855375
52865376 The <em>auxiliary library</em> provides several convenient functions
52875377 to interface C with Lua.
5288−While the basic API provides the primitive functions for all
5378+While the basic API provides the primitive functions for all
52895379 interactions between C and Lua,
52905380 the auxiliary library provides higher-level functions for some
52915381 common tasks.
@@ -5816,15 +5906,28 @@ as <code>return luaL_error(<em>args</em>)</code>. · 524 unmodified lines
58165906
58175907
58185908
5819−<hr><h3><a name="luaL_findtable"><code>luaL_findtable</code></a></h3><p>
5820−<span class="apii">[-0, +1, <em>m</em>]</span>
5821−<pre>void luaL_findtable (lua_State *L, int idx, const char *fname);</pre>
5909+<hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
5910+<span class="apii">[-0, +(1/3), <em>m</em>]</span>
5911+<pre>int luaL_execresult (lua_State *L, int stat);</pre>
58225912
58235913 <p>
5824−Ensures that the value <code>t[fname]</code>,
5825−where <code>t</code> is the value at the valid index <code>idx</code>,
5826−is a table,
5827−and pushes that table onto the stack.
5914+This function produces the return values for
5915+process-related functions in the standard library
5916+(<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>).
5917+
5918+
5919+
5920+
5921+
5922+<hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
5923+<span class="apii">[-0, +(1/3), <em>m</em>]</span>
5924+<pre>int luaL_fileresult (lua_State *L, int stat,
5925+ const char *fname)</pre>
5926+
5927+<p>
5928+This function produces the return values for
5929+file-related functions in the standard library
5930+(<a href="#pdf-io.open"><code>io.open</code></a>, <a href="#pdf-os.rename"><code>os.rename</code></a>, <a href="#pdf-file:seek"><code>file:seek</code></a>, etc.).
58285931
58295932
58305933
@@ -5857,6 +5960,22 @@ in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code> · 26 unmodified lines
58575960
58585961
58595962
5963+<hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
5964+<span class="apii">[-0, +1, <em>m</em>]</span>
5965+<pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre>
5966+
5967+<p>
5968+Ensures that the value <code>t[fname]</code>,
5969+where <code>t</code> is the value at the valid index <code>idx</code>,
5970+is a table,
5971+and pushes that table onto the stack.
5972+Returns true if it finds a previous table there
5973+and false if it creates a new table.
5974+
5975+
5976+
5977+
5978+
58605979 <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
58615980 <span class="apii">[-0, +1, <em>m</em>]</span>
58625981 <pre>const char *luaL_gsub (lua_State *L,
@@ -5965,13 +6084,27 @@ it does not run it. · 102 unmodified lines
59656084 <pre>int luaL_newlib (lua_State *L, const luaL_Reg *l);</pre>
59666085
59676086 <p>
5968−Equivalent to <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> with no upvalues,
5969−but first creates a new table wherein to register the functions
5970−in list <code>l</code>.
6087+Creates a new table and registers there
6088+the functions in list <code>l</code>.
6089+It is implemented as the following macro:
6090+
6091+<pre>
6092+ (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
6093+</pre>
6094+
6095+
6096+
59716097
6098+<hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
6099+<span class="apii">[-0, +1, <em>m</em>]</span>
6100+<pre>int luaL_newlibtable (lua_State *L, const luaL_Reg *l);</pre>
59726101
59736102 <p>
5974−Leaves the new table on the stack.
6103+Creates a new table with a size optimized
6104+to store all entries in the array <code>l</code>
6105+(but does not actually store them).
6106+It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>
6107+(see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
59756108
59766109
59776110 <p>
@@ -6176,7 +6309,7 @@ Returns an address to a space of size <code>sz</code> · 198 unmodified lines
61766309 where you can copy a string to be added to buffer <code>B</code>
61776310 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
61786311 After copying the string into this space you must call
6179−<a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
6312+<a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
61806313 it to the buffer.
61816314
61826315
@@ -6284,8 +6417,6 @@ Leaves a copy of that result on the stack. · 101 unmodified lines
62846417 Registers all functions in the array <code>l</code>
62856418 (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
62866419 (below optional upvalues, see next).
6287−The pointer <code>l</code> may be <code>NULL</code>,
6288−representing an empty list.
62896420
62906421
62916422 <p>
@@ -6495,7 +6626,7 @@ e.g., by using <a href="#lua_call"><code>lua_call</code></a>. · 203 unmodified lines
64956626 <p>
64966627 The basic library provides some core functions to Lua.
64976628 If you do not include this library in your application,
6498−you should check carefully whether you need to provide
6629+you should check carefully whether you need to provide
64996630 implementations for some of its facilities.
65006631
65016632
@@ -6526,11 +6657,13 @@ This is the default option. · 24 unmodified lines
65266657 </li>
65276658
65286659 <li><b>"stop":</b>
6529−stops the garbage collector.
6660+stops automatic invocation of the garbage collector.
6661+The collector will run only when explcitly invoked,
6662+until a call to restart it.
65306663 </li>
65316664
65326665 <li><b>"restart":</b>
6533−restarts the garbage collector.
6666+restarts automatic invocation the garbage collector.
65346667 </li>
65356668
65366669 <li><b>"count":</b>
@@ -6543,6 +6676,8 @@ so the following equality is always true: · 6 unmodified lines
65436676 k, b = collectgarbage("count")
65446677 assert(k*1024 == math.floor(k)*1024 + b)
65456678 </pre><p>
6679+(The second result is useful when Lua is compiled
6680+with a non floating-point type for numbers.)
65466681 </li>
65476682
65486683 <li><b>"step":</b>
@@ -6658,7 +6793,7 @@ up to the first integer key absent from the table. · 109 unmodified lines
66586793
66596794
66606795 <p>
6661−<hr><h3><a name="pdf-load"><code>load (ld [, source [, mode]])</code></a></h3>
6796+<hr><h3><a name="pdf-load"><code>load (ld [, source [, mode [, env]]])</code></a></h3>
66626797
66636798
66646799 <p>
@@ -6666,32 +6801,35 @@ Loads a chunk. · 1 unmodified line
66666801
66676802
66686803 <p>
6804+If <code>ld</code> is a string, the chunk is this string.
66696805 If <code>ld</code> is a function,
6670−calls it repeatedly to get the chunk pieces.
6806+<code>load</code> calls it repeatedly to get the chunk pieces.
66716807 Each call to <code>ld</code> must return a string that concatenates
66726808 with previous results.
66736809 A return of an empty string, <b>nil</b>, or no value signals the end of the chunk.
66746810
66756811
66766812 <p>
6677−If <code>ld</code> is a string, the chunk is this string.
6813+If there are no errors,
6814+returns the compiled chunk as a function;
6815+otherwise, returns <b>nil</b> plus the error message.
66786816
66796817
66806818 <p>
6681−If there are no errors,
6682−returns the compiled chunk as a function;
6683−otherwise, returns <b>nil</b> plus the error message.
66846819 If the resulting function has upvalues,
6685−the first upvalue is set to the value of the global environment
6820+the first upvalue is set to the value of the
6821+global environment or to <code>env</code>,
6822+if that parameter is given.
66866823 (When loading main chunks,
6687−this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).)
6824+the first upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).)
66886825
66896826
66906827 <p>
66916828 <code>source</code> is used as the source of the chunk for error messages
66926829 and debug information (see <a href="#4.9">&sect;4.9</a>).
66936830 When absent,
6694−it defaults to "<code>=(load)</code>".
6831+it defaults to <code>ld</code>, if <code>ld</code> is a string,
6832+or to "<code>=(load)</code>".
66956833
66966834
66976835 <p>
@@ -6704,21 +6842,6 @@ The default is "<code>bt</code>". · 6 unmodified lines
67046842
67056843
67066844
6707−<p>
6708−<hr><h3><a name="pdf-loadin"><code>loadin (env, ...)</code></a></h3>
6709−
6710−
6711−<p>
6712−This function is similar to <a href="#pdf-load"><code>load</code></a>,
6713−but sets <code>env</code> as the value of the first upvalue
6714−of the created function in case of success.
6715−(When loading main chunks,
6716−this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).)
6717−The parameters after <code>env</code> are similar to those of <a href="#pdf-load"><code>load</code></a>, too.
6718−
6719−
6720−
6721−
67226845 <p>
67236846 <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename])</code></a></h3>
67246847
@@ -6732,29 +6855,6 @@ if no file name is given. · 7 unmodified lines
67326855
67336856
67346857
6735−<p>
6736−<hr><h3><a name="pdf-loadstring"><code>loadstring (string [, chunkname])</code></a></h3>
6737−
6738−
6739−<p>
6740−Similar to <a href="#pdf-load"><code>load</code></a>,
6741−but gets the chunk from the given string.
6742−
6743−
6744−<p>
6745−To load and run a given string, use the idiom
6746−
6747−<pre>
6748− assert(loadstring(s))()
6749−</pre>
6750−
6751−<p>
6752−When absent,
6753−<code>chunkname</code> defaults to the given string.
6754−
6755−
6756−
6757−
67586858 <p>
67596859 <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3>
67606860
@@ -6784,7 +6884,7 @@ use a numerical <b>for</b>.) · 23 unmodified lines
67846884
67856885
67866886 <p>
6787−The behavior of <code>next</code> is <em>undefined</em> if,
6887+The behavior of <code>next</code> is undefined if,
67886888 during the traversal,
67896889 you assign any value to a non-existent field in the table.
67906890 You may however modify existing fields.
@@ -6872,6 +6972,16 @@ without invoking any metamethod. · 81 unmodified lines
68726972
68736973
68746974
6975+<p>
6976+<hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3>
6977+Returns the length of the object <code>v</code>,
6978+which must be a table or a string,
6979+without invoking any metamethod.
6980+Returns an integer number.
6981+
6982+
6983+
6984+
68756985 <p>
68766986 <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3>
68776987 Sets the real value of <code>table[index]</code> to <code>value</code>,
@@ -6934,9 +7044,9 @@ The base may be any integer between 2 and 36, inclusive. · 56 unmodified lines
69347044 In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)
69357045 represents&nbsp;10, '<code>B</code>' represents&nbsp;11, and so forth,
69367046 with '<code>Z</code>' representing 35.
6937−In base 10 (the default), the number can have a decimal part,
6938−as well as an optional exponent part (see <a href="#3.1">&sect;3.1</a>).
6939−In other bases, only unsigned integers are accepted.
7047+In base 10 (the default),
7048+the numeral is converted following the coercion rules (see <a href="#3.4.2">&sect;3.4.2</a>).
7049+In other bases, only integers are accepted.
69407050
69417051
69427052
@@ -6984,12 +7094,12 @@ The current contents of this variable is "<code>Lua 5.2</code>". · 41 unmodified lines
69847094
69857095
69867096 <p>
6987−<hr><h3><a name="pdf-xpcall"><code>xpcall (f, err [, arg1, &middot;&middot;&middot;])</code></a></h3>
7097+<hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, &middot;&middot;&middot;])</code></a></h3>
69887098
69897099
69907100 <p>
69917101 This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>,
6992−except that it sets a new error handler <code>err</code>.
7102+except that it sets a new message handler <code>msgh</code>.
69937103
69947104
69957105
@@ -7130,8 +7240,8 @@ Otherwise, it tries to find a <em>loader</em> for the module. · 134 unmodified lines
71307240
71317241 <p>
71327242 To find a loader,
7133−<code>require</code> is guided by the <a href="#pdf-package.loaders"><code>package.loaders</code></a> array.
7134−By changing this array,
7243+<code>require</code> is guided by the <a href="#pdf-package.loaders"><code>package.loaders</code></a> sequence.
7244+By changing this sequence,
71357245 we can change how <code>require</code> looks for a module.
71367246 The following explanation is based on the default configuration
71377247 for <a href="#pdf-package.loaders"><code>package.loaders</code></a>.
@@ -7151,7 +7261,10 @@ it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.loaders"><code · 13 unmodified lines
71517261
71527262 <p>
71537263 Once a loader is found,
7154−<code>require</code> calls the loader with a single argument, <code>modname</code>.
7264+<code>require</code> calls the loader with two arguments:
7265+<code>modname</code> and an extra value dependent on how it got the loader.
7266+(If the loader came from a file,
7267+this extra value is the file name.)
71557268 If the loader returns any non-nil value,
71567269 <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.
71577270 If the loader does not return a non-nil value and
@@ -7164,7 +7277,7 @@ final value of <code>package.loaded[modname]</code>. · 6 unmodified lines
71647277 <p>
71657278 If there is any error loading or running the module,
71667279 or if it cannot find any loader for the module,
7167−then <code>require</code> signals an error.
7280+then <code>require</code> signals an error.
71687281
71697282
71707283
@@ -7255,9 +7368,13 @@ When looking for a module, · 84 unmodified lines
72557368 with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
72567369 sole parameter.
72577370 The function can return another function (the module <em>loader</em>)
7371+plus an extra value that will be passed to that loader,
72587372 or a string explaining why it did not find that module
72597373 (or <b>nil</b> if it has nothing to say).
7260−Lua initializes this table with four functions.
7374+
7375+
7376+<p>
7377+Lua initializes this table with four searcher functions.
72617378
72627379
72637380 <p>
@@ -7313,6 +7430,13 @@ into one single library, · 49 unmodified lines
73137430 with each submodule keeping its original open function.
73147431
73157432
7433+<p>
7434+All searchers except the first (preload) return as the extra value
7435+the file name where the module was found,
7436+as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
7437+The first searcher returns no extra value.
7438+
7439+
73167440
73177441
73187442 <p>
@@ -7384,10 +7508,16 @@ A table to store loaders for specific modules · 65 unmodified lines
73847508 (see <a href="#pdf-require"><code>require</code></a>).
73857509
73867510
7511+<p>
7512+This variable is only a reference to the real table;
7513+assignments to this variable do not change the
7514+table used by <a href="#pdf-require"><code>require</code></a>.
7515+
7516+
73877517
73887518
73897519 <p>
7390−<hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path)</code></a></h3>
7520+<hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep])</code></a></h3>
73917521
73927522
73937523 <p>
@@ -7400,8 +7530,15 @@ A path is string containing a sequence of · 6 unmodified lines
74007530 For each template,
74017531 the function changes each interrogation
74027532 mark in the template by a copy of <code>name</code>
7403−wherein all dots were replaced by the system's directory separator,
7533+wherein all occurrences of <code>sep</code>
7534+(a dot, by default)
7535+were replaced by the system's directory separator,
74047536 and then tries to open the resulting file name.
7537+If <code>sep</code> is the empty string,
7538+the replacement is not done.
7539+
7540+
7541+<p>
74057542 For instance, if the path is the string
74067543
74077544 <pre>
@@ -7485,9 +7622,8 @@ Note that numerical codes are not necessarily portable across platforms. · 77 unmodified lines
74857622
74867623 <p>
74877624 Returns a string containing a binary representation of the given function,
7488−so that a later <a href="#pdf-loadstring"><code>loadstring</code></a> on this string returns
7489−a copy of the function.
7490−<code>function</code> must be a Lua function without upvalues.
7625+so that a later <a href="#pdf-load"><code>load</code></a> on this string returns
7626+a copy of the function (but with new upvalues).
74917627
74927628
74937629
@@ -7552,16 +7688,19 @@ will produce the string: · 58 unmodified lines
75527688 </pre>
75537689
75547690 <p>
7555−The options <code>c</code>, <code>d</code>, <code>E</code>, <code>e</code>, <code>f</code>,
7556−<code>g</code>, <code>G</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code> all
7557−expect a number as argument,
7558−whereas <code>q</code> and <code>s</code> expect a string.
7559−
7560−
7561−<p>
7562−This function does not accept string values
7563−containing embedded zeros,
7564−except as arguments to the <code>q</code> option.
7691+Options
7692+<code>A</code> and <code>a</code> (when available),
7693+<code>E</code>, <code>e</code>, <code>f</code>,
7694+<code>G</code>, and <code>g</code> all expect a number as argument.
7695+Options <code>c</code>, <code>d</code>,
7696+<code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>
7697+expect an integer as argument;
7698+the range of that integer may be limited by
7699+the underlying C&nbsp;implementation.
7700+Option <code>q</code> expects a string
7701+and <code>s</code> expects a string without embedded zeros.
7702+If the argument to option <code>s</code> is not a string,
7703+it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>.
75657704
75667705
75677706
@@ -7577,6 +7716,8 @@ then the whole match is produced in each call. · 9 unmodified lines
75777716
75787717 <p>
75797718 As an example, the following loop
7719+will iterate over all the words from string <code>s</code>,
7720+printing one per line:
75807721
75817722 <pre>
75827723 s = "hello world from Lua"
@@ -7584,8 +7725,6 @@ As an example, the following loop · 1 unmodified line
75847725 print(w)
75857726 end
75867727 </pre><p>
7587−will iterate over all the words from string <code>s</code>,
7588−printing one per line.
75897728 The next example collects all pairs <code>key=value</code> from the
75907729 given string into a table:
75917730
@@ -7667,7 +7806,7 @@ Here are some examples: · 75 unmodified lines
76677806 --&gt; x="home = /home/roberto, user = roberto"
76687807
76697808 x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
7670− return loadstring(s)()
7809+ return load(s)()
76717810 end)
76727811 --&gt; x="4+5 = 9"
76737812
@@ -7715,9 +7854,11 @@ its default value is&nbsp;1 and can be negative. · 41 unmodified lines
77157854
77167855
77177856 <p>
7718−<hr><h3><a name="pdf-string.rep"><code>string.rep (s, n)</code></a></h3>
7857+<hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
77197858 Returns a string that is the concatenation of <code>n</code> copies of
7720−the string <code>s</code>.
7859+the string <code>s</code> separated by the string <code>sep</code>.
7860+The default value for <code>sep</code> is the empty string
7861+(that is, no separator).
77217862
77227863
77237864
@@ -7867,7 +8008,7 @@ These repetition items will always match the longest possible sequence; · 143 unmodified lines
78678008 a single character class followed by '<code>-</code>',
78688009 which also matches 0 or more repetitions of characters in the class.
78698010 Unlike '<code>*</code>',
7870−these repetition items will always match the <em>shortest</em> possible sequence;
8011+these repetition items will always match the shortest possible sequence;
78718012 </li>
78728013
78738014 <li>
@@ -7949,41 +8090,55 @@ string <code>"flaaap"</code>, there will be two captures: 3&nbsp;and&nbsp;5. · 75 unmodified lines
79498090
79508091
79518092
7952−<h2>6.5 &ndash; <a name="6.5">Table Manipulation</a></h2><p>
8093+<h2>6.5 &ndash; <a name="6.5">Table Manipulation</a></h2>
8094+
8095+<p>
79538096 This library provides generic functions for table manipulation.
79548097 It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>.
79558098
79568099
79578100 <p>
7958−Most functions in the table library assume that the table
7959−represents an array or a list.
7960−For these functions, when we talk about the "length" of a table
7961−we mean the result of the length operator.
8101+Currently, all functions in the table library assume that the table
8102+represents a list.
8103+In particular, they all ignore non-numeric keys
8104+in tables given as arguments.
8105+All functions in the table library,
8106+except <a href="#pdf-table.pack"><code>table.pack</code></a>,
8107+may apply the length operator on the list.
8108+In that case, the list must be a proper sequence
8109+or have a <code>__len</code> metamethod (see <a href="#3.4.6">&sect;3.4.6</a>).
8110+
8111+
8112+<p>
8113+For performance reasons,
8114+all table accesses (get/set) performed by these functions are raw.
8115+
8116+
8117+<p>
8118+<hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3>
79628119
79638120
79648121 <p>
7965−<hr><h3><a name="pdf-table.concat"><code>table.concat (table [, sep [, i [, j]]])</code></a></h3>
7966−Given an array where all elements are strings or numbers,
7967−returns <code>table[i]..sep..table[i+1] &middot;&middot;&middot; sep..table[j]</code>.
8122+Given a list where all elements are strings or numbers,
8123+returns <code>list[i]..sep..list[i+1] &middot;&middot;&middot; sep..list[j]</code>.
79688124 The default value for <code>sep</code> is the empty string,
79698125 the default for <code>i</code> is 1,
7970−and the default for <code>j</code> is the length of the table.
8126+and the default for <code>j</code> is <code>#list</code>.
79718127 If <code>i</code> is greater than <code>j</code>, returns the empty string.
79728128
79738129
79748130
79758131
79768132 <p>
7977−<hr><h3><a name="pdf-table.insert"><code>table.insert (table, [pos,] value)</code></a></h3>
8133+<hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3>
79788134
79798135
79808136 <p>
7981−Inserts element <code>value</code> at position <code>pos</code> in <code>table</code>,
8137+Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>,
79828138 shifting up other elements to open space, if necessary.
7983−The default value for <code>pos</code> is <code>n+1</code>,
7984−where <code>n</code> is the length of the table (see <a href="#3.4.6">&sect;3.4.6</a>),
8139+The default value for <code>pos</code> is <code>#list+1</code>,
79858140 so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end
7986−of table <code>t</code>.
8141+of list <code>t</code>.
79878142
79888143
79898144
@@ -7993,36 +8148,38 @@ of table <code>t</code>. · 3 unmodified lines
79938148
79948149
79958150 <p>
7996−Returns a new table with all parameters stored into
7997−keys 1, 2, etc.
8151+Returns a new table with all parameters stored into keys 1, 2, etc.
79988152 and with a field "<code>n</code>" with the total number of parameters.
8153+Note that the result may not be a sequence,
8154+if any parameter is <b>nil</b>.
79998155
80008156
80018157
80028158
80038159 <p>
8004−<hr><h3><a name="pdf-table.remove"><code>table.remove (table [, pos])</code></a></h3>
8160+<hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3>
80058161
80068162
80078163 <p>
8008−Removes from <code>table</code> the element at position <code>pos</code>,
8164+Removes from <code>list</code> the element at position <code>pos</code>,
80098165 shifting down other elements to close the space, if necessary.
80108166 Returns the value of the removed element.
8011−The default value for <code>pos</code> is <code>n</code>,
8012−where <code>n</code> is the length of the table,
8167+The default value for <code>pos</code> is <code>#list</code>,
80138168 so that a call <code>table.remove(t)</code> removes the last element
8014−of table <code>t</code>.
8169+of list <code>t</code>.
80158170
80168171
80178172
80188173
80198174 <p>
8020−<hr><h3><a name="pdf-table.sort"><code>table.sort (table [, comp])</code></a></h3>
8021−Sorts table elements in a given order, <em>in-place</em>,
8022−from <code>table[1]</code> to <code>table[n]</code>,
8023−where <code>n</code> is the length of the table.
8175+<hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3>
8176+
8177+
8178+<p>
8179+Sorts list elements in a given order, <em>in-place</em>,
8180+from <code>table[1]</code> to <code>table[#list]</code>.
80248181 If <code>comp</code> is given,
8025−then it must be a function that receives two table elements
8182+then it must be a function that receives two list elements
80268183 and returns true when the first element must come
80278184 before the second in the final order
80288185 (so that <code>not comp(a[i+1],a[i])</code> will be true after the sort).
@@ -8040,16 +8197,16 @@ may have their relative positions changed by the sort. · 11 unmodified lines
80408197
80418198 <p>
80428199 <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3>
8200+
8201+
8202+<p>
80438203 Returns the elements from the given table.
80448204 This function is equivalent to
80458205
80468206 <pre>
80478207 return list[i], list[i+1], &middot;&middot;&middot;, list[j]
80488208 </pre><p>
8049−except that the above code can be written only for a fixed number
8050−of elements.
8051−By default, <code>i</code> is&nbsp;1 and <code>j</code> is the length of the list,
8052−as defined by the length operator (see <a href="#3.4.6">&sect;3.4.6</a>).
8209+By default, <code>i</code> is&nbsp;1 and <code>j</code> is <code>#list</code>.
80538210
80548211
80558212
@@ -8386,7 +8543,7 @@ Returns the hyperbolic tangent of <code>x</code>. · 330 unmodified lines
83868543
83878544
83888545
8389−<h2>6.7 &ndash; <a name="6.7">Bitwise operations</a></h2>
8546+<h2>6.7 &ndash; <a name="6.7">Bitwise Operations</a></h2>
83908547
83918548 <p>
83928549 This library provides bitwise operations.
@@ -8468,7 +8625,7 @@ Returns the bitwise or of its operands. · 75 unmodified lines
84688625
84698626
84708627 <p>
8471−Returns a boolean signaling
8628+Returns a boolean signaling
84728629 whether the bitwise and of its operands is different from zero.
84738630
84748631
@@ -8484,6 +8641,32 @@ Returns the bitwise exclusive or of its operands. · 9 unmodified lines
84848641
84858642
84868643
8644+<p>
8645+<hr><h3><a name="pdf-bit32.extract"><code>bit32.extract (n, field, width)</code></a></h3>
8646+
8647+
8648+<p>
8649+Returns the unsigned number formed by the bits
8650+<code>field</code> to <code>field + width - 1</code> from <code>n</code>.
8651+Bits are numbered from 0 (least significant) to 31 (most significant).
8652+All accessed bits must be in the range <em>[0, 31]</em>.
8653+
8654+
8655+
8656+
8657+<p>
8658+<hr><h3><a name="pdf-bit32.replace"><code>bit32.replace (n, v, field, width)</code></a></h3>
8659+
8660+
8661+<p>
8662+Returns a copy of <code>n</code> with
8663+the bits <code>field</code> to <code>field + width - 1</code>
8664+replaced by the value <code>v</code>.
8665+See <a href="#pdf-bit32.extract"><code>bit32.extract</code></a> for details about <code>field</code> and <code>width</code>.
8666+
8667+
8668+
8669+
84878670 <p>
84888671 <hr><h3><a name="pdf-bit32.lrotate"><code>bit32.lrotate (x, disp)</code></a></h3>
84898672
@@ -8722,6 +8905,11 @@ Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over · 232 unmodified lines
87228905 <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
87238906
87248907
8908+<p>
8909+This function is system dependent and is not available
8910+on all platforms.
8911+
8912+
87258913 <p>
87268914 Starts program <code>prog</code> in a separated process and returns
87278915 a file handle that you can use to read data from this program
@@ -8730,11 +8918,6 @@ or to write data to this program · 2 unmodified lines
87308918 (if <code>mode</code> is <code>"w"</code>).
87318919
87328920
8733−<p>
8734−This function is system dependent and is not available
8735−on all platforms.
8736−
8737−
87388921
87398922
87408923 <p>
@@ -8794,8 +8977,9 @@ but that takes an unpredictable amount of time to happen. · 53 unmodified lines
87948977
87958978
87968979 <p>
8797−If <code>file</code> was created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
8798−a successful close returns the exit status of the process.
8980+When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
8981+<a href="#pdf-file:close"><code>file:close</code></a> returns the same values
8982+returned by <a href="#pdf-os.execute"><code>os.execute</code></a>.
87998983
88008984
88018985
@@ -9020,6 +9204,8 @@ then <code>date</code> returns a table with the following fields: · 218 unmodified lines
90209204 <code>wday</code> (weekday, Sunday is&nbsp;1),
90219205 <code>yday</code> (day of the year),
90229206 and <code>isdst</code> (daylight saving flag, a boolean).
9207+This last field may be absent
9208+if the information is not available.
90239209
90249210
90259211 <p>
@@ -9056,9 +9242,28 @@ this value is exactly <code>t2</code><em>-</em><code>t1</code>. · 30 unmodified lines
90569242 <p>
90579243 This function is equivalent to the C&nbsp;function <code>system</code>.
90589244 It passes <code>command</code> to be executed by an operating system shell.
9059−It returns a status code, which is system-dependent.
9060−If <code>command</code> is absent, then it returns nonzero if a shell is available
9061−and zero otherwise.
9245+It returns <b>true</b>
9246+if the command terminated successfully.
9247+Otherwise, it returns <b>nil</b> plus a string and a number,
9248+as follows:
9249+
9250+<ul>
9251+
9252+<li><b>"<code>exit</code>":</b>
9253+the command terminated normally;
9254+the following number is the exit status of the command.
9255+</li>
9256+
9257+<li><b>"<code>signal</code>":</b>
9258+the command was terminated by a signal;
9259+the following number is the signal that terminated the command.
9260+</li>
9261+
9262+</ul>
9263+
9264+<p>
9265+When called without a <code>command</code>,
9266+<code>os.execute</code> returns a boolean that is true if a shell is available.
90629267
90639268
90649269
@@ -9072,10 +9277,10 @@ Calls the C&nbsp;function <code>exit</code> to terminate the host program. · 7 unmodified lines
90729277 If <code>code</code> is <b>true</b>,
90739278 the returned status is <code>EXIT_SUCCESS</code>;
90749279 if <code>code</code> is <b>false</b>,
9075−the returned status is <code>EXIT_FAILURE</code>.
9280+the returned status is <code>EXIT_FAILURE</code>;
90769281 if <code>code</code> is a number,
90779282 the returned status is this number.
9078−The default value for <code>code</code> is the success code.
9283+The default value for <code>code</code> is <b>true</b>.
90799284
90809285
90819286 <p>
@@ -9101,8 +9306,8 @@ or <b>nil</b> if the variable is not defined. · 19 unmodified lines
91019306
91029307
91039308 <p>
9104−Deletes the file or directory with the given name.
9105−Directories must be empty to be removed.
9309+Deletes the file (or empty directory, on POSIX systems)
9310+with the given name.
91069311 If this function fails, it returns <b>nil</b>,
91079312 plus a string describing the error.
91089313
@@ -9165,7 +9370,8 @@ and may have fields <code>hour</code>, <code>min</code>, <code>sec</code>, and < · 56 unmodified lines
91659370
91669371 <p>
91679372 The returned value is a number, whose meaning depends on your system.
9168−In POSIX, Windows, and some other systems, this number counts the number
9373+In POSIX, Windows, and some other systems,
9374+this number counts the number
91699375 of seconds since some given start time (the "epoch").
91709376 In other systems, the meaning is not specified,
91719377 and the number returned by <code>time</code> can be used only as an argument to
@@ -9186,7 +9392,7 @@ and explicitly removed when no longer needed. · 14 unmodified lines
91869392
91879393
91889394 <p>
9189−On some systems (POSIX),
9395+On POSIX systems,
91909396 this function also creates a file with that name,
91919397 to avoid security risks.
91929398 (Someone else might create the file with wrong permissions
@@ -9327,10 +9533,16 @@ about the <a href="#pdf-print"><code>print</code></a> function. · 134 unmodified lines
93279533 <p>
93289534 This function returns the name and the value of the local variable
93299535 with index <code>local</code> of the function at level <code>f</code> of the stack.
9330−(The first parameter or local variable has index&nbsp;1, and so on,
9331−until the last active local variable.)
9332−The function returns <b>nil</b> if there is no local
9333−variable with the given index,
9536+This function accesses not only explicit local variables,
9537+but also parameters, temporaries, etc.
9538+
9539+
9540+<p>
9541+The first parameter or local variable has index&nbsp;1, and so on,
9542+until the last active variable.
9543+Negative indices refer to vararg parameters;
9544+-1 is the first vararg parameter.
9545+The function returns <b>nil</b> if there is no variable with the given index,
93349546 and raises an error when called with a level out of range.
93359547 (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.)
93369548
@@ -9338,7 +9550,7 @@ and raises an error when called with a level out of range. · 1 unmodified line
93389550 <p>
93399551 Variable names starting with '<code>(</code>' (open parentheses)
93409552 represent internal variables
9341−(loop control variables, temporaries, and C&nbsp;function locals).
9553+(loop control variables, temporaries, varargs, and C&nbsp;function locals).
93429554
93439555
93449556 <p>
@@ -9454,6 +9666,11 @@ and raises an error when called with a <code>level</code> out of range. · 109 unmodified lines
94549666 Otherwise, it returns the name of the local variable.
94559667
94569668
9669+<p>
9670+See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about
9671+variable indices and names.
9672+
9673+
94579674
94589675
94599676 <p>
@@ -9463,6 +9680,7 @@ Otherwise, it returns the name of the local variable. · 3 unmodified lines
94639680 <p>
94649681 Sets the metatable for the given <code>object</code> to the given <code>table</code>
94659682 (which can be <b>nil</b>).
9683+Returns <code>object</code>.
94669684
94679685
94689686
@@ -9482,14 +9700,14 @@ Otherwise, it returns the name of the upvalue. · 13 unmodified lines
94829700
94839701
94849702 <p>
9485−<hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message] [, level])</code></a></h3>
9703+<hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>
94869704
94879705
94889706 <p>
9489−If <code>message</code> is present but is not a string,
9707+If <code>message</code> is present but is neither a string nor <b>nil</b>,
94909708 this function returns <code>message</code> without further processing.
94919709 Otherwise,
9492−returns a string with a traceback of the call stack.
9710+it returns a string with a traceback of the call stack.
94939711 An optional <code>message</code> string is appended
94949712 at the beginning of the traceback.
94959713 An optional <code>level</code> number tells at which level
@@ -9635,6 +9853,14 @@ Finally, if the error object is <b>nil</b>, · 139 unmodified lines
96359853 the interpreter does not report the error.
96369854
96379855
9856+<p>
9857+When finishing normally,
9858+the interpreter closes its main Lua state
9859+(see <a href="#lua_close"><code>lua_close</code></a>).
9860+The script can avoid this step by terminating
9861+through <a href="#pdf-os.exit"><code>os.exit</code></a>.
9862+
9863+
96389864 <p>
96399865 If the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string,
96409866 then its value is used as the prompt.
@@ -9671,12 +9897,12 @@ as in · 30 unmodified lines
96719897 (Of course,
96729898 the location of the Lua interpreter can be different in your machine.
96739899 If <code>lua</code> is in your <code>PATH</code>,
9674−then
9900+then
96759901
96769902 <pre>
96779903 #!/usr/bin/env lua
96789904 </pre><p>
9679−is a more portable solution.)
9905+is a more portable solution.)
96809906
96819907
96829908
@@ -9696,21 +9922,38 @@ all these compatibility options will be removed in the next version of Lua. · 13 unmodified lines
96969922 <ul>
96979923
96989924 <li>
9699−Lua identifiers cannot use locale-dependent letters.
9925+The concept of <em>environment</em> changed.
9926+Only Lua functions have environments.
9927+To set the environment of a Lua function,
9928+use the variable <code>_ENV</code> or the function <a href="#pdf-load"><code>load</code></a>.
9929+
9930+
9931+<p>
9932+C functions do not have environments any more.
9933+Use an upvalue with a shared table if you need to keep
9934+shared state among several C functions.
9935+(You may use <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> to open a C library
9936+with all functions sharing a common upvalue.)
9937+
9938+
9939+<p>
9940+To manipulate the "environment" of a userdata
9941+(which is now called user value),
9942+use the new functions
9943+<a href="#lua_getuservalue"><code>lua_getuservalue</code></a> and <a href="#lua_setuservalue"><code>lua_setuservalue</code></a>.
97009944 </li>
97019945
97029946 <li>
9703−Doing a step in the garbage collector does not restart the collector
9704−if it has been stopped.
9947+Lua identifiers cannot use locale-dependent letters.
97059948 </li>
97069949
97079950 <li>
9708−Weak tables with weak keys now perform like <em>ephemeron tables</em>.
9951+Doing a step or a full collection in the garbage collector
9952+does not restart the collector if it has been stopped.
97099953 </li>
97109954
97119955 <li>
9712−Threads do not have individual environments.
9713−All threads share a single fixed environment.
9956+Weak tables with weak keys now perform like <em>ephemeron tables</em>.
97149957 </li>
97159958
97169959 <li>
@@ -9720,6 +9963,13 @@ Instead, tail calls generate a special new event, · 3 unmodified lines
97209963 not be a corresponding return event.
97219964 </li>
97229965
9966+<li>
9967+Equality between function values has changed.
9968+Now, a function definition may not create a new value;
9969+it may reuse some previous value if there is no
9970+observable difference to the new function.
9971+</li>
9972+
97239973 </ul>
97249974
97259975
@@ -9735,9 +9985,8 @@ and it is easy to set up a module with regular Lua code. · 9 unmodified lines
97359985 </li>
97369986
97379987 <li>
9738−Functions <code>setfenv</code> and <code>getfenv</code> are deprecated.
9739−To set the environment of a Lua function,
9740−use the variable <code>_ENV</code> or the new function <a href="#pdf-loadin"><code>loadin</code></a>.
9988+Functions <code>setfenv</code> and <code>getfenv</code> are deprecated,
9989+because of the changes in environments.
97419990 </li>
97429991
97439992 <li>
@@ -9745,11 +9994,23 @@ Function <code>math.log10</code> is deprecated. · 1 unmodified line
97459994 Use <a href="#pdf-math.log"><code>math.log</code></a> with 10 as its second argument, instead.
97469995 </li>
97479996
9997+<li>
9998+Function <code>loadstring</code> is deprecated.
9999+Use <code>load</code> instead; it now accepts string arguments
10000+and are exactly equivalent to <code>loadstring</code>.
10001+</li>
10002+
974810003 <li>
974910004 Function <code>table.maxn</code> is deprecated.
975010005 Write it in Lua if you really need it.
975110006 </li>
975210007
10008+<li>
10009+Function <code>os.execute</code> now returns <b>true</b> when command
10010+terminates successfully and <b>nil</b> plus error information
10011+otherwise.
10012+</li>
10013+
975310014 <li>
975410015 Function <code>unpack</code> was moved into the table library
975510016 and therefore must be called as <a href="#pdf-table.unpack"><code>table.unpack</code></a>.
@@ -9763,7 +10024,7 @@ as now patterns may contain '<code>\0</code>' as a regular character. · 7 unmodified lines
976310024 <li>
976410025 Lua does not have bytecode verification anymore.
976510026 So, all functions that load code
9766−(<a href="#pdf-load"><code>load</code></a>, <a href="#pdf-loadfile"><code>loadfile</code></a>, <a href="#pdf-loadstring"><code>loadstring</code></a>)
10027+(<a href="#pdf-load"><code>load</code></a> and <a href="#pdf-loadfile"><code>loadfile</code></a>)
976710028 are potentially insecure when loading untrusted binary data.
976810029 (Actually, those functions were already insecure because
976910030 of bugs in the verification algorithm.)
@@ -9789,19 +10050,8 @@ You must get the global environment from the registry · 19 unmodified lines
978910050 <li>
979010051 Pseudoindex <code>LUA_ENVIRONINDEX</code>
979110052 and functions <code>lua_getfenv</code>/<code>lua_setfenv</code>
9792−were removed.
9793−C functions do not have environments any more.
9794−Use an upvalue with a shared table if you need to keep
9795−shared state among several C functions.
9796−(You may use <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> to open a C library
9797−with all functions sharing a common upvalue.)
9798−
9799−
9800−<p>
9801−To manipulate the "environment" of a userdata
9802−(which is not called "environment" anymore),
9803−use the new functions
9804−<a href="#lua_getuservalue"><code>lua_getuservalue</code></a> and <a href="#lua_setuservalue"><code>lua_setuservalue</code></a>.
10053+were removed,
10054+as C&nbsp;functions do not have environments any more.
980510055 </li>
980610056
980710057 <li>
@@ -9812,12 +10062,21 @@ create globals anymore. · 4 unmodified lines
981210062 </li>
981310063
981410064 <li>
9815−Finalizers ("<code>__gc</code>" metamethods) for userdata are called in the
10065+The <code>osize</code> argument to the allocation function
10066+may not be zero when creating a new block,
10067+that is, when <code>ptr</code> is <code>NULL</code>
10068+(see <a href="#lua_Alloc"><code>lua_Alloc</code></a>).
10069+Use only the test <code>ptr == NULL</code> to check whether
10070+the block is new.
10071+</li>
10072+
10073+<li>
10074+Finalizers (<code>__gc</code> metamethods) for userdata are called in the
981610075 reverse order that they were marked,
981710076 not that they were created (see <a href="#2.5.1">&sect;2.5.1</a>).
981810077 (Most userdata are marked immediately after they are created.)
981910078 Moreover,
9820−if the metatable does not have a "<code>__gc</code>" field when set,
10079+if the metatable does not have a <code>__gc</code> field when set,
982110080 the finalizer will not be called,
982210081 even if it is set later.
982310082 </li>
@@ -9860,11 +10119,14 @@ Here is the complete syntax of Lua in extended BNF. · 36 unmodified lines
986010119
986110120 chunk ::= block
986210121
9863− block ::= {stat} [laststat [&lsquo;<b>;</b>&rsquo;]]
10122+ block ::= {stat} [retstat]
986410123
986510124 stat ::= &lsquo;<b>;</b>&rsquo; |
986610125 varlist &lsquo;<b>=</b>&rsquo; explist |
986710126 functioncall |
10127+ label |
10128+ <b>break</b> |
10129+ <b>goto</b> Name |
986810130 <b>do</b> block <b>end</b> |
986910131 <b>while</b> exp <b>do</b> block <b>end</b> |
987010132 <b>repeat</b> block <b>until</b> exp |
@@ -9875,7 +10137,9 @@ Here is the complete syntax of Lua in extended BNF. · 4 unmodified lines
987510137 <b>local</b> <b>function</b> Name funcbody |
987610138 <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
987710139
9878− laststat ::= <b>return</b> [explist] | <b>break</b>
10140+ retstat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
10141+
10142+ label ::= &lsquo;<b>@</b>&rsquo; Name &lsquo;<b>:</b>&rsquo;
987910143
988010144 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
988110145
@@ -9885,7 +10149,7 @@ Here is the complete syntax of Lua in extended BNF. · 3 unmodified lines
988510149
988610150 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
988710151
9888− explist ::= {exp &lsquo;<b>,</b>&rsquo;} exp
10152+ explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
988910153
989010154 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Number | String | &lsquo;<b>...</b>&rsquo; | function |
989110155 prefixexp | tableconstructor | exp binop exp | unop exp
@@ -9896,7 +10160,7 @@ Here is the complete syntax of Lua in extended BNF. · 4 unmodified lines
989610160
989710161 args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo; | tableconstructor | String
989810162
9899− function ::= <b>function</b> funcbody
10163+ functiondef ::= <b>function</b> funcbody
990010164
990110165 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
990210166
@@ -9929,10 +10193,10 @@ Here is the complete syntax of Lua in extended BNF. · 26 unmodified lines
992910193 <HR>
993010194 <SMALL>
993110195 Last update:
9932−Mon Nov 22 16:36:19 BRST 2010
10196+Mon Jun 13 14:56:14 BRT 2011
993310197 </SMALL>
993410198 <!--
9935−Last change: revised for Lua 5.2.0 (alpha)
10199+Last change: revised for Lua 5.2.0 (beta)
993610200 -->
993710201
993810202 </body></html>
doc/readme.html+16−9
@@ -27,12 +27,12 @@ tt, kbd, code { · 26 unmodified lines
2727 <HR>
2828 <H1>
2929 <A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua" BORDER=0></A>
30−Welcome to Lua 5.2 (alpha)
30+Welcome to Lua 5.2 (beta)
3131 </H1>
3232
3333 <P>
3434 <IMG SRC="alert.png" ALIGN="absbottom" ALT="[!]">
35−<EM>This is an alpha version of Lua 5.2.
35+<EM>This is a beta version of Lua 5.2.
3636 Some details may change in the final version.</EM>
3737
3838 <P>
@@ -278,11 +278,13 @@ lists the · 239 unmodified lines
278278 <H3>Main changes</H3>
279279 <UL>
280280 <LI> yieldable pcall and metamethods
281−<LI> new <CODE>_ENV</CODE> scheme
281+<LI> new lexical scheme for globals
282282 <LI> ephemeron tables
283283 <LI> new library for bitwise operations
284284 <LI> light C functions
285285 <LI> emergency garbage collector
286+<LI> <CODE>goto</CODE> statement
287+<LI> finalizers for tables
286288 </UL>
287289
288290 Here are the other changes introduced in Lua 5.2:
@@ -291,29 +293,34 @@ Here are the other changes introduced in Lua 5.2: · 2 unmodified lines
291293 <LI> no more fenv for threads or functions
292294 <LI> tables honor the <CODE>__len</CODE> metamethod
293295 <LI> hex and <CODE>\*</CODE> escapes in strings
296+<LI> support for hexadecimal floats
294297 <LI> order metamethods work for different types
295298 <LI> no more verification of opcode consistency
296299 <LI> hook event "tail return" replaced by "tail call"
297300 <LI> empty statement
301+<LI> <CODE>break</CODE> statement may appear in the middle of a block
298302 </UL>
299303
300304 <H3>Libraries</H3>
301305 <UL>
302−<LI> new metamethods <CODE>__pairs</CODE> and <CODE>__ipairs</CODE>
303306 <LI> arguments for function called through <CODE>xpcall</CODE>
304307 <LI> optional 'mode' argument to load (to control binary x text)
305−<LI> new function <CODE>loadin</CODE>
308+<LI> optional 'env' argument to load (environment for loaded chunk)
306309 <LI> <CODE>loadlib</CODE> may load libraries with global names (RTLD_GLOBAL)
307310 <LI> new function <CODE>package.searchpath</CODE>
311+<LI> modules receive their paths when loaded
308312 <LI> optional base in <CODE>math.log</CODE>
313+<LI> optional separator in <CODE>string.rep</CODE>
309314 <LI> <CODE>file:write</CODE> returns <CODE>file</CODE>
310315 <LI> closing a pipe returns exit status
311316 <LI> <CODE>os.exit</CODE> may close state
317+<LI> new metamethods <CODE>__pairs</CODE> and <CODE>__ipairs</CODE>
312318 <LI> new option 'isrunning' for <CODE>collectgarbage</CODE> and <CODE>lua_gc</CODE>
313319 <LI> frontier patterns
314320 <LI> <CODE>\0</CODE> in patterns
315321 <LI> new option <CODE>*L</CODE> for <CODE>io.read</CODE>
316322 <LI> options for <CODE>io.lines</CODE>
323+<LI> <CODE>debug.getlocal</CODE> can access function varargs
317324 </UL>
318325
319326 <H3>C API</H3>
@@ -321,7 +328,7 @@ Here are the other changes introduced in Lua 5.2: · 1 unmodified line
321328 <LI> main thread predefined in the registry
322329 <LI> new constants <CODE>LUA_OK</CODE> and <CODE>LUA_ERRGCMM</CODE>
323330 <LI> new <CODE>lua_compare</CODE>, <CODE>lua_arith</CODE>, and <CODE>lua_len</CODE>
324−<LI> new <CODE>lua_version</CODE> and <CODE>luaL_version</CODE>
331+<LI> new <CODE>lua_version</CODE> and <CODE>luaL_checkversion</CODE>
325332 <LI> <CODE>lua_pushstring</CODE> and <CODE>pushlstring</CODE> return string
326333 <LI> new <CODE>luaL_testudata</CODE> and <CODE>luaL_setmetatable</CODE>
327334 <LI> new <CODE>luaL_tolstring</CODE>
@@ -364,7 +371,7 @@ For details, see · 36 unmodified lines
364371 <A HREF="http://www.lua.org/license.html">this</A>.
365372
366373 <BLOCKQUOTE>
367−Copyright &copy; 1994&ndash;2010 Lua.org, PUC-Rio.
374+Copyright &copy; 1994&ndash;2011 Lua.org, PUC-Rio.
368375
369376 <P>
370377 Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -391,10 +398,10 @@ THE SOFTWARE. · 20 unmodified lines
391398 <HR>
392399 <SMALL>
393400 Last update:
394−Sat Nov 20 19:23:14 BRST 2010
401+Mon Jun 13 15:07:49 BRT 2011
395402 </SMALL>
396403 <!--
397−Last change: revised for Lua 5.2.0 (alpha)
404+Last change: revised for Lua 5.2.0 (beta)
398405 -->
399406
400407 </BODY>
src/Makefile+2−2
@@ -126,7 +126,7 @@ lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h · 125 unmodified lines
126126 lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h
127127 lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
128128 lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
129− lstring.h ltable.h
129+ lstring.h ltable.h lvm.h
130130 lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h
131131 lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h
132132 ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
@@ -164,7 +164,7 @@ lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ · 31 unmodified lines
164164 ltm.h lzio.h lstring.h lgc.h
165165 lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
166166 ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
167− ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h
167+ ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
168168 ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
169169 ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \
170170 lmem.h lstring.h lgc.h ltable.h
src/lapi.c+88−74
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lapi.c,v 2.140 2010/11/03 15:16:17 roberto Exp $
2+** $Id: lapi.c,v 2.149 2011/06/13 14:13:06 roberto Exp $
33 ** Lua API
44 ** See Copyright Notice in lua.h
55 */
@@ -59,9 +59,9 @@ static TValue *index2addr (lua_State *L, int idx) { · 53 unmodified lines
5959 if (ttislcf(ci->func)) /* light C function? */
6060 return cast(TValue *, luaO_nilobject); /* it has no upvalues */
6161 else {
62− Closure *func = clvalue(ci->func);
63− return (idx <= func->c.nupvalues)
64− ? &func->c.upvalue[idx-1]
62+ CClosure *func = clCvalue(ci->func);
63+ return (idx <= func->nupvalues)
64+ ? &func->upvalue[idx-1]
6565 : cast(TValue *, luaO_nilobject);
6666 }
6767 }
@@ -195,10 +195,8 @@ static void moveto (lua_State *L, TValue *fr, int idx) { · 127 unmodified lines
195195 TValue *to = index2addr(L, idx);
196196 api_checkvalidindex(L, to);
197197 setobj(L, to, fr);
198− if (idx < LUA_REGISTRYINDEX) { /* function upvalue? */
199− lua_assert(ttisclosure(L->ci->func));
200− luaC_barrier(L, clvalue(L->ci->func), fr);
201− }
198+ if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
199+ luaC_barrier(L, clCvalue(L->ci->func), fr);
202200 /* LUA_REGISTRYINDEX does not need gc barrier
203201 (collector revisits it before finishing collection) */
204202 }
@@ -251,7 +249,7 @@ LUA_API const char *lua_typename (lua_State *L, int t) { · 46 unmodified lines
251249
252250 LUA_API int lua_iscfunction (lua_State *L, int idx) {
253251 StkId o = index2addr(L, idx);
254− return (ttislcf(o) || (ttisclosure(o) && clvalue(o)->c.isC));
252+ return (ttislcf(o) || (ttisCclosure(o)));
255253 }
256254
257255
@@ -278,20 +276,28 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { · 20 unmodified lines
278276 StkId o1 = index2addr(L, index1);
279277 StkId o2 = index2addr(L, index2);
280278 return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
281− : luaO_rawequalObj(o1, o2);
279+ : luaV_rawequalobj(o1, o2);
282280 }
283281
284282
285283 LUA_API void lua_arith (lua_State *L, int op) {
284+ StkId o1; /* 1st operand */
285+ StkId o2; /* 2nd operand */
286286 lua_lock(L);
287− api_checknelems(L, 2);
288− if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1)) {
289− changenvalue(L->top - 2,
290− luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
287+ if (op != LUA_OPUNM) /* all other operations expect two operands */
288+ api_checknelems(L, 2);
289+ else { /* for unary minus, add fake 2nd operand */
290+ api_checknelems(L, 1);
291+ setobjs2s(L, L->top, L->top - 1);
292+ L->top++;
293+ }
294+ o1 = L->top - 2;
295+ o2 = L->top - 1;
296+ if (ttisnumber(o1) && ttisnumber(o2)) {
297+ changenvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
291298 }
292299 else
293− luaV_arith(L, L->top - 2, L->top - 2, L->top - 1,
294− cast(TMS, op - LUA_OPADD + TM_ADD));
300+ luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
295301 L->top--;
296302 lua_unlock(L);
297303 }
@@ -390,7 +396,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { · 92 unmodified lines
390396
391397 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
392398 StkId o = index2addr(L, idx);
393− switch (ttype(o)) {
399+ switch (ttypenv(o)) {
394400 case LUA_TSTRING: return tsvalue(o)->len;
395401 case LUA_TUSERDATA: return uvalue(o)->len;
396402 case LUA_TTABLE: return luaH_getn(hvalue(o));
@@ -402,15 +408,15 @@ LUA_API size_t lua_rawlen (lua_State *L, int idx) { · 5 unmodified lines
402408 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
403409 StkId o = index2addr(L, idx);
404410 if (ttislcf(o)) return fvalue(o);
405− else if (ttisclosure(o) && clvalue(o)->c.isC)
406− return clvalue(o)->c.f;
411+ else if (ttisCclosure(o))
412+ return clCvalue(o)->f;
407413 else return NULL; /* not a C function */
408414 }
409415
410416
411417 LUA_API void *lua_touserdata (lua_State *L, int idx) {
412418 StkId o = index2addr(L, idx);
413− switch (ttype(o)) {
419+ switch (ttypenv(o)) {
414420 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
415421 case LUA_TLIGHTUSERDATA: return pvalue(o);
416422 default: return NULL;
@@ -428,7 +434,8 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { · 11 unmodified lines
428434 StkId o = index2addr(L, idx);
429435 switch (ttype(o)) {
430436 case LUA_TTABLE: return hvalue(o);
431− case LUA_TFUNCTION: return clvalue(o);
437+ case LUA_TLCL: return clLvalue(o);
438+ case LUA_TCCL: return clCvalue(o);
432439 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
433440 case LUA_TTHREAD: return thvalue(o);
434441 case LUA_TUSERDATA:
@@ -456,6 +463,8 @@ LUA_API void lua_pushnil (lua_State *L) { · 21 unmodified lines
456463 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
457464 lua_lock(L);
458465 setnvalue(L->top, n);
466+ luai_checknum(L, L->top,
467+ luaG_runerror(L, "C API - attempt to push a signaling NaN"));
459468 api_incr_top(L);
460469 lua_unlock(L);
461470 }
@@ -548,7 +557,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { · 86 unmodified lines
548557 L->top -= n;
549558 while (n--)
550559 setobj2n(L, &cl->c.upvalue[n], L->top + n);
551− setclvalue(L, L->top, cl);
560+ setclCvalue(L, L->top, cl);
552561 }
553562 api_incr_top(L);
554563 lua_unlock(L);
@@ -648,7 +657,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { · 93 unmodified lines
648657 int res;
649658 lua_lock(L);
650659 obj = index2addr(L, objindex);
651− switch (ttype(obj)) {
660+ switch (ttypenv(obj)) {
652661 case LUA_TTABLE:
653662 mt = hvalue(obj)->metatable;
654663 break;
@@ -755,18 +764,19 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { · 100 unmodified lines
755764 api_check(L, ttistable(L->top - 1), "table expected");
756765 mt = hvalue(L->top - 1);
757766 }
758− switch (ttype(obj)) {
767+ switch (ttypenv(obj)) {
759768 case LUA_TTABLE: {
760769 hvalue(obj)->metatable = mt;
761770 if (mt)
762771 luaC_objbarrierback(L, gcvalue(obj), mt);
772+ luaC_checkfinalizer(L, gcvalue(obj), mt);
763773 break;
764774 }
765775 case LUA_TUSERDATA: {
766776 uvalue(obj)->metatable = mt;
767777 if (mt) {
768778 luaC_objbarrier(L, rawuvalue(obj), mt);
769− luaC_checkfinalizer(L, rawuvalue(obj));
779+ luaC_checkfinalizer(L, gcvalue(obj), mt);
770780 }
771781 break;
772782 }
@@ -912,15 +922,14 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, · 139 unmodified lines
912922 luaZ_init(L, &z, reader, data);
913923 status = luaD_protectedparser(L, &z, chunkname);
914924 if (status == LUA_OK) { /* no errors? */
915− Closure *f = clvalue(L->top - 1); /* get newly created function */
916− lua_assert(!f->c.isC);
917− if (f->l.nupvalues == 1) { /* does it have one upvalue? */
925+ LClosure *f = clLvalue(L->top - 1); /* get newly created function */
926+ if (f->nupvalues == 1) { /* does it have one upvalue? */
918927 /* get global table from registry */
919928 Table *reg = hvalue(&G(L)->l_registry);
920929 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
921930 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
922− setobj(L, f->l.upvals[0]->v, gt);
923− luaC_barrier(L, f->l.upvals[0], gt);
931+ setobj(L, f->upvals[0]->v, gt);
932+ luaC_barrier(L, f->upvals[0], gt);
924933 }
925934 }
926935 lua_unlock(L);
@@ -959,11 +968,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 32 unmodified lines
959968 g = G(L);
960969 switch (what) {
961970 case LUA_GCSTOP: {
962− stopgc(g);
971+ g->gcrunning = 0;
963972 break;
964973 }
965974 case LUA_GCRESTART: {
966− g->GCdebt = 0;
975+ luaE_setdebt(g, 0);
976+ g->gcrunning = 1;
967977 break;
968978 }
969979 case LUA_GCCOLLECT: {
@@ -972,30 +982,27 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 2 unmodified lines
972982 }
973983 case LUA_GCCOUNT: {
974984 /* GC values are expressed in Kbytes: #bytes/2^10 */
975− res = cast_int(g->totalbytes >> 10);
985+ res = cast_int(gettotalbytes(g) >> 10);
976986 break;
977987 }
978988 case LUA_GCCOUNTB: {
979− res = cast_int(g->totalbytes & 0x3ff);
989+ res = cast_int(gettotalbytes(g) & 0x3ff);
980990 break;
981991 }
982992 case LUA_GCSTEP: {
983− int stopped = gcstopped(g);
984993 if (g->gckind == KGC_GEN) { /* generational mode? */
985994 res = (g->lastmajormem == 0); /* 1 if will do major collection */
986− luaC_step(L); /* do a single step */
995+ luaC_forcestep(L); /* do a single step */
987996 }
988997 else {
989998 while (data-- >= 0) {
990− luaC_step(L);
999+ luaC_forcestep(L);
9911000 if (g->gcstate == GCSpause) { /* end of cycle? */
9921001 res = 1; /* signal it */
9931002 break;
9941003 }
9951004 }
9961005 }
997− if (stopped) /* collector was stopped? */
998− stopgc(g); /* keep it that way */
9991006 break;
10001007 }
10011008 case LUA_GCSETPAUSE: {
@@ -1014,7 +1021,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 12 unmodified lines
10141021 break;
10151022 }
10161023 case LUA_GCISRUNNING: {
1017− res = !gcstopped(g);
1024+ res = g->gcrunning;
10181025 break;
10191026 }
10201027 case LUA_GCGEN: { /* change collector to generational mode */
@@ -1124,25 +1131,27 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { · 103 unmodified lines
11241131
11251132 static const char *aux_upvalue (StkId fi, int n, TValue **val,
11261133 GCObject **owner) {
1127− Closure *f;
1128− if (!ttisclosure(fi)) return NULL;
1129− f = clvalue(fi);
1130− if (f->c.isC) {
1131− if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
1132− *val = &f->c.upvalue[n-1];
1133− if (owner) *owner = obj2gco(f);
1134− return "";
1135− }
1136− else {
1137− const char *name;
1138− Proto *p = f->l.p;
1139− if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1140− *val = f->l.upvals[n-1]->v;
1141− if (owner) *owner = obj2gco(f->l.upvals[n - 1]);
1142− name = getstr(p->upvalues[n-1].name);
1143− if (name == NULL) /* no debug information? */
1144− name = "";
1145− return name;
1134+ switch (ttype(fi)) {
1135+ case LUA_TCCL: { /* C closure */
1136+ CClosure *f = clCvalue(fi);
1137+ if (!(1 <= n && n <= f->nupvalues)) return NULL;
1138+ *val = &f->upvalue[n-1];
1139+ if (owner) *owner = obj2gco(f);
1140+ return "";
1141+ }
1142+ case LUA_TLCL: { /* Lua closure */
1143+ LClosure *f = clLvalue(fi);
1144+ const char *name;
1145+ Proto *p = f->p;
1146+ if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1147+ *val = f->upvals[n-1]->v;
1148+ if (owner) *owner = obj2gco(f->upvals[n - 1]);
1149+ name = getstr(p->upvalues[n-1].name);
1150+ if (name == NULL) /* no debug information? */
1151+ name = "";
1152+ return name;
1153+ }
1154+ default: return NULL; /* not a closure */
11461155 }
11471156 }
11481157
@@ -1180,34 +1189,39 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { · 31 unmodified lines
11801189 }
11811190
11821191
1183−static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
1184− Closure *f;
1192+static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1193+ LClosure *f;
11851194 StkId fi = index2addr(L, fidx);
1186− api_check(L, ttisclosure(fi), "Lua function expected");
1187− f = clvalue(fi);
1188− api_check(L, !f->c.isC, "Lua function expected");
1189− api_check(L, (1 <= n && n <= f->l.p->sizeupvalues), "invalid upvalue index");
1195+ api_check(L, ttisLclosure(fi), "Lua function expected");
1196+ f = clLvalue(fi);
1197+ api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
11901198 if (pf) *pf = f;
1191− return &f->l.upvals[n - 1]; /* get its upvalue pointer */
1199+ return &f->upvals[n - 1]; /* get its upvalue pointer */
11921200 }
11931201
11941202
11951203 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1196− Closure *f;
11971204 StkId fi = index2addr(L, fidx);
1198− api_check(L, ttisclosure(fi), "function expected");
1199− f = clvalue(fi);
1200− if (f->c.isC) {
1201− api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
1202− return &f->c.upvalue[n - 1];
1205+ switch (ttype(fi)) {
1206+ case LUA_TLCL: { /* lua closure */
1207+ return *getupvalref(L, fidx, n, NULL);
1208+ }
1209+ case LUA_TCCL: { /* C closure */
1210+ CClosure *f = clCvalue(fi);
1211+ api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1212+ return &f->upvalue[n - 1];
1213+ }
1214+ default: {
1215+ api_check(L, 0, "closure expected");
1216+ return NULL;
1217+ }
12031218 }
1204− else return *getupvalref(L, fidx, n, NULL);
12051219 }
12061220
12071221
12081222 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
12091223 int fidx2, int n2) {
1210− Closure *f1;
1224+ LClosure *f1;
12111225 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
12121226 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
12131227 *up1 = *up2;
src/lauxlib.c+77−13
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.c,v 1.227 2010/11/10 18:05:36 roberto Exp $
2+** $Id: lauxlib.c,v 1.232 2011/05/03 16:01:57 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -203,6 +203,64 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { · 197 unmodified lines
203203 return lua_error(L);
204204 }
205205
206+
207+LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
208+ int en = errno; /* calls to Lua API may change this value */
209+ if (stat) {
210+ lua_pushboolean(L, 1);
211+ return 1;
212+ }
213+ else {
214+ lua_pushnil(L);
215+ if (fname)
216+ lua_pushfstring(L, "%s: %s", fname, strerror(en));
217+ else
218+ lua_pushfstring(L, "%s", strerror(en));
219+ lua_pushinteger(L, en);
220+ return 3;
221+ }
222+}
223+
224+
225+#if !defined(inspectstat) /* { */
226+
227+#if defined(LUA_USE_POSIX)
228+
229+#include <sys/wait.h>
230+
231+/*
232+** use appropriate macros to interpret 'pclose' return status
233+*/
234+#define inspectstat(stat,what) \
235+ if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
236+ else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
237+
238+#else
239+
240+#define inspectstat(stat,what) /* no op */
241+
242+#endif
243+
244+#endif /* } */
245+
246+
247+LUALIB_API int luaL_execresult (lua_State *L, int stat) {
248+ const char *what = "exit"; /* type of termination */
249+ if (stat == -1) /* error? */
250+ return luaL_fileresult(L, 0, NULL);
251+ else {
252+ inspectstat(stat, what); /* interpret result */
253+ if (*what == 'e' && stat == 0) /* successful termination? */
254+ return luaL_fileresult(L, 1, NULL);
255+ else { /* return nil,what,code */
256+ lua_pushnil(L);
257+ lua_pushstring(L, what);
258+ lua_pushinteger(L, stat);
259+ return 3;
260+ }
261+ }
262+}
263+
206264 /* }====================================================== */
207265
208266
@@ -384,8 +442,10 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { · 175 unmodified lines
384442 newsize = B->n + sz;
385443 if (newsize < B->n || newsize - B->n < sz)
386444 luaL_error(L, "buffer too large");
387− newbuff = (char *)lua_newuserdata(L, newsize); /* create larger buffer */
388− memcpy(newbuff, B->b, B->n); /* move content to new buffer */
445+ /* create larger buffer */
446+ newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
447+ /* move content to new buffer */
448+ memcpy(newbuff, B->b, B->n * sizeof(char));
389449 if (buffonstack(B))
390450 lua_remove(L, -2); /* remove old buffer */
391451 B->b = newbuff;
@@ -397,7 +457,7 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { · 5 unmodified lines
397457
398458 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
399459 char *b = luaL_prepbuffsize(B, l);
400− memcpy(b, s, l);
460+ memcpy(b, s, l * sizeof(char));
401461 luaL_addsize(B, l);
402462 }
403463
@@ -700,8 +760,8 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { · 296 unmodified lines
700760 */
701761 #if defined(LUA_COMPAT_MODULE)
702762
703−static const char *luaL_findtablex (lua_State *L, int idx,
704− const char *fname, int szhint) {
763+static const char *luaL_findtable (lua_State *L, int idx,
764+ const char *fname, int szhint) {
705765 const char *e;
706766 if (idx) lua_pushvalue(L, idx);
707767 do {
@@ -745,13 +805,13 @@ static int libsize (const luaL_Reg *l) { · 37 unmodified lines
745805 */
746806 LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
747807 int sizehint) {
748− luaL_findtablex(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
808+ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
749809 lua_getfield(L, -1, modname); /* get _LOADED[modname] */
750810 if (!lua_istable(L, -1)) { /* not found? */
751811 lua_pop(L, 1); /* remove previous result */
752812 /* try global variable (and create one if it does not exist) */
753813 lua_pushglobaltable(L);
754− if (luaL_findtablex(L, 0, modname, sizehint) != NULL)
814+ if (luaL_findtable(L, 0, modname, sizehint) != NULL)
755815 luaL_error(L, "name conflict for module " LUA_QS, modname);
756816 lua_pushvalue(L, -1);
757817 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
@@ -767,7 +827,10 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, · 9 unmodified lines
767827 luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
768828 lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
769829 }
770− luaL_setfuncs(L, l, nup);
830+ if (l)
831+ luaL_setfuncs(L, l, nup);
832+ else
833+ lua_pop(L, nup); /* remove upvalues */
771834 }
772835
773836 #endif
@@ -780,7 +843,7 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, · 6 unmodified lines
780843 */
781844 LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
782845 luaL_checkstack(L, nup, "too many upvalues");
783− for (; l && l->name; l++) { /* fill the table with given functions */
846+ for (; l->name != NULL; l++) { /* fill the table with given functions */
784847 int i;
785848 for (i = 0; i < nup; i++) /* copy upvalues to the top */
786849 lua_pushvalue(L, -nup);
@@ -795,15 +858,16 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { · 8 unmodified lines
795858 ** ensure that stack[idx][fname] has a table and push that table
796859 ** into the stack
797860 */
798−LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) {
861+LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
799862 lua_getfield(L, idx, fname);
800− if (lua_istable(L, -1)) return; /* table already there */
863+ if (lua_istable(L, -1)) return 1; /* table already there */
801864 else {
802865 idx = lua_absindex(L, idx);
803866 lua_pop(L, 1); /* remove previous result */
804867 lua_newtable(L);
805868 lua_pushvalue(L, -1); /* copy to be left at top */
806869 lua_setfield(L, idx, fname); /* assign new table to field */
870+ return 0; /* false, because did not find table there */
807871 }
808872 }
809873
@@ -819,7 +883,7 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, · 9 unmodified lines
819883 lua_pushcfunction(L, openf);
820884 lua_pushstring(L, modname); /* argument to open function */
821885 lua_call(L, 1, 1); /* open module */
822− luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
886+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
823887 lua_pushvalue(L, -2); /* make copy of module (call result) */
824888 lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
825889 lua_pop(L, 1); /* remove _LOADED table */
src/lauxlib.h+5−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.h,v 1.113 2010/11/16 19:20:01 roberto Exp $
2+** $Id: lauxlib.h,v 1.116 2011/04/08 19:17:36 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -62,6 +62,9 @@ LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); · 56 unmodified lines
6262 LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
6363 const char *const lst[]);
6464
65+LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
66+LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
67+
6568 /* pre-defined references */
6669 #define LUA_NOREF (-2)
6770 #define LUA_REFNIL (-1)
@@ -83,7 +86,7 @@ LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, · 15 unmodified lines
8386
8487 LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
8588
86−LUALIB_API void (luaL_findtable) (lua_State *L, int idx, const char *fname);
89+LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
8790
8891 LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
8992 const char *msg, int level);
src/lbaselib.c+76−108
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lbaselib.c,v 1.251 2010/10/28 15:36:30 roberto Exp $
2+** $Id: lbaselib.c,v 1.261 2011/05/26 16:09:40 roberto Exp $
33 ** Basic library
44 ** See Copyright Notice in lua.h
55 */
@@ -32,44 +32,54 @@ static int luaB_print (lua_State *L) { · 26 unmodified lines
3232 lua_call(L, 1, 1);
3333 s = lua_tolstring(L, -1, &l); /* get result */
3434 if (s == NULL)
35− return luaL_error(L, LUA_QL("tostring") " must return a string to "
36− LUA_QL("print"));
35+ return luaL_error(L,
36+ LUA_QL("tostring") " must return a string to " LUA_QL("print"));
3737 if (i>1) luai_writestring("\t", 1);
3838 luai_writestring(s, l);
3939 lua_pop(L, 1); /* pop result */
4040 }
41− luai_writestring("\n", 1);
41+ luai_writeline();
4242 return 0;
4343 }
4444
4545
46+#define SPACECHARS " \f\n\r\t\v"
47+
4648 static int luaB_tonumber (lua_State *L) {
4749 int base = luaL_optint(L, 2, 10);
50+ luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
4851 if (base == 10) { /* standard conversion */
4952 luaL_checkany(L, 1);
5053 if (lua_isnumber(L, 1)) {
5154 lua_pushnumber(L, lua_tonumber(L, 1));
5255 return 1;
53− }
56+ } /* else not a number */
5457 }
5558 else {
56− const char *s1 = luaL_checkstring(L, 1);
57− char *s2;
58− unsigned long n;
59+ size_t l;
60+ const char *s = luaL_checklstring(L, 1, &l);
61+ const char *e = s + l; /* end point for 's' */
5962 int neg = 0;
60− luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
61− while (isspace((unsigned char)(*s1))) s1++; /* skip initial spaces */
62− if (*s1 == '-') { s1++; neg = 1; }
63− n = strtoul(s1, &s2, base);
64− if (s1 != s2) { /* at least one valid digit? */
65− while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
66− if (*s2 == '\0') { /* no invalid trailing characters? */
67− lua_pushnumber(L, (neg) ? -(lua_Number)n : (lua_Number)n);
63+ s += strspn(s, SPACECHARS); /* skip initial spaces */
64+ if (*s == '-') { s++; neg = 1; } /* handle signal */
65+ else if (*s == '+') s++;
66+ if (isalnum((unsigned char)*s)) {
67+ lua_Number n = 0;
68+ do {
69+ int digit = (isdigit((unsigned char)*s)) ? *s - '0'
70+ : toupper((unsigned char)*s) - 'A' + 10;
71+ if (digit >= base) break; /* invalid numeral; force a fail */
72+ n = n * (lua_Number)base + (lua_Number)digit;
73+ s++;
74+ } while (isalnum((unsigned char)*s));
75+ s += strspn(s, SPACECHARS); /* skip trailing spaces */
76+ if (s == e) { /* no invalid trailing characters? */
77+ lua_pushnumber(L, (neg) ? -n : n);
6878 return 1;
69− }
70− }
79+ } /* else not a number */
80+ } /* else not a number */
7181 }
72− lua_pushnil(L); /* else not a number */
82+ lua_pushnil(L); /* not a number */
7383 return 1;
7484 }
7585
@@ -110,12 +120,10 @@ static int luaB_setmetatable (lua_State *L) { · 34 unmodified lines
110120 }
111121
112122
113−static int luaB_getfenv (lua_State *L) {
114− return luaL_error(L, "getfenv/setfenv deprecated");
123+static int luaB_deprecated (lua_State *L) {
124+ return luaL_error(L, "deprecated function");
115125 }
116126
117−#define luaB_setfenv luaB_getfenv
118−
119127
120128 static int luaB_rawequal (lua_State *L) {
121129 luaL_checkany(L, 1);
@@ -125,6 +133,15 @@ static int luaB_rawequal (lua_State *L) { · 3 unmodified lines
125133 }
126134
127135
136+static int luaB_rawlen (lua_State *L) {
137+ int t = lua_type(L, 1);
138+ luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
139+ "table or string expected");
140+ lua_pushinteger(L, lua_rawlen(L, 1));
141+ return 1;
142+}
143+
144+
128145 static int luaB_rawget (lua_State *L) {
129146 luaL_checktype(L, 1, LUA_TTABLE);
130147 luaL_checkany(L, 2);
@@ -251,6 +268,11 @@ static int luaB_loadfile (lua_State *L) { · 120 unmodified lines
251268 ** =======================================================
252269 */
253270
271+/*
272+** check whether a chunk (prefix in 's') satisfies given 'mode'
273+** ('t' for text, 'b' for binary). Returns error message (also
274+** pushed on the stack) in case of errors.
275+*/
254276 static const char *checkrights (lua_State *L, const char *mode, const char *s) {
255277 if (strchr(mode, 'b') == NULL && *s == LUA_SIGNATURE[0])
256278 return lua_pushstring(L, "attempt to load a binary chunk");
@@ -261,10 +283,11 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) { · 4 unmodified lines
261283
262284
263285 /*
264−** reserves a slot, above all arguments, to hold a copy of the returned
265−** string to avoid it being collected while parsed
286+** reserved slot, above all arguments, to hold a copy of the returned
287+** string to avoid it being collected while parsed. 'load' has four
288+** optional arguments (chunk, source name, mode, and environment).
266289 */
267−#define RESERVEDSLOT 4
290+#define RESERVEDSLOT 5
268291
269292
270293 /*
@@ -273,25 +296,20 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) { · 2 unmodified lines
273296 ** stack top. Instead, it keeps its resulting string in a
274297 ** reserved slot inside the stack.
275298 */
276−typedef struct { /* reader state */
277− int f; /* position of reader function on stack */
278− const char *mode; /* allowed modes (binary/text) */
279−} Readstat;
280−
281299 static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
282300 const char *s;
283− Readstat *stat = (Readstat *)ud;
301+ const char **mode = (const char **)ud;
284302 luaL_checkstack(L, 2, "too many nested functions");
285− lua_pushvalue(L, stat->f); /* get function */
303+ lua_pushvalue(L, 1); /* get function */
286304 lua_call(L, 0, 1); /* call it */
287305 if (lua_isnil(L, -1)) {
288306 *size = 0;
289307 return NULL;
290308 }
291309 else if ((s = lua_tostring(L, -1)) != NULL) {
292− if (stat->mode != NULL) { /* first time? */
293− s = checkrights(L, stat->mode, s); /* check mode */
294− stat->mode = NULL; /* to avoid further checks */
310+ if (*mode != NULL) { /* first time? */
311+ s = checkrights(L, *mode, s); /* check mode */
312+ *mode = NULL; /* to avoid further checks */
295313 if (s) luaL_error(L, s);
296314 }
297315 lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
@@ -304,52 +322,38 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { · 6 unmodified lines
304322 }
305323
306324
307−static int luaB_load_aux (lua_State *L, int farg) {
325+static int luaB_load (lua_State *L) {
308326 int status;
309− Readstat stat;
310327 size_t l;
311− const char *s = lua_tolstring(L, farg, &l);
312− stat.mode = luaL_optstring(L, farg + 2, "bt");
328+ int top = lua_gettop(L);
329+ const char *s = lua_tolstring(L, 1, &l);
330+ const char *mode = luaL_optstring(L, 3, "bt");
313331 if (s != NULL) { /* loading a string? */
314− const char *chunkname = luaL_optstring(L, farg + 1, s);
315− status = (checkrights(L, stat.mode, s) != NULL)
332+ const char *chunkname = luaL_optstring(L, 2, s);
333+ status = (checkrights(L, mode, s) != NULL)
316334 || luaL_loadbuffer(L, s, l, chunkname);
317335 }
318336 else { /* loading from a reader function */
319− const char *chunkname = luaL_optstring(L, farg + 1, "=(load)");
320− luaL_checktype(L, farg, LUA_TFUNCTION);
321− stat.f = farg;
337+ const char *chunkname = luaL_optstring(L, 2, "=(load)");
338+ luaL_checktype(L, 1, LUA_TFUNCTION);
322339 lua_settop(L, RESERVEDSLOT); /* create reserved slot */
323− status = lua_load(L, generic_reader, &stat, chunkname);
340+ status = lua_load(L, generic_reader, &mode, chunkname);
341+ }
342+ if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */
343+ lua_pushvalue(L, 4); /* environment for loaded function */
344+ lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */
324345 }
325346 return load_aux(L, status);
326347 }
327348
328349
329−static int luaB_load (lua_State *L) {
330− return luaB_load_aux(L, 1);
331−}
332−
333−
334−static int luaB_loadin (lua_State *L) {
335− int n;
336− luaL_checkany(L, 1);
337− n = luaB_load_aux(L, 2);
338− if (n == 1) { /* success? */
339− lua_pushvalue(L, 1); /* environment for loaded function */
340− if (lua_setupvalue(L, -2, 1) == NULL)
341− luaL_error(L, "loaded chunk does not have an upvalue");
342− }
343− return n;
344−}
350+#if defined(LUA_COMPAT_LOADSTRING)
351+#define luaB_loadstring luaB_load
352+#else
353+#define luaB_loadstring luaB_deprecated
354+#endif
345355
346356
347−static int luaB_loadstring (lua_State *L) {
348− lua_settop(L, 2);
349− lua_pushliteral(L, "tb");
350− return luaB_load(L); /* dostring(s, n) == load(s, n, "tb") */
351−
352−}
353357 /* }====================================================== */
354358
355359
@@ -391,7 +395,7 @@ static int luaB_select (lua_State *L) { · 35 unmodified lines
391395
392396
393397 static int pcallcont (lua_State *L) {
394− int errfunc; /* call has an error function in bottom of the stack */
398+ int errfunc = 0; /* =0 to avoid warnings */
395399 int status = lua_getctx(L, &errfunc);
396400 lua_assert(status != LUA_OK);
397401 lua_pushboolean(L, (status == LUA_YIELD)); /* first result (status) */
@@ -436,55 +440,27 @@ static int luaB_tostring (lua_State *L) { · 38 unmodified lines
436440 }
437441
438442
439−static int luaB_newproxy (lua_State *L) {
440− lua_settop(L, 1);
441− lua_newuserdata(L, 0); /* create proxy */
442− if (lua_toboolean(L, 1) == 0)
443− return 1; /* no metatable */
444− else if (lua_isboolean(L, 1)) {
445− lua_createtable(L, 0, 1); /* create a new metatable `m' ... */
446− lua_pushboolean(L, 1);
447− lua_setfield(L, -2, "__gc"); /* ... m.__gc = false (HACK!!)... */
448− lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
449− lua_pushboolean(L, 1);
450− lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
451− }
452− else {
453− int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
454− if (lua_getmetatable(L, 1)) {
455− lua_rawget(L, lua_upvalueindex(1));
456− validproxy = lua_toboolean(L, -1);
457− lua_pop(L, 1); /* remove value */
458− }
459− luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
460− lua_getmetatable(L, 1); /* metatable is valid; get it */
461− }
462− lua_setmetatable(L, 2);
463− return 1;
464−}
465−
466−
467443 static const luaL_Reg base_funcs[] = {
468444 {"assert", luaB_assert},
469445 {"collectgarbage", luaB_collectgarbage},
470446 {"dofile", luaB_dofile},
471447 {"error", luaB_error},
472− {"getfenv", luaB_getfenv},
448+ {"getfenv", luaB_deprecated},
473449 {"getmetatable", luaB_getmetatable},
474450 {"ipairs", luaB_ipairs},
475451 {"loadfile", luaB_loadfile},
476452 {"load", luaB_load},
477− {"loadin", luaB_loadin},
478453 {"loadstring", luaB_loadstring},
479454 {"next", luaB_next},
480455 {"pairs", luaB_pairs},
481456 {"pcall", luaB_pcall},
482457 {"print", luaB_print},
483458 {"rawequal", luaB_rawequal},
459+ {"rawlen", luaB_rawlen},
484460 {"rawget", luaB_rawget},
485461 {"rawset", luaB_rawset},
486462 {"select", luaB_select},
487− {"setfenv", luaB_setfenv},
463+ {"setfenv", luaB_deprecated},
488464 {"setmetatable", luaB_setmetatable},
489465 {"tonumber", luaB_tonumber},
490466 {"tostring", luaB_tostring},
@@ -503,14 +479,6 @@ LUAMOD_API int luaopen_base (lua_State *L) { · 12 unmodified lines
503479 luaL_setfuncs(L, base_funcs, 0);
504480 lua_pushliteral(L, LUA_VERSION);
505481 lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
506− /* `newproxy' needs a weaktable as upvalue */
507− lua_createtable(L, 0, 1); /* new table `w' */
508− lua_pushvalue(L, -1); /* `w' will be its own metatable */
509− lua_setmetatable(L, -2);
510− lua_pushliteral(L, "kv");
511− lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
512− lua_pushcclosure(L, luaB_newproxy, 1);
513− lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */
514482 return 1;
515483 }
516484
src/lbitlib.c+67−21
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lbitlib.c,v 1.13 2010/11/22 18:06:33 roberto Exp $
2+** $Id: lbitlib.c,v 1.15 2010/12/17 13:26:38 roberto Exp $
33 ** Standard library for bitwise operations
44 ** See Copyright Notice in lua.h
55 */
@@ -14,25 +14,30 @@ · 8 unmodified lines
1414
1515
1616 /* number of bits to consider in a number */
17−#define NBITS 32
17+#if !defined(LUA_NBITS)
18+#define LUA_NBITS 32
19+#endif
1820
19−#define ALLONES (~(((~(lua_Unsigned)0) << (NBITS - 1)) << 1))
2021
21−/* mask to trim extra bits */
22+#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
23+
24+/* macro to trim extra bits */
2225 #define trim(x) ((x) & ALLONES)
2326
2427
25−typedef lua_Unsigned b_uint;
28+/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
29+#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
30+
2631
32+typedef lua_Unsigned b_uint;
2733
28−#define getuintarg(L,arg) luaL_checkunsigned(L,arg)
2934
3035
3136 static b_uint andaux (lua_State *L) {
3237 int i, n = lua_gettop(L);
3338 b_uint r = ~(b_uint)0;
3439 for (i = 1; i <= n; i++)
35− r &= getuintarg(L, i);
40+ r &= luaL_checkunsigned(L, i);
3641 return trim(r);
3742 }
3843
@@ -55,7 +60,7 @@ static int b_or (lua_State *L) { · 16 unmodified lines
5560 int i, n = lua_gettop(L);
5661 b_uint r = 0;
5762 for (i = 1; i <= n; i++)
58− r |= getuintarg(L, i);
63+ r |= luaL_checkunsigned(L, i);
5964 lua_pushunsigned(L, trim(r));
6065 return 1;
6166 }
@@ -65,14 +70,14 @@ static int b_xor (lua_State *L) { · 3 unmodified lines
6570 int i, n = lua_gettop(L);
6671 b_uint r = 0;
6772 for (i = 1; i <= n; i++)
68− r ^= getuintarg(L, i);
73+ r ^= luaL_checkunsigned(L, i);
6974 lua_pushunsigned(L, trim(r));
7075 return 1;
7176 }
7277
7378
7479 static int b_not (lua_State *L) {
75− b_uint r = ~getuintarg(L, 1);
80+ b_uint r = ~luaL_checkunsigned(L, 1);
7681 lua_pushunsigned(L, trim(r));
7782 return 1;
7883 }
@@ -82,11 +87,11 @@ static int b_shift (lua_State *L, b_uint r, int i) { · 3 unmodified lines
8287 if (i < 0) { /* shift right? */
8388 i = -i;
8489 r = trim(r);
85− if (i >= NBITS) r = 0;
90+ if (i >= LUA_NBITS) r = 0;
8691 else r >>= i;
8792 }
8893 else { /* shift left */
89− if (i >= NBITS) r = 0;
94+ if (i >= LUA_NBITS) r = 0;
9095 else r <<= i;
9196 r = trim(r);
9297 }
@@ -96,22 +101,22 @@ static int b_shift (lua_State *L, b_uint r, int i) { · 3 unmodified lines
96101
97102
98103 static int b_lshift (lua_State *L) {
99− return b_shift(L, getuintarg(L, 1), luaL_checkint(L, 2));
104+ return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
100105 }
101106
102107
103108 static int b_rshift (lua_State *L) {
104− return b_shift(L, getuintarg(L, 1), -luaL_checkint(L, 2));
109+ return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
105110 }
106111
107112
108113 static int b_arshift (lua_State *L) {
109− b_uint r = getuintarg(L, 1);
114+ b_uint r = luaL_checkunsigned(L, 1);
110115 int i = luaL_checkint(L, 2);
111− if (i < 0 || !(r & ((b_uint)1 << (NBITS - 1))))
116+ if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
112117 return b_shift(L, r, -i);
113118 else { /* arithmetic shift for 'negative' number */
114− if (i >= NBITS) r = ALLONES;
119+ if (i >= LUA_NBITS) r = ALLONES;
115120 else
116121 r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
117122 lua_pushunsigned(L, r);
@@ -121,10 +126,10 @@ static int b_arshift (lua_State *L) { · 3 unmodified lines
121126
122127
123128 static int b_rot (lua_State *L, int i) {
124− b_uint r = getuintarg(L, 1);
125− i &= (NBITS - 1); /* i = i % NBITS */
129+ b_uint r = luaL_checkunsigned(L, 1);
130+ i &= (LUA_NBITS - 1); /* i = i % NBITS */
126131 r = trim(r);
127− r = (r << i) | (r >> (NBITS - i));
132+ r = (r << i) | (r >> (LUA_NBITS - i));
128133 lua_pushunsigned(L, trim(r));
129134 return 1;
130135 }
@@ -140,17 +145,58 @@ static int b_rrot (lua_State *L) { · 9 unmodified lines
140145 }
141146
142147
148+/*
149+** get field and width arguments for field-manipulation functions,
150+** checking whether they are valid
151+*/
152+static int fieldargs (lua_State *L, int farg, int *width) {
153+ int f = luaL_checkint(L, farg);
154+ int w = luaL_optint(L, farg + 1, 1);
155+ luaL_argcheck(L, 0 <= f, farg, "field cannot be netative");
156+ luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
157+ if (f + w > LUA_NBITS)
158+ luaL_error(L, "trying to access non-existent bits");
159+ *width = w;
160+ return f;
161+}
162+
163+
164+static int b_extract (lua_State *L) {
165+ int w;
166+ b_uint r = luaL_checkunsigned(L, 1);
167+ int f = fieldargs(L, 2, &w);
168+ r = (r >> f) & mask(w);
169+ lua_pushunsigned(L, r);
170+ return 1;
171+}
172+
173+
174+static int b_replace (lua_State *L) {
175+ int w;
176+ b_uint r = luaL_checkunsigned(L, 1);
177+ b_uint v = luaL_checkunsigned(L, 2);
178+ int f = fieldargs(L, 3, &w);
179+ int m = mask(w);
180+ v &= m; /* erase bits outside given width */
181+ r = (r & ~(m << f)) | (v << f);
182+ lua_pushunsigned(L, r);
183+ return 1;
184+}
185+
186+
143187 static const luaL_Reg bitlib[] = {
144188 {"arshift", b_arshift},
145189 {"band", b_and},
146190 {"bnot", b_not},
147191 {"bor", b_or},
148192 {"bxor", b_xor},
193+ {"btest", b_test},
194+ {"extract", b_extract},
149195 {"lrotate", b_lrot},
150196 {"lshift", b_lshift},
197+ {"replace", b_replace},
151198 {"rrotate", b_rrot},
152199 {"rshift", b_rshift},
153− {"btest", b_test},
154200 {NULL, NULL}
155201 };
156202
src/lcode.c+42−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcode.c,v 2.49 2010/07/07 16:27:29 roberto Exp $
2+** $Id: lcode.c,v 2.56 2011/05/31 18:27:56 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -23,6 +23,7 @@ · 17 unmodified lines
2323 #include "lparser.h"
2424 #include "lstring.h"
2525 #include "ltable.h"
26+#include "lvm.h"
2627
2728
2829 #define hasjumps(e) ((e)->t != (e)->f)
@@ -35,19 +36,23 @@ static int isnumeral(expdesc *e) { · 6 unmodified lines
3536
3637 void luaK_nil (FuncState *fs, int from, int n) {
3738 Instruction *previous;
39+ int l = from + n - 1; /* last register to set nil */
3840 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
3941 previous = &fs->f->code[fs->pc-1];
4042 if (GET_OPCODE(*previous) == OP_LOADNIL) {
4143 int pfrom = GETARG_A(*previous);
42− int pto = GETARG_B(*previous);
43− if (pfrom <= from && from <= pto+1) { /* can connect both? */
44− if (from+n-1 > pto)
45− SETARG_B(*previous, from+n-1);
44+ int pl = pfrom + GETARG_B(*previous);
45+ if ((pfrom <= from && from <= pl + 1) ||
46+ (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
47+ if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
48+ if (pl > l) l = pl; /* l = max(l, pl) */
49+ SETARG_A(*previous, from);
50+ SETARG_B(*previous, l - from);
4651 return;
4752 }
48− }
53+ } /* else go through */
4954 }
50− luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
55+ luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
5156 }
5257
5358
@@ -171,6 +176,19 @@ void luaK_patchlist (FuncState *fs, int list, int target) { · 117 unmodified lines
171176 }
172177
173178
179+LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
180+ level++; /* argument is +1 to reserve 0 as non-op */
181+ while (list != NO_JUMP) {
182+ int next = getjump(fs, list);
183+ lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
184+ (GETARG_A(fs->f->code[list]) == 0 ||
185+ GETARG_A(fs->f->code[list]) >= level));
186+ SETARG_A(fs->f->code[list], level);
187+ list = next;
188+ }
189+}
190+
191+
174192 void luaK_patchtohere (FuncState *fs, int list) {
175193 luaK_getlabel(fs);
176194 luaK_concat(fs, &fs->jpc, list);
@@ -229,11 +247,11 @@ static int codeextraarg (FuncState *fs, int a) { · 52 unmodified lines
229247 }
230248
231249
232−int luaK_codeABxX (FuncState *fs, OpCode o, int reg, int k) {
233− if (k < MAXARG_Bx)
234− return luaK_codeABx(fs, o, reg, k + 1);
250+int luaK_codek (FuncState *fs, int reg, int k) {
251+ if (k <= MAXARG_Bx)
252+ return luaK_codeABx(fs, OP_LOADK, reg, k);
235253 else {
236− int p = luaK_codeABx(fs, o, reg, 0);
254+ int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
237255 codeextraarg(fs, k);
238256 return p;
239257 }
@@ -278,7 +296,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { · 38 unmodified lines
278296 if (ttisnumber(idx)) {
279297 lua_Number n = nvalue(idx);
280298 lua_number2int(k, n);
281− if (luaO_rawequalObj(&f->k[k], v))
299+ if (luaV_rawequalobj(&f->k[k], v))
282300 return k;
283301 /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
284302 go through and create a new entry for this value */
@@ -564,15 +582,15 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { · 279 unmodified lines
564582
565583
566584 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
567− int func;
585+ int ereg;
568586 luaK_exp2anyreg(fs, e);
587+ ereg = e->u.info; /* register where 'e' was placed */
569588 freeexp(fs, e);
570− func = fs->freereg;
571− luaK_codeABC(fs, OP_SELF, func, e->u.info, luaK_exp2RK(fs, key));
572− freeexp(fs, key);
573− luaK_reserveregs(fs, 2);
574− e->u.info = func;
589+ e->u.info = fs->freereg; /* base register for op_self */
575590 e->k = VNONRELOC;
591+ luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
592+ luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
593+ freeexp(fs, key);
576594 }
577595
578596
@@ -603,21 +621,14 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { · 24 unmodified lines
603621 int pc; /* pc of last jump */
604622 luaK_dischargevars(fs, e);
605623 switch (e->k) {
606− case VK: case VKNUM: case VTRUE: {
607− pc = NO_JUMP; /* always true; do nothing */
608− break;
609− }
610624 case VJMP: {
611625 invertjump(fs, e);
612626 pc = e->u.info;
613627 break;
614628 }
615− case VFALSE: {
616− if (!hasjumps(e)) {
617− pc = luaK_jump(fs); /* always jump */
618− break;
619− }
620− /* else go through */
629+ case VK: case VKNUM: case VTRUE: {
630+ pc = NO_JUMP; /* always true; do nothing */
631+ break;
621632 }
622633 default: {
623634 pc = jumponcond(fs, e, 0);
@@ -634,20 +645,13 @@ static void luaK_goiffalse (FuncState *fs, expdesc *e) { · 10 unmodified lines
634645 int pc; /* pc of last jump */
635646 luaK_dischargevars(fs, e);
636647 switch (e->k) {
637− case VNIL: case VFALSE: {
638− pc = NO_JUMP; /* always false; do nothing */
639− break;
640− }
641648 case VJMP: {
642649 pc = e->u.info;
643650 break;
644651 }
645− case VTRUE: {
646− if (!hasjumps(e)) {
647− pc = luaK_jump(fs); /* always jump */
648− break;
649− }
650− /* else go through */
652+ case VNIL: case VFALSE: {
653+ pc = NO_JUMP; /* always false; do nothing */
654+ break;
651655 }
652656 default: {
653657 pc = jumponcond(fs, e, 1);
src/lcode.h+3−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcode.h,v 1.55 2010/07/02 20:42:40 roberto Exp $
2+** $Id: lcode.h,v 1.57 2011/04/07 18:14:12 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -44,11 +44,9 @@ typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; · 38 unmodified lines
4444
4545 #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
4646
47−#define luaK_codek(fs,reg,k) luaK_codeABxX(fs, OP_LOADK, reg, k)
48−
4947 LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
5048 LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
51−LUAI_FUNC int luaK_codeABxX (FuncState *fs, OpCode o, int reg, int k);
49+LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k);
5250 LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
5351 LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
5452 LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
@@ -71,6 +69,7 @@ LUAI_FUNC int luaK_jump (FuncState *fs); · 16 unmodified lines
7169 LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
7270 LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
7371 LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
72+LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level);
7473 LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
7574 LUAI_FUNC int luaK_getlabel (FuncState *fs);
7675 LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
src/ldblib.c+8−19
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldblib.c,v 1.126 2010/11/16 18:01:28 roberto Exp $
2+** $Id: ldblib.c,v 1.130 2011/04/08 19:17:36 roberto Exp $
33 ** Interface from Lua to its debug API
44 ** See Copyright Notice in lua.h
55 */
@@ -18,6 +18,9 @@ · 12 unmodified lines
1818 #include "lualib.h"
1919
2020
21+#define HOOKKEY "_HKEY"
22+
23+
2124
2225 static int db_getregistry (lua_State *L) {
2326 lua_pushvalue(L, LUA_REGISTRYINDEX);
@@ -39,8 +42,8 @@ static int db_setmetatable (lua_State *L) { · 15 unmodified lines
3942 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
4043 "nil or table expected");
4144 lua_settop(L, 2);
42− lua_pushboolean(L, lua_setmetatable(L, 1));
43− return 1;
45+ lua_setmetatable(L, 1);
46+ return 1; /* return 1st argument */
4447 }
4548
4649
@@ -250,14 +253,13 @@ static int db_upvaluejoin (lua_State *L) { · 203 unmodified lines
250253 }
251254
252255
253−static const char KEY_HOOK = 'h';
256+#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY);
254257
255258
256259 static void hookf (lua_State *L, lua_Debug *ar) {
257260 static const char *const hooknames[] =
258261 {"call", "return", "line", "count", "tail call"};
259− lua_pushlightuserdata(L, (void *)&KEY_HOOK);
260− lua_rawget(L, LUA_REGISTRYINDEX);
262+ gethooktable(L);
261263 lua_pushlightuserdata(L, L);
262264 lua_rawget(L, -2);
263265 if (lua_isfunction(L, -1)) {
@@ -291,19 +293,6 @@ static char *unmakemask (int mask, char *smask) { · 27 unmodified lines
291293 }
292294
293295
294−static void gethooktable (lua_State *L) {
295− lua_pushlightuserdata(L, (void *)&KEY_HOOK);
296− lua_rawget(L, LUA_REGISTRYINDEX);
297− if (!lua_istable(L, -1)) {
298− lua_pop(L, 1);
299− lua_createtable(L, 0, 1);
300− lua_pushlightuserdata(L, (void *)&KEY_HOOK);
301− lua_pushvalue(L, -2);
302− lua_rawset(L, LUA_REGISTRYINDEX);
303− }
304−}
305−
306−
307296 static int db_sethook (lua_State *L) {
308297 int arg, mask, count;
309298 lua_Hook func;
src/ldebug.c+74−36
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldebug.c,v 2.74 2010/10/11 20:24:42 roberto Exp $
2+** $Id: ldebug.c,v 2.82 2011/06/02 19:31:40 roberto Exp $
33 ** Debug Interface
44 ** See Copyright Notice in lua.h
55 */
@@ -35,12 +35,12 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); · 29 unmodified lines
3535
3636 static int currentpc (CallInfo *ci) {
3737 lua_assert(isLua(ci));
38− return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
38+ return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
3939 }
4040
4141
4242 static int currentline (CallInfo *ci) {
43− return getfuncline(ci_func(ci)->l.p, currentpc(ci));
43+ return getfuncline(ci_func(ci)->p, currentpc(ci));
4444 }
4545
4646
@@ -94,13 +94,28 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { · 47 unmodified lines
9494 }
9595
9696
97+static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
98+ int nparams = clLvalue(ci->func)->p->numparams;
99+ if (n >= ci->u.l.base - ci->func - nparams)
100+ return NULL; /* no such vararg */
101+ else {
102+ *pos = ci->func + nparams + n;
103+ return "(*vararg)"; /* generic name for any vararg */
104+ }
105+}
106+
107+
97108 static const char *findlocal (lua_State *L, CallInfo *ci, int n,
98109 StkId *pos) {
99110 const char *name = NULL;
100111 StkId base;
101112 if (isLua(ci)) {
102− base = ci->u.l.base;
103− name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci));
113+ if (n < 0) /* access to vararg values? */
114+ return findvararg(ci, -n, pos);
115+ else {
116+ base = ci->u.l.base;
117+ name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
118+ }
104119 }
105120 else
106121 base = ci->func + 1;
@@ -108,10 +123,8 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, · 1 unmodified line
108123 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
109124 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
110125 name = "(*temporary)"; /* generic name for any valid slot */
111− else {
112− *pos = base; /* to avoid warnings */
126+ else
113127 return NULL; /* no name */
114− }
115128 }
116129 *pos = base + (n - 1);
117130 return name;
@@ -125,10 +138,10 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { · 7 unmodified lines
125138 if (!isLfunction(L->top - 1)) /* not a Lua function? */
126139 name = NULL;
127140 else /* consider live variables at function start (parameters) */
128− name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0);
141+ name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
129142 }
130143 else { /* active function; get information through 'ar' */
131− StkId pos;
144+ StkId pos = 0; /* to avoid warnings */
132145 name = findlocal(L, ar->i_ci, n, &pos);
133146 if (name) {
134147 setobj2s(L, L->top, pos);
@@ -141,11 +154,11 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { · 6 unmodified lines
141154
142155
143156 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
144− StkId pos;
157+ StkId pos = 0; /* to avoid warnings */
145158 const char *name = findlocal(L, ar->i_ci, n, &pos);
146159 lua_lock(L);
147160 if (name)
148− setobjs2s(L, pos, L->top - 1);
161+ setobjs2s(L, pos, L->top - 1);
149162 L->top--; /* pop value */
150163 lua_unlock(L);
151164 return name;
@@ -243,7 +256,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { · 91 unmodified lines
243256 if (*what == '>') {
244257 ci = NULL;
245258 func = L->top - 1;
246− luai_apicheck(L, ttisfunction(func));
259+ api_check(L, ttisfunction(func), "function expected");
247260 what++; /* skip the '>' */
248261 L->top--; /* pop function */
249262 }
@@ -271,21 +284,38 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { · 21 unmodified lines
271284 ** =======================================================
272285 */
273286
287+static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
288+ const char **name);
274289
275−static void kname (Proto *p, int c, int reg, const char *what,
276− const char **name) {
277− if (c == reg && what && *what == 'c')
278− return; /* index is a constant; name already correct */
279− else if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
280− *name = svalue(&p->k[INDEXK(c)]);
281− else
282− *name = "?";
290+
291+/*
292+** find a "name" for the RK value 'c'
293+*/
294+static void kname (lua_State *L, CallInfo *ci, int c, int oreg,
295+ const char *what, const char **name) {
296+ if (ISK(c)) { /* is 'c' a constant? */
297+ TValue *kvalue = &ci_func(ci)->p->k[INDEXK(c)];
298+ if (ttisstring(kvalue)) { /* literal constant? */
299+ *name = svalue(kvalue); /* it is its own name */
300+ return;
301+ }
302+ /* else no reasonable name found */
303+ }
304+ else { /* 'c' is a register */
305+ if (c != oreg) /* not the original register? */
306+ what = getobjname(L, ci, c, name); /* search for 'c' */
307+ if (what && *what == 'c') { /* found a constant name? */
308+ return; /* 'name' already filled */
309+ }
310+ /* else no reasonable name found */
311+ }
312+ *name = "?"; /* no reasonable name found */
283313 }
284314
285315
286316 static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
287317 const char **name) {
288− Proto *p = ci_func(ci)->l.p;
318+ Proto *p = ci_func(ci)->p;
289319 const char *what = NULL;
290320 int lastpc = currentpc(ci);
291321 int pc;
@@ -312,10 +342,10 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 20 unmodified lines
312342 if (reg == a) {
313343 int k = GETARG_C(i); /* key index */
314344 int t = GETARG_B(i);
315− const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
345+ const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
316346 ? luaF_getlocalname(p, t + 1, pc)
317347 : getstr(p->upvalues[t].name);
318− kname(p, k, a, what, name);
348+ kname(L, ci, k, a, what, name);
319349 what = (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
320350 }
321351 break;
@@ -329,10 +359,11 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 7 unmodified lines
329359 }
330360 break;
331361 }
332− case OP_LOADK: {
362+ case OP_LOADK:
363+ case OP_LOADKX: {
333364 if (reg == a) {
334− int b = GETARG_Bx(i);
335− b = (b > 0) ? b - 1 : GETARG_Ax(p->code[pc + 1]);
365+ int b = (op == OP_LOADK) ? GETARG_Bx(i)
366+ : GETARG_Ax(p->code[pc + 1]);
336367 if (ttisstring(&p->k[b])) {
337368 what = "constant";
338369 *name = svalue(&p->k[b]);
@@ -341,15 +372,15 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 2 unmodified lines
341372 break;
342373 }
343374 case OP_LOADNIL: {
344− int b = GETARG_B(i); /* move from 'b' to 'a' */
345− if (a <= reg && reg <= b) /* set registers from 'a' to 'b' */
375+ int b = GETARG_B(i);
376+ if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
346377 what = NULL;
347378 break;
348379 }
349380 case OP_SELF: {
350381 if (reg == a) {
351382 int k = GETARG_C(i); /* key index */
352− kname(p, k, a, what, name);
383+ kname(L, ci, k, a, what, name);
353384 what = "method";
354385 }
355386 break;
@@ -371,6 +402,10 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 15 unmodified lines
371402 pc += b; /* do the jump */
372403 break;
373404 }
405+ case OP_TEST: {
406+ if (reg == a) what = NULL; /* jumped code can change 'a' */
407+ break;
408+ }
374409 default:
375410 if (testAMode(op) && reg == a) what = NULL;
376411 break;
@@ -386,9 +421,9 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { · 9 unmodified lines
386421 if ((ci->callstatus & CIST_TAIL) || !isLua(ci->previous))
387422 return NULL; /* calling function is not Lua (or is unknown) */
388423 ci = ci->previous; /* calling function */
389− i = ci_func(ci)->l.p->code[currentpc(ci)];
424+ i = ci_func(ci)->p->code[currentpc(ci)];
390425 if (GET_OPCODE(i) == OP_EXTRAARG) /* extra argument? */
391− i = ci_func(ci)->l.p->code[currentpc(ci) - 1]; /* get 'real' instruction */
426+ i = ci_func(ci)->p->code[currentpc(ci) - 1]; /* get 'real' instruction */
392427 switch (GET_OPCODE(i)) {
393428 case OP_CALL:
394429 case OP_TAILCALL:
@@ -425,7 +460,10 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { · 30 unmodified lines
425460
426461
427462
428−/* only ANSI way to check whether a pointer points to an array */
463+/*
464+** only ANSI way to check whether a pointer points to an array
465+** (used only for error messages, so efficiency is not a big concern)
466+*/
429467 static int isinstack (CallInfo *ci, const TValue *o) {
430468 StkId p;
431469 for (p = ci->u.l.base; p < ci->top; p++)
@@ -436,7 +474,7 @@ static int isinstack (CallInfo *ci, const TValue *o) { · 4 unmodified lines
436474
437475 static const char *getupvalname (CallInfo *ci, const TValue *o,
438476 const char **name) {
439− LClosure *c = &ci_func(ci)->l;
477+ LClosure *c = ci_func(ci);
440478 int i;
441479 for (i = 0; i < c->nupvalues; i++) {
442480 if (c->upvals[i]->v == o) {
@@ -455,7 +493,7 @@ void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { · 12 unmodified lines
455493 const char *kind = NULL;
456494 if (isLua(ci)) {
457495 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
458− if (!kind && isinstack(ci, o)) /* no? try a register */
496+ if (!kind && isinstack(ci, o)) /* no? try a register */
459497 kind = getobjname(L, ci, cast_int(o - ci->u.l.base), &name);
460498 }
461499 if (kind)
@@ -497,7 +535,7 @@ static void addinfo (lua_State *L, const char *msg) { · 35 unmodified lines
497535 if (isLua(ci)) { /* is Lua code? */
498536 char buff[LUA_IDSIZE]; /* add file:line information */
499537 int line = currentline(ci);
500− TString *src = ci_func(ci)->l.p->source;
538+ TString *src = ci_func(ci)->p->source;
501539 if (src)
502540 luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
503541 else { /* no source available; use "?" instead */
src/ldebug.h+4−1
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldebug.h,v 2.5 2009/06/10 16:57:53 roberto Exp $
2+** $Id: ldebug.h,v 2.6 2011/06/02 19:31:40 roberto Exp $
33 ** Auxiliary functions from Debug Interface module
44 ** See Copyright Notice in lua.h
55 */
@@ -17,6 +17,9 @@ · 11 unmodified lines
1717
1818 #define resethookcount(L) (L->hookcount = L->basehookcount)
1919
20+/* Active Lua function (given call info) */
21+#define ci_func(ci) (clLvalue((ci)->func))
22+
2023
2124 LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o,
2225 const char *opname);
src/ldo.c+64−66
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldo.c,v 2.90 2010/10/25 19:01:37 roberto Exp $
2+** $Id: ldo.c,v 2.95 2011/06/02 19:31:40 roberto Exp $
33 ** Stack and Call structure of Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -293,65 +293,59 @@ static StkId tryfuncTM (lua_State *L, StkId func) { · 287 unmodified lines
293293 ** returns true if function has been executed (C function)
294294 */
295295 int luaD_precall (lua_State *L, StkId func, int nresults) {
296− Closure *cl;
297296 lua_CFunction f;
298− ptrdiff_t funcr;
299− if (!ttisfunction(func)) /* `func' is not a function? */
300− func = tryfuncTM(L, func); /* check the `function' tag method */
301− funcr = savestack(L, func);
302− if (ttislcf(func)) { /* light C function? */
303− f = fvalue(func); /* get it */
304− goto isCfunc; /* go to call it */
305− }
306− cl = clvalue(func);
307− if (cl->c.isC) { /* C closure? */
308− CallInfo *ci;
309− int n;
310− f = cl->c.f;
311− isCfunc: /* call C function 'f' */
312− luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
313− ci = next_ci(L); /* now 'enter' new function */
314− ci->nresults = nresults;
315− ci->func = restorestack(L, funcr);
316− ci->top = L->top + LUA_MINSTACK;
317− lua_assert(ci->top <= L->stack_last);
318− ci->callstatus = 0;
319− if (L->hookmask & LUA_MASKCALL)
320− luaD_hook(L, LUA_HOOKCALL, -1);
321− lua_unlock(L);
322− n = (*f)(L); /* do the actual call */
323− lua_lock(L);
324− api_checknelems(L, n);
325− luaD_poscall(L, L->top - n);
326− return 1;
327− }
328− else { /* Lua function: prepare its call */
329− CallInfo *ci;
330− int nparams, nargs;
331− StkId base;
332− Proto *p = cl->l.p;
333− luaD_checkstack(L, p->maxstacksize);
334− func = restorestack(L, funcr);
335− nargs = cast_int(L->top - func) - 1; /* number of real arguments */
336− nparams = p->numparams; /* number of expected parameters */
337− for (; nargs < nparams; nargs++)
338− setnilvalue(L->top++); /* complete missing arguments */
339− if (!p->is_vararg) /* no varargs? */
340− base = func + 1;
341− else /* vararg function */
342− base = adjust_varargs(L, p, nargs);
343− ci = next_ci(L); /* now 'enter' new function */
344− ci->nresults = nresults;
345− ci->func = func;
346− ci->u.l.base = base;
347− ci->top = base + p->maxstacksize;
348− lua_assert(ci->top <= L->stack_last);
349− ci->u.l.savedpc = p->code; /* starting point */
350− ci->callstatus = CIST_LUA;
351− L->top = ci->top;
352− if (L->hookmask & LUA_MASKCALL)
353− callhook(L, ci);
354− return 0;
297+ CallInfo *ci;
298+ int n; /* number of arguments (Lua) or returns (C) */
299+ ptrdiff_t funcr = savestack(L, func);
300+ switch (ttype(func)) {
301+ case LUA_TLCF: /* light C function */
302+ f = fvalue(func);
303+ goto Cfunc;
304+ case LUA_TCCL: { /* C closure */
305+ f = clCvalue(func)->f;
306+ Cfunc:
307+ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
308+ ci = next_ci(L); /* now 'enter' new function */
309+ ci->nresults = nresults;
310+ ci->func = restorestack(L, funcr);
311+ ci->top = L->top + LUA_MINSTACK;
312+ lua_assert(ci->top <= L->stack_last);
313+ ci->callstatus = 0;
314+ if (L->hookmask & LUA_MASKCALL)
315+ luaD_hook(L, LUA_HOOKCALL, -1);
316+ lua_unlock(L);
317+ n = (*f)(L); /* do the actual call */
318+ lua_lock(L);
319+ api_checknelems(L, n);
320+ luaD_poscall(L, L->top - n);
321+ return 1;
322+ }
323+ case LUA_TLCL: { /* Lua function: prepare its call */
324+ StkId base;
325+ Proto *p = clLvalue(func)->p;
326+ luaD_checkstack(L, p->maxstacksize);
327+ func = restorestack(L, funcr);
328+ n = cast_int(L->top - func) - 1; /* number of real arguments */
329+ for (; n < p->numparams; n++)
330+ setnilvalue(L->top++); /* complete missing arguments */
331+ base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n);
332+ ci = next_ci(L); /* now 'enter' new function */
333+ ci->nresults = nresults;
334+ ci->func = func;
335+ ci->u.l.base = base;
336+ ci->top = base + p->maxstacksize;
337+ lua_assert(ci->top <= L->stack_last);
338+ ci->u.l.savedpc = p->code; /* starting point */
339+ ci->callstatus = CIST_LUA;
340+ L->top = ci->top;
341+ if (L->hookmask & LUA_MASKCALL)
342+ callhook(L, ci);
343+ return 0;
344+ }
345+ default: { /* not a function */
346+ func = tryfuncTM(L, func); /* retry with 'function' tag method */
347+ return luaD_precall(L, func, nresults);
348+ }
355349 }
356350 }
357351
@@ -615,8 +609,8 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, · 257 unmodified lines
615609 */
616610 struct SParser { /* data to `f_parser' */
617611 ZIO *z;
618− Mbuffer buff; /* buffer to be used by the scanner */
619− Varlist varl; /* list of local variables (to be used by the parser) */
612+ Mbuffer buff; /* dynamic structure used by the scanner */
613+ Dyndata dyd; /* dynamic structures used by the parser */
620614 const char *name;
621615 };
622616
@@ -625,14 +619,14 @@ static void f_parser (lua_State *L, void *ud) { · 2 unmodified lines
625619 Proto *tf;
626620 Closure *cl;
627621 struct SParser *p = cast(struct SParser *, ud);
628− int c = luaZ_lookahead(p->z);
622+ int c = zgetc(p->z); /* read first character */
629623 tf = (c == LUA_SIGNATURE[0])
630624 ? luaU_undump(L, p->z, &p->buff, p->name)
631− : luaY_parser(L, p->z, &p->buff, &p->varl, p->name);
625+ : luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
632626 setptvalue2s(L, L->top, tf);
633627 incr_top(L);
634628 cl = luaF_newLclosure(L, tf);
635− setclvalue(L, L->top - 1, cl);
629+ setclLvalue(L, L->top - 1, cl);
636630 for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */
637631 cl->l.upvals[i] = luaF_newupval(L);
638632 }
@@ -643,11 +637,15 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { · 4 unmodified lines
643637 int status;
644638 L->nny++; /* cannot yield during parsing */
645639 p.z = z; p.name = name;
646− p.varl.actvar = NULL; p.varl.nactvar = p.varl.actvarsize = 0;
640+ p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
641+ p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
642+ p.dyd.label.arr = NULL; p.dyd.label.size = 0;
647643 luaZ_initbuffer(L, &p.buff);
648644 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
649645 luaZ_freebuffer(L, &p.buff);
650− luaM_freearray(L, p.varl.actvar, p.varl.actvarsize);
646+ luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
647+ luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
648+ luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
651649 L->nny--;
652650 return status;
653651 }
src/ldump.c+3−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldump.c,v 1.17 2010/10/13 21:04:52 lhf Exp $
2+** $Id: ldump.c,v 1.18 2011/05/06 13:35:17 lhf Exp $
33 ** save precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -69,7 +69,7 @@ static void DumpString(const TString* s, DumpState* D) · 63 unmodified lines
6969 {
7070 size_t size=s->tsv.len+1; /* include trailing '\0' */
7171 DumpVar(size,D);
72− DumpBlock(getstr(s),size,D);
72+ DumpBlock(getstr(s),size*sizeof(char),D);
7373 }
7474 }
7575
@@ -150,7 +150,7 @@ static void DumpFunction(const Proto* f, DumpState* D) · 74 unmodified lines
150150
151151 static void DumpHeader(DumpState* D)
152152 {
153− char h[LUAC_HEADERSIZE];
153+ lu_byte h[LUAC_HEADERSIZE];
154154 luaU_header(h);
155155 DumpBlock(h,LUAC_HEADERSIZE,D);
156156 }
src/lgc.c+79−58
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lgc.c,v 2.103 2010/10/25 19:01:37 roberto Exp $
2+** $Id: lgc.c,v 2.109 2011/05/05 19:42:25 roberto Exp $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -51,7 +51,7 @@ · 45 unmodified lines
5151 ** standard negative debt for GC; a reasonable "time" to wait before
5252 ** starting a new cycle
5353 */
54−#define stddebt(g) (-cast(l_mem, g->totalbytes/100) * g->gcpause)
54+#define stddebt(g) (-cast(l_mem, gettotalbytes(g)/100) * g->gcpause)
5555
5656
5757 /*
@@ -68,11 +68,15 @@ · 10 unmodified lines
6868 #define stringmark(s) ((void)((s) && resetbits((s)->tsv.marked, WHITEBITS)))
6969
7070
71−#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
71+#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT)
7272
7373 #define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n)))
7474
7575
76+#define checkconsistency(obj) \
77+ lua_longassert(!iscollectable(obj) || righttt(obj))
78+
79+
7680 #define markvalue(g,o) { checkconsistency(o); \
7781 if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
7882
@@ -114,12 +118,11 @@ static void removeentry (Node *n) { · 35 unmodified lines
114118 */
115119 static int iscleared (const TValue *o, int iskey) {
116120 if (!iscollectable(o)) return 0;
117− if (ttisstring(o)) {
121+ else if (ttisstring(o)) {
118122 stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
119123 return 0;
120124 }
121− return iswhite(gcvalue(o)) ||
122− (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
125+ else return iswhite(gcvalue(o)) || (!iskey && isfinalized(gcvalue(o)));
123126 }
124127
125128
@@ -319,8 +322,7 @@ static void remarkupvals (global_State *g) { · 193 unmodified lines
319322 ** mark root set and reset all gray lists, to start a new
320323 ** incremental (or full) collection
321324 */
322−static void markroot (lua_State *L) {
323− global_State *g = G(L);
325+static void markroot (global_State *g) {
324326 g->gray = g->grayagain = NULL;
325327 g->weak = g->allweak = g->ephemeron = NULL;
326328 markobject(g, g->mainthread);
@@ -635,6 +637,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { · 308 unmodified lines
635637 int ow = otherwhite(g);
636638 int toclear, toset; /* bits to clear and to set in all live objects */
637639 int tostop; /* stop sweep when this is true */
640+ l_mem debt = g->GCdebt; /* current debt */
638641 if (isgenerational(g)) { /* generational mode? */
639642 toclear = ~0; /* clear nothing */
640643 toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */
@@ -657,13 +660,15 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { · 16 unmodified lines
657660 sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */
658661 if (testbits(marked, tostop)) {
659662 static GCObject *nullp = NULL;
660− return &nullp; /* stop sweeping this list */
663+ p = &nullp; /* stop sweeping this list */
664+ break;
661665 }
662666 /* update marks */
663667 gch(curr)->marked = cast_byte((marked & toclear) | toset);
664668 p = &gch(curr)->next; /* go to next element */
665669 }
666670 }
671+ luaE_setdebt(g, debt); /* sweeping should not change debt */
667672 return p;
668673 }
669674
@@ -687,19 +692,17 @@ static void checkSizes (lua_State *L) { · 17 unmodified lines
687692 }
688693
689694
690−static Udata *udata2finalize (global_State *g) {
695+static GCObject *udata2finalize (global_State *g) {
691696 GCObject *o = g->tobefnz; /* get first element */
692− Udata *u = rawgco2u(o);
693− lua_assert(isfinalized(&u->uv));
694− lua_assert(!isold(o));
695− g->tobefnz = u->uv.next; /* remove it from 'tobefnz' list */
696− u->uv.next = g->allgc; /* return it to 'allgc' list */
697+ lua_assert(isfinalized(o));
698+ g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */
699+ gch(o)->next = g->allgc; /* return it to 'allgc' list */
697700 g->allgc = o;
698− resetbit(u->uv.marked, SEPARATED); /* mark that it is not in 'tobefnz' */
699− resetoldbit(o); /* see MOVE OLD rule */
701+ resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */
702+ lua_assert(!isold(o)); /* see MOVE OLD rule */
700703 if (!keepinvariant(g)) /* not keeping invariant? */
701704 makewhite(g, o); /* "sweep" object */
702− return u;
705+ return o;
703706 }
704707
705708
@@ -711,20 +714,22 @@ static void dothecall (lua_State *L, void *ud) { · 5 unmodified lines
711714
712715 static void GCTM (lua_State *L, int propagateerrors) {
713716 global_State *g = G(L);
714− Udata *udata = udata2finalize(g);
715− const TValue *tm = gfasttm(g, udata->uv.metatable, TM_GC);
717+ const TValue *tm;
718+ TValue v;
719+ setgcovalue(L, &v, udata2finalize(g));
720+ tm = luaT_gettmbyobj(L, &v, TM_GC);
716721 if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */
717722 int status;
718723 lu_byte oldah = L->allowhook;
719− lu_mem oldd = g->GCdebt;
724+ int running = g->gcrunning;
720725 L->allowhook = 0; /* stop debug hooks during GC tag method */
721− stopgc(g); /* avoid GC steps */
726+ g->gcrunning = 0; /* avoid GC steps */
722727 setobj2s(L, L->top, tm); /* push finalizer... */
723− setuvalue(L, L->top+1, udata); /* ... and its argument */
728+ setobj2s(L, L->top + 1, &v); /* ... and its argument */
724729 L->top += 2; /* and (next line) call the finalizer */
725730 status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
726731 L->allowhook = oldah; /* restore hooks */
727− g->GCdebt = oldd; /* restore threshold */
732+ g->gcrunning = running; /* restore state */
728733 if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
729734 if (status == LUA_ERRRUN) { /* is there an error msg.? */
730735 luaO_pushfstring(L, "error in __gc tag method (%s)",
@@ -738,25 +743,25 @@ static void GCTM (lua_State *L, int propagateerrors) { · 7 unmodified lines
738743
739744
740745 /*
741−** move all unreachable udata that need finalization from list 'udgc' to
742−** list 'tobefnz'
746+** move all unreachable objects that need finalization from list 'finobj'
747+** to list 'tobefnz'
743748 */
744749 void luaC_separateudata (lua_State *L, int all) {
745750 global_State *g = G(L);
746− GCObject **p = &g->udgc;
751+ GCObject **p = &g->finobj;
747752 GCObject *curr;
748753 GCObject **lastnext = &g->tobefnz;
749754 /* find last 'next' field in 'tobefnz' list (to add elements in its end) */
750755 while (*lastnext != NULL)
751756 lastnext = &gch(*lastnext)->next;
752757 while ((curr = *p) != NULL) { /* traverse all finalizable objects */
753− lua_assert(gch(curr)->tt == LUA_TUSERDATA && !isfinalized(gco2u(curr)));
758+ lua_assert(!isfinalized(curr));
754759 lua_assert(testbit(gch(curr)->marked, SEPARATED));
755760 if (!(all || iswhite(curr))) /* not being collected? */
756761 p = &gch(curr)->next; /* don't bother with it */
757762 else {
758763 l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */
759− *p = gch(curr)->next; /* remove 'curr' from 'udgc' list */
764+ *p = gch(curr)->next; /* remove 'curr' from 'finobj' list */
760765 gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */
761766 *lastnext = curr;
762767 lastnext = &gch(curr)->next;
@@ -766,23 +771,23 @@ void luaC_separateudata (lua_State *L, int all) { · 3 unmodified lines
766771
767772
768773 /*
769−** if userdata 'u' has a finalizer, remove it from 'allgc' list (must
770−** search the list to find it) and link it in 'udgc' list.
774+** if object 'o' has a finalizer, remove it from 'allgc' list (must
775+** search the list to find it) and link it in 'finobj' list.
771776 */
772−void luaC_checkfinalizer (lua_State *L, Udata *u) {
777+void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
773778 global_State *g = G(L);
774− if (testbit(u->uv.marked, SEPARATED) || /* udata is already separated... */
775− isfinalized(&u->uv) || /* ... or is finalized... */
776− gfasttm(g, u->uv.metatable, TM_GC) == NULL) /* or has no finalizer? */
779+ if (testbit(gch(o)->marked, SEPARATED) || /* obj. is already separated... */
780+ isfinalized(o) || /* ... or is finalized... */
781+ gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */
777782 return; /* nothing to be done */
778− else { /* move 'u' to 'udgc' list */
783+ else { /* move 'o' to 'finobj' list */
779784 GCObject **p;
780− for (p = &g->allgc; *p != obj2gco(u); p = &gch(*p)->next) ;
781− *p = u->uv.next; /* remove 'u' from root list */
782− u->uv.next = g->udgc; /* link it in list 'udgc' */
783− g->udgc = obj2gco(u);
784− l_setbit(u->uv.marked, SEPARATED); /* mark it as such */
785− resetoldbit(obj2gco(u)); /* see MOVE OLD rule */
785+ for (p = &g->allgc; *p != o; p = &gch(*p)->next) ;
786+ *p = gch(o)->next; /* remove 'o' from root list */
787+ gch(o)->next = g->finobj; /* link it in list 'finobj' */
788+ g->finobj = o;
789+ l_setbit(gch(o)->marked, SEPARATED); /* mark it as such */
790+ resetoldbit(o); /* see MOVE OLD rule */
786791 }
787792 }
788793
@@ -808,7 +813,7 @@ void luaC_changemode (lua_State *L, int mode) { · 19 unmodified lines
808813 if (mode == KGC_GEN) { /* change to generational mode */
809814 /* make sure gray lists are consistent */
810815 luaC_runtilstate(L, bitmask(GCSpropagate));
811− g->lastmajormem = g->totalbytes;
816+ g->lastmajormem = gettotalbytes(g);
812817 g->gckind = KGC_GEN;
813818 }
814819 else { /* change to incremental mode */
@@ -823,10 +828,14 @@ void luaC_changemode (lua_State *L, int mode) { · 8 unmodified lines
823828
824829
825830 /*
826−** call all pending finalizers */
831+** call all pending finalizers
832+*/
827833 static void callallpendingfinalizers (lua_State *L, int propagateerrors) {
828834 global_State *g = G(L);
829− while (g->tobefnz) GCTM(L, propagateerrors);
835+ while (g->tobefnz) {
836+ resetoldbit(g->tobefnz);
837+ GCTM(L, propagateerrors);
838+ }
830839 }
831840
832841
@@ -837,8 +846,8 @@ void luaC_freeallobjects (lua_State *L) { · 4 unmodified lines
837846 /* following "white" makes all objects look dead */
838847 g->currentwhite = WHITEBITS;
839848 g->gckind = KGC_NORMAL;
840− sweepwholelist(L, &g->udgc);
841− lua_assert(g->udgc == NULL);
849+ sweepwholelist(L, &g->finobj);
850+ lua_assert(g->finobj == NULL);
842851 sweepwholelist(L, &g->allgc);
843852 lua_assert(g->allgc == NULL);
844853 for (i = 0; i < g->strt.size; i++) /* free all string lists */
@@ -883,7 +892,7 @@ static l_mem singlestep (lua_State *L) { · 38 unmodified lines
883892 switch (g->gcstate) {
884893 case GCSpause: {
885894 if (!isgenerational(g))
886− markroot(L); /* start a new collection */
895+ markroot(g); /* start a new collection */
887896 /* in any case, root must be marked */
888897 lua_assert(!iswhite(obj2gco(g->mainthread))
889898 && !iswhite(gcvalue(&g->l_registry)));
@@ -905,7 +914,7 @@ static l_mem singlestep (lua_State *L) { · 15 unmodified lines
905914 return GCSWEEPCOST;
906915 }
907916 else { /* no more strings to sweep */
908− g->sweepgc = &g->udgc; /* prepare to sweep userdata */
917+ g->sweepgc = &g->finobj; /* prepare to sweep finalizable objects */
909918 g->gcstate = GCSsweepudata;
910919 return 0;
911920 }
@@ -955,15 +964,15 @@ static void generationalcollection (lua_State *L) { · 43 unmodified lines
955964 global_State *g = G(L);
956965 if (g->lastmajormem == 0) { /* signal for another major collection? */
957966 luaC_fullgc(L, 0); /* perform a full regular collection */
958− g->lastmajormem = g->totalbytes; /* update control */
967+ g->lastmajormem = gettotalbytes(g); /* update control */
959968 }
960969 else {
961970 luaC_runtilstate(L, ~bitmask(GCSpause)); /* run complete cycle */
962971 luaC_runtilstate(L, bitmask(GCSpause));
963− if (g->totalbytes > g->lastmajormem/100 * g->gcmajorinc)
972+ if (gettotalbytes(g) > g->lastmajormem/100 * g->gcmajorinc)
964973 g->lastmajormem = 0; /* signal for a major collection */
965974 }
966− g->GCdebt = stddebt(g);
975+ luaE_setdebt(g, stddebt(g));
967976 }
968977
969978
@@ -974,21 +983,33 @@ static void step (lua_State *L) { · 4 unmodified lines
974983 lim -= singlestep(L);
975984 } while (lim > 0 && g->gcstate != GCSpause);
976985 if (g->gcstate != GCSpause)
977− g->GCdebt -= GCSTEPSIZE;
986+ luaE_setdebt(g, g->GCdebt - GCSTEPSIZE);
978987 else
979− g->GCdebt = stddebt(g);
988+ luaE_setdebt(g, stddebt(g));
980989 }
981990
982991
983−void luaC_step (lua_State *L) {
992+/*
993+** performs a basic GC step even if the collector is stopped
994+*/
995+void luaC_forcestep (lua_State *L) {
996+ global_State *g = G(L);
984997 int i;
985− if (isgenerational(G(L))) generationalcollection(L);
998+ if (isgenerational(g)) generationalcollection(L);
986999 else step(L);
987− for (i = 0; i < GCFINALIZENUM && G(L)->tobefnz; i++)
1000+ for (i = 0; i < GCFINALIZENUM && g->tobefnz; i++)
9881001 GCTM(L, 1); /* Call a few pending finalizers */
9891002 }
9901003
9911004
1005+/*
1006+** performs a basic GC step only if collector is running
1007+*/
1008+void luaC_step (lua_State *L) {
1009+ if (G(L)->gcrunning) luaC_forcestep(L);
1010+}
1011+
1012+
9921013 /*
9931014 ** performs a full GC cycle; if "isemergency", does not call
9941015 ** finalizers (which could change stack positions)
@@ -1016,7 +1037,7 @@ void luaC_fullgc (lua_State *L, int isemergency) { · 21 unmodified lines
10161037 luaC_runtilstate(L, bitmask(GCSpropagate));
10171038 }
10181039 g->gckind = origkind;
1019− g->GCdebt = stddebt(g);
1040+ luaE_setdebt(g, stddebt(g));
10201041 if (!isemergency) /* do not run finalizers during emergency GC */
10211042 callallpendingfinalizers(L, 1);
10221043 }
src/lgc.h+8−9
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lgc.h,v 2.44 2010/06/30 14:11:17 roberto Exp $
2+** $Id: lgc.h,v 2.50 2011/01/26 16:30:02 roberto Exp $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -52,10 +52,6 @@ · 46 unmodified lines
5252 #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
5353
5454
55−#define gcstopped(g) ((g)->GCdebt == MIN_LMEM)
56−#define stopgc(g) ((g)->GCdebt = MIN_LMEM)
57−
58−
5955 /*
6056 ** some useful bit tricks
6157 */
@@ -76,8 +72,8 @@ · 14 unmodified lines
7672 #define WHITE0BIT 0 /* object is white (type 0) */
7773 #define WHITE1BIT 1 /* object is white (type 1) */
7874 #define BLACKBIT 2 /* object is black */
79−#define FINALIZEDBIT 3 /* for userdata: has been finalized */
80−#define SEPARATED 4 /* " ": it's in 'udgc' list or in 'tobefnz' */
75+#define FINALIZEDBIT 3 /* object has been separated for finalization */
76+#define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */
8177 #define FIXEDBIT 5 /* object is fixed (should not be collected) */
8278 #define OLDBIT 6 /* object is old (only in generational mode) */
8379 /* bit 7 is currently used by tests (luaL_checkmemory) */
@@ -108,7 +104,9 @@ · 24 unmodified lines
108104 #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
109105
110106
111−#define luaC_checkGC(L) {condchangemem(L); if (G(L)->GCdebt > 0) luaC_step(L);}
107+#define luaC_condGC(L,c) \
108+ {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
109+#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
112110
113111
114112 #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
@@ -130,6 +128,7 @@ · 15 unmodified lines
130128 LUAI_FUNC void luaC_separateudata (lua_State *L, int all);
131129 LUAI_FUNC void luaC_freeallobjects (lua_State *L);
132130 LUAI_FUNC void luaC_step (lua_State *L);
131+LUAI_FUNC void luaC_forcestep (lua_State *L);
133132 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
134133 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
135134 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
@@ -137,7 +136,7 @@ LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, · 1 unmodified line
137136 LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
138137 LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
139138 LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
140−LUAI_FUNC void luaC_checkfinalizer (lua_State *L, Udata *u);
139+LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
141140 LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
142141 LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
143142
src/linit.c+5−5
@@ -1,16 +1,16 @@
11 /*
2−** $Id: linit.c,v 1.30 2010/11/12 15:48:30 roberto Exp $
3−** Initialization of libraries for lua.c and other clients
2+** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $
3+** Initialization of libraries for lua.c and other clients
44 ** See Copyright Notice in lua.h
55 */
66
77
8−/*
8+/*
99 ** If you embed Lua in your program and need to open the standard
1010 ** libraries, call luaL_openlibs in your program. If you need a
1111 ** different set of libraries, copy this file to your project and edit
1212 ** it to suit your needs.
13−*/
13+*/
1414
1515
1616 #define linit_c
@@ -57,7 +57,7 @@ LUALIB_API void luaL_openlibs (lua_State *L) { · 40 unmodified lines
5757 lua_pop(L, 1); /* remove lib */
5858 }
5959 /* add open functions from 'preloadedlibs' into 'package.preload' table */
60− luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
60+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
6161 for (lib = preloadedlibs; lib->func; lib++) {
6262 lua_pushcfunction(L, lib->func);
6363 lua_setfield(L, -2, lib->name);
src/liolib.c+34−57
@@ -1,5 +1,5 @@
11 /*
2−** $Id: liolib.c,v 2.95 2010/11/10 18:05:36 roberto Exp $
2+** $Id: liolib.c,v 2.99 2011/03/03 16:34:46 roberto Exp $
33 ** Standard I/O (and system) library
44 ** See Copyright Notice in lua.h
55 */
@@ -26,28 +26,29 @@ · 20 unmodified lines
2626 ** lua_popen spawns a new process connected to the current one through
2727 ** the file streams.
2828 */
29−#if !defined(lua_popen)
29+#if !defined(lua_popen) /* { */
3030
31−#if defined(LUA_USE_POPEN)
31+#if defined(LUA_USE_POPEN) /* { */
3232
33−#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
34−#define lua_pclose(L,file) ((void)L, pclose(file))
33+#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
34+#define lua_pclose(L,file) ((void)L, pclose(file))
3535
36−#elif defined(LUA_WIN)
36+#elif defined(LUA_WIN) /* }{ */
3737
38−#define lua_popen(L,c,m) ((void)L, _popen(c,m))
39−#define lua_pclose(L,file) ((void)L, _pclose(file))
38+#define lua_popen(L,c,m) ((void)L, _popen(c,m))
39+#define lua_pclose(L,file) ((void)L, _pclose(file))
4040
41−#else
4241
43−#define lua_popen(L,c,m) ((void)((void)c, m), \
44− luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
45−#define lua_pclose(L,file) ((void)((void)L, file), -1)
42+#else /* }{ */
4643
47−#endif
44+#define lua_popen(L,c,m) ((void)((void)c, m), \
45+ luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
46+#define lua_pclose(L,file) ((void)((void)L, file), -1)
4847
49−#endif
5048
49+#endif /* } */
50+
51+#endif /* } */
5152
5253
5354 #define IO_INPUT 1
@@ -57,24 +58,6 @@ · 3 unmodified lines
5758 static const char *const fnames[] = {"input", "output"};
5859
5960
60−static int pushresult (lua_State *L, int i, const char *filename) {
61− int en = errno; /* calls to Lua API may change this value */
62− if (i) {
63− lua_pushboolean(L, 1);
64− return 1;
65− }
66− else {
67− lua_pushnil(L);
68− if (filename)
69− lua_pushfstring(L, "%s: %s", filename, strerror(en));
70− else
71− lua_pushfstring(L, "%s", strerror(en));
72− lua_pushinteger(L, en);
73− return 3;
74− }
75−}
76−
77−
7861 static void fileerror (lua_State *L, int arg, const char *filename) {
7962 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
8063 luaL_argerror(L, arg, lua_tostring(L, -1));
@@ -143,13 +126,8 @@ static int io_noclose (lua_State *L) { · 62 unmodified lines
143126 static int io_pclose (lua_State *L) {
144127 FILE **p = tofilep(L);
145128 int stat = lua_pclose(L, *p);
146− *p = NULL;
147− if (stat == -1) /* error? */
148− return pushresult(L, 0, NULL);
149− else {
150− lua_pushinteger(L, stat);
151− return 1; /* return status */
152− }
129+ *p = NULL; /* mark stream as closed (for GC) */
130+ return luaL_execresult(L, stat);
153131 }
154132
155133
@@ -159,8 +137,8 @@ static int io_pclose (lua_State *L) { · 3 unmodified lines
159137 static int io_fclose (lua_State *L) {
160138 FILE **p = tofilep(L);
161139 int ok = (fclose(*p) == 0);
162− *p = NULL;
163− return pushresult(L, ok, NULL);
140+ *p = NULL; /* mark stream as closed (for GC) */
141+ return luaL_fileresult(L, ok, NULL);
164142 }
165143
166144
@@ -181,8 +159,7 @@ static int io_close (lua_State *L) { · 14 unmodified lines
181159
182160 static int io_gc (lua_State *L) {
183161 FILE *f = *tofilep(L);
184− /* ignore closed files */
185− if (f != NULL)
162+ if (f != NULL) /* ignore closed files */
186163 aux_close(L);
187164 return 0;
188165 }
@@ -205,14 +182,14 @@ static int io_open (lua_State *L) { · 16 unmodified lines
205182 int i = 0;
206183 /* check whether 'mode' matches '[rwa]%+?b?' */
207184 if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL &&
208− (mode[i] != '+' || ++i) && /* skip if char is '+' */
209− (mode[i] != 'b' || ++i) && /* skip if char is 'b' */
185+ (mode[i] != '+' || ++i) && /* skip if char is '+' */
186+ (mode[i] != 'b' || ++i) && /* skip if char is 'b' */
210187 (mode[i] == '\0')))
211188 luaL_error(L, "invalid mode " LUA_QL("%s")
212189 " (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
213190 pf = newfile(L);
214191 *pf = fopen(filename, mode);
215− return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
192+ return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
216193 }
217194
218195
@@ -225,14 +202,14 @@ static int io_popen (lua_State *L) { · 6 unmodified lines
225202 const char *mode = luaL_optstring(L, 2, "r");
226203 FILE **pf = newfile(L);
227204 *pf = lua_popen(L, filename, mode);
228− return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
205+ return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
229206 }
230207
231208
232209 static int io_tmpfile (lua_State *L) {
233210 FILE **pf = newfile(L);
234211 *pf = tmpfile();
235− return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
212+ return (*pf == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
236213 }
237214
238215
@@ -305,7 +282,7 @@ static int io_lines (lua_State *L) { · 66 unmodified lines
305282 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
306283 if (lua_isnil(L, 1)) { /* no file name? */
307284 lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT); /* get default input */
308− lua_replace(L, 1); /* put it at index 1 */
285+ lua_replace(L, 1); /* put it at index 1 */
309286 tofile(L); /* check that it's a valid file handle */
310287 toclose = 0; /* do not close it after iteration */
311288 }
@@ -315,7 +292,7 @@ static int io_lines (lua_State *L) { · 3 unmodified lines
315292 *pf = fopen(filename, "r");
316293 if (*pf == NULL)
317294 fileerror(L, 1, filename);
318− lua_replace(L, 1); /* put file at index 1 */
295+ lua_replace(L, 1); /* put file at index 1 */
319296 toclose = 1; /* close it after iteration */
320297 }
321298 aux_lines(L, toclose);
@@ -443,7 +420,7 @@ static int g_read (lua_State *L, FILE *f, int first) { · 121 unmodified lines
443420 }
444421 }
445422 if (ferror(f))
446− return pushresult(L, 0, NULL);
423+ return luaL_fileresult(L, 0, NULL);
447424 if (!success) {
448425 lua_pop(L, 1); /* remove last result */
449426 lua_pushnil(L); /* push nil instead */
@@ -507,7 +484,7 @@ static int g_write (lua_State *L, FILE *f, int arg) { · 57 unmodified lines
507484 }
508485 }
509486 if (status) return 1; /* file handle already on stack top */
510− else return pushresult(L, status, NULL);
487+ else return luaL_fileresult(L, status, NULL);
511488 }
512489
513490
@@ -517,7 +494,7 @@ static int io_write (lua_State *L) { · 3 unmodified lines
517494
518495
519496 static int f_write (lua_State *L) {
520− FILE * f = tofile(L);
497+ FILE * f = tofile(L);
521498 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
522499 return g_write(L, f, 2);
523500 }
@@ -531,7 +508,7 @@ static int f_seek (lua_State *L) { · 7 unmodified lines
531508 long offset = luaL_optlong(L, 3, 0);
532509 op = fseek(f, offset, mode[op]);
533510 if (op)
534− return pushresult(L, 0, NULL); /* error */
511+ return luaL_fileresult(L, 0, NULL); /* error */
535512 else {
536513 lua_pushinteger(L, ftell(f));
537514 return 1;
@@ -546,18 +523,18 @@ static int f_setvbuf (lua_State *L) { · 8 unmodified lines
546523 int op = luaL_checkoption(L, 2, NULL, modenames);
547524 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
548525 int res = setvbuf(f, NULL, mode[op], sz);
549− return pushresult(L, res == 0, NULL);
526+ return luaL_fileresult(L, res == 0, NULL);
550527 }
551528
552529
553530
554531 static int io_flush (lua_State *L) {
555− return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
532+ return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
556533 }
557534
558535
559536 static int f_flush (lua_State *L) {
560− return pushresult(L, fflush(tofile(L)) == 0, NULL);
537+ return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
561538 }
562539
563540
src/llex.c+13−17
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llex.c,v 2.41 2010/11/18 18:38:44 roberto Exp $
2+** $Id: llex.c,v 2.47 2011/05/03 15:51:16 roberto Exp $
33 ** Lexical Analyzer
44 ** See Copyright Notice in lua.h
55 */
@@ -35,7 +35,7 @@ · 29 unmodified lines
3535 /* ORDER RESERVED */
3636 static const char *const luaX_tokens [] = {
3737 "and", "break", "do", "else", "elseif",
38− "end", "false", "for", "function", "if",
38+ "end", "false", "for", "function", "goto", "if",
3939 "in", "local", "nil", "not", "or", "repeat",
4040 "return", "then", "true", "until", "while",
4141 "..", "...", "==", ">=", "<=", "~=", "<eof>",
@@ -67,7 +67,6 @@ void luaX_init (lua_State *L) { · 25 unmodified lines
6767 for (i=0; i<NUM_RESERVED; i++) {
6868 TString *ts = luaS_new(L, luaX_tokens[i]);
6969 luaS_fix(ts); /* reserved words are never collected */
70− lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
7170 ts->tsv.reserved = cast_byte(i+1); /* reserved word */
7271 }
7372 }
@@ -127,7 +126,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { · 53 unmodified lines
127126 TValue *o; /* entry for `str' */
128127 TString *ts = luaS_newlstr(L, str, l); /* create new string */
129128 setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
130− o = luaH_setstr(L, ls->fs->h, ts);
129+ o = luaH_setstr(L, ls->fs->h, ts);
131130 if (ttisnil(o)) {
132131 setbvalue(o, 1); /* t[string] = true */
133132 luaC_checkGC(L);
@@ -152,9 +151,11 @@ static void inclinenumber (LexState *ls) { · 18 unmodified lines
152151 }
153152
154153
155−void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
154+void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
155+ int firstchar) {
156156 ls->decpoint = '.';
157157 ls->L = L;
158+ ls->current = firstchar;
158159 ls->lookahead.token = TK_EOS; /* no look-ahead token */
159160 ls->z = z;
160161 ls->fs = NULL;
@@ -164,7 +165,6 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { · 3 unmodified lines
164165 ls->envn = luaS_new(L, LUA_ENV); /* create env name */
165166 luaS_fix(ls->envn); /* never collect this name */
166167 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
167− next(ls); /* read first char */
168168 }
169169
170170
@@ -185,7 +185,7 @@ static int check_next (LexState *ls, const char *set) { · 14 unmodified lines
185185 }
186186
187187
188−/*
188+/*
189189 ** change all characters 'from' in buffer to 'to'
190190 */
191191 static void buffreplace (LexState *ls, char from, char to) {
@@ -200,6 +200,9 @@ static void buffreplace (LexState *ls, char from, char to) { · 8 unmodified lines
200200 #define getlocaledecpoint() (localeconv()->decimal_point[0])
201201 #endif
202202
203+
204+#define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e)
205+
203206 /*
204207 ** in case of format error, try to change decimal point separator to
205208 ** the one defined in the current locale and check again
@@ -208,7 +211,7 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { · 2 unmodified lines
208211 char old = ls->decpoint;
209212 ls->decpoint = getlocaledecpoint();
210213 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
211− if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
214+ if (!buff2d(ls->buff, &seminfo->r)) {
212215 /* format error with correct decimal point: no more options */
213216 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
214217 lexerror(ls, "malformed number", TK_NUMBER);
@@ -226,7 +229,7 @@ static void read_numeral (LexState *ls, SemInfo *seminfo) { · 11 unmodified lines
226229 } while (lislalnum(ls->current) || ls->current == '.');
227230 save(ls, '\0');
228231 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
229− if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
232+ if (!buff2d(ls->buff, &seminfo->r)) /* format error? */
230233 trydecpoint(ls, seminfo); /* try to update decimal point separator */
231234 }
232235
@@ -283,13 +286,6 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { · 50 unmodified lines
283286 }
284287
285288
286−static int hexavalue (int c) {
287− if (lisdigit(c)) return c - '0';
288− else if (lisupper(c)) return c - 'A' + 10;
289− else return c - 'a' + 10;
290−}
291−
292−
293289 static int readhexaesc (LexState *ls) {
294290 int c1, c2 = EOZ;
295291 if (!lisxdigit(c1 = next(ls)) || !lisxdigit(c2 = next(ls))) {
@@ -299,7 +295,7 @@ static int readhexaesc (LexState *ls) { · 3 unmodified lines
299295 if (c2 != EOZ) save(ls, c2);
300296 lexerror(ls, "hexadecimal digit expected", TK_STRING);
301297 }
302− return (hexavalue(c1) << 4) + hexavalue(c2);
298+ return (luaO_hexavalue(c1) << 4) + luaO_hexavalue(c2);
303299 }
304300
305301
src/llex.h+7−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llex.h,v 1.65 2010/04/05 16:35:37 roberto Exp $
2+** $Id: llex.h,v 1.70 2011/05/03 15:51:16 roberto Exp $
33 ** Lexical Analyzer
44 ** See Copyright Notice in lua.h
55 */
@@ -13,8 +13,6 @@ · 7 unmodified lines
1313
1414 #define FIRST_RESERVED 257
1515
16−/* maximum length of a reserved word */
17−#define TOKEN_LEN (sizeof("function")/sizeof(char))
1816
1917
2018 /*
@@ -25,7 +23,7 @@ enum RESERVED { · 4 unmodified lines
2523 /* terminal symbols denoted by reserved words */
2624 TK_AND = FIRST_RESERVED, TK_BREAK,
2725 TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
28− TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
26+ TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
2927 TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
3028 /* other terminal symbols */
3129 TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_EOS,
@@ -48,17 +46,19 @@ typedef struct Token { · 16 unmodified lines
4846 } Token;
4947
5048
49+/* state of the lexer plus state of the parser when shared by all
50+ functions */
5151 typedef struct LexState {
5252 int current; /* current character (charint) */
5353 int linenumber; /* input line counter */
5454 int lastline; /* line of last token `consumed' */
5555 Token t; /* current token */
5656 Token lookahead; /* look ahead token */
57− struct FuncState *fs; /* `FuncState' is private to the parser */
57+ struct FuncState *fs; /* current function (parser) */
5858 struct lua_State *L;
5959 ZIO *z; /* input stream */
6060 Mbuffer *buff; /* buffer for tokens */
61− struct Varlist *varl; /* list of all active local variables */
61+ struct Dyndata *dyd; /* dynamic structures used by the parser */
6262 TString *source; /* current source name */
6363 TString *envn; /* environment variable name */
6464 char decpoint; /* locale decimal point */
@@ -67,7 +67,7 @@ typedef struct LexState { · 2 unmodified lines
6767
6868 LUAI_FUNC void luaX_init (lua_State *L);
6969 LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
70− TString *source);
70+ TString *source, int firstchar);
7171 LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
7272 LUAI_FUNC void luaX_next (LexState *ls);
7373 LUAI_FUNC int luaX_lookahead (LexState *ls);
src/llimits.h+15−10
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llimits.h,v 1.84 2010/11/08 16:33:20 roberto Exp $
2+** $Id: llimits.h,v 1.89 2011/05/05 19:43:14 roberto Exp $
33 ** Limits, basic types, and some other `installation-dependent' definitions
44 ** See Copyright Notice in lua.h
55 */
@@ -59,9 +59,12 @@ typedef LUAI_UACNUMBER l_uacNumber; · 53 unmodified lines
5959 /* internal assertions for in-house debugging */
6060 #if defined(lua_assert)
6161 #define check_exp(c,e) (lua_assert(c), (e))
62+/* to avoid problems with conditions too long */
63+#define lua_longassert(c) { if (!(c)) lua_assert(0); }
6264 #else
6365 #define lua_assert(c) /* empty */
6466 #define check_exp(c,e) (e)
67+#define lua_longassert(c) /* empty */
6568 #endif
6669
6770 /*
@@ -87,6 +90,7 @@ typedef LUAI_UACNUMBER l_uacNumber; · 19 unmodified lines
8790 #define cast_byte(i) cast(lu_byte, (i))
8891 #define cast_num(i) cast(lua_Number, (i))
8992 #define cast_int(i) cast(int, (i))
93+#define cast_uchar(i) cast(unsigned char, (i))
9094
9195
9296 /*
@@ -173,6 +177,9 @@ typedef lu_int32 Instruction; · 80 unmodified lines
173177 ** lua_number2integer is a macro to convert lua_Number to lua_Integer.
174178 ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
175179 ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
180+** luai_hashnum is a macro to hash a lua_Number value into an integer.
181+** The hash must be deterministic and give reasonable values for
182+** both small and large values (outside the range of integers).
176183 */
177184
178185 #if defined(MS_ASMTRICK) /* { */
@@ -203,6 +210,10 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 24 unmodified lines
203210 volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
204211 (i) = (t)u.l_p[LUA_IEEEENDIAN]; }
205212
213+#define luai_hashnum(i,n) \
214+ { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
215+ (i) = u.l_p[0] + u.l_p[1]; } /* add double bits for his hash */
216+
206217 #define lua_number2int(i,n) lua_number2int32(i, n, int)
207218 #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
208219 #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
@@ -241,14 +252,8 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 32 unmodified lines
241252 #endif
242253
243254
244−/*
245−** luai_hashnum is a macro do hash a lua_Number value into an integer.
246−** The hash must be deterministic and give reasonable values for
247−** both small and large values (outside the range of integers).
248−** It is used only in ltable.c.
249−*/
250255
251−#if !defined(luai_hashnum) /* { */
256+#if (defined(ltable_c) || defined(luaall_c)) && !defined(luai_hashnum)
252257
253258 #include <float.h>
254259 #include <math.h>
@@ -257,7 +262,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 2 unmodified lines
257262 n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
258263 lua_number2int(i, n); i += e; }
259264
260−#endif /* } */
265+#endif
261266
262267
263268
@@ -275,7 +280,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 11 unmodified lines
275280 #define condchangemem(L) condmovestack(L)
276281 #else
277282 #define condchangemem(L) \
278− ((void)(gcstopped(G(L)) || (luaC_fullgc(L, 0), 1)))
283+ ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
279284 #endif
280285
281286 #endif
src/lmem.c+5−9
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lmem.c,v 1.79 2010/05/05 18:49:56 roberto Exp $
2+** $Id: lmem.c,v 1.81 2010/12/20 19:40:07 roberto Exp $
33 ** Interface to Memory Manager
44 ** See Copyright Notice in lua.h
55 */
@@ -79,14 +79,14 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { · 73 unmodified lines
7979 size_t realosize = (block) ? osize : 0;
8080 lua_assert((realosize == 0) == (block == NULL));
8181 #if defined(HARDMEMTESTS)
82− if (nsize > realosize && !gcstopped(g))
82+ if (nsize > realosize && g->gcrunning)
8383 luaC_fullgc(L, 1); /* force a GC whenever possible */
8484 #endif
8585 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
8686 if (newblock == NULL && nsize > 0) {
8787 api_check(L, nsize > realosize,
8888 "realloc cannot fail when shrinking a block");
89− if (!gcstopped(g)) {
89+ if (g->gcrunning) {
9090 luaC_fullgc(L, 1); /* try to free some memory... */
9191 newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
9292 }
@@ -94,9 +94,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { · 1 unmodified line
9494 luaD_throw(L, LUA_ERRMEM);
9595 }
9696 lua_assert((nsize == 0) == (newblock == NULL));
97− g->totalbytes = (g->totalbytes - realosize) + nsize;
98− if (!gcstopped(g))
99− g->GCdebt += nsize; /* give some credit to garbage collector */
97+ g->GCdebt = (g->GCdebt + nsize) - realosize;
10098 #if defined(TRACEMEM)
10199 { /* auxiliary patch to monitor garbage collection.
102100 ** To plot, gnuplot with following command:
@@ -108,9 +106,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { · 5 unmodified lines
108106 if ((total % 200) == 0) {
109107 if (f == NULL) f = fopen(TRACEMEM, "w");
110108 fprintf(f, "%lu %u %d %d\n", total,
111− g->totalbytes,
112− gcstopped(g) ? 0 : g->GCdebt,
113− g->gcstate * 1000);
109+ g->totalbytes, g->GCdebt, g->gcstate * 1000);
114110 }
115111 }
116112 #endif
src/loadlib.c+43−30
@@ -1,5 +1,5 @@
11 /*
2−** $Id: loadlib.c,v 1.94 2010/11/10 20:00:04 roberto Exp $
2+** $Id: loadlib.c,v 1.98 2011/04/08 19:17:36 roberto Exp $
33 ** Dynamic library loader for Lua
44 ** See Copyright Notice in lua.h
55 **
@@ -164,7 +164,7 @@ static void pusherror (lua_State *L) { · 158 unmodified lines
164164 int error = GetLastError();
165165 char buffer[128];
166166 if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
167− NULL, error, 0, buffer, sizeof(buffer), NULL))
167+ NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
168168 lua_pushstring(L, buffer);
169169 else
170170 lua_pushfstring(L, "system error %d\n", error);
@@ -321,8 +321,10 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { · 150 unmodified lines
321321
322322
323323 static const char *searchpath (lua_State *L, const char *name,
324− const char *path) {
325− name = luaL_gsub(L, name, ".", LUA_DIRSEP);
324+ const char *path,
325+ const char *sep) {
326+ if (*sep != '\0') /* non-empty separator? */
327+ name = luaL_gsub(L, name, sep, LUA_DIRSEP); /* replace it by proper one */
326328 lua_pushliteral(L, ""); /* error accumulator */
327329 while ((path = pushnexttemplate(L, path)) != NULL) {
328330 const char *filename = luaL_gsub(L, lua_tostring(L, -1),
@@ -339,7 +341,9 @@ static const char *searchpath (lua_State *L, const char *name, · 10 unmodified lines
339341
340342
341343 static int ll_searchpath (lua_State *L) {
342− const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2));
344+ const char *f = searchpath(L, luaL_checkstring(L, 1),
345+ luaL_checkstring(L, 2),
346+ luaL_optstring(L, 3, "."));
343347 if (f != NULL) return 1;
344348 else { /* error message is on top of the stack */
345349 lua_pushnil(L);
@@ -356,13 +360,19 @@ static const char *findfile (lua_State *L, const char *name, · 10 unmodified lines
356360 path = lua_tostring(L, -1);
357361 if (path == NULL)
358362 luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
359− return searchpath(L, name, path);
363+ return searchpath(L, name, path, ".");
360364 }
361365
362366
363−static void loaderror (lua_State *L, const char *filename) {
364− luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
365− lua_tostring(L, 1), filename, lua_tostring(L, -1));
367+static int checkload (lua_State *L, int stat, const char *filename) {
368+ if (stat) { /* module loaded successfully? */
369+ lua_pushstring(L, filename); /* will be 2nd argument to module */
370+ return 2; /* return open function and file name */
371+ }
372+ else
373+ return luaL_error(L, "error loading module " LUA_QS
374+ " from file " LUA_QS ":\n\t%s",
375+ lua_tostring(L, 1), filename, lua_tostring(L, -1));
366376 }
367377
368378
@@ -370,10 +380,8 @@ static int loader_Lua (lua_State *L) { · 1 unmodified line
370380 const char *filename;
371381 const char *name = luaL_checkstring(L, 1);
372382 filename = findfile(L, name, "path");
373− if (filename == NULL) return 1; /* library not found in this path */
374− if (luaL_loadfile(L, filename) != LUA_OK)
375− loaderror(L, filename);
376− return 1; /* library loaded successfully */
383+ if (filename == NULL) return 1; /* module not found in this path */
384+ return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
377385 }
378386
379387
@@ -398,10 +406,8 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) { · 18 unmodified lines
398406 static int loader_C (lua_State *L) {
399407 const char *name = luaL_checkstring(L, 1);
400408 const char *filename = findfile(L, name, "cpath");
401− if (filename == NULL) return 1; /* library not found in this path */
402− if (loadfunc(L, filename, name) != 0)
403− loaderror(L, filename);
404− return 1; /* library loaded successfully */
409+ if (filename == NULL) return 1; /* module not found in this path */
410+ return checkload(L, (loadfunc(L, filename, name) == 0), filename);
405411 }
406412
407413
@@ -415,12 +421,16 @@ static int loader_Croot (lua_State *L) { · 7 unmodified lines
415421 filename = findfile(L, lua_tostring(L, -1), "cpath");
416422 if (filename == NULL) return 1; /* root not found */
417423 if ((stat = loadfunc(L, filename, name)) != 0) {
418− if (stat != ERRFUNC) loaderror(L, filename); /* real error */
419− lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
420− name, filename);
421− return 1; /* function not found */
424+ if (stat != ERRFUNC)
425+ return checkload(L, 0, filename); /* real error */
426+ else { /* open function not found */
427+ lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
428+ name, filename);
429+ return 1;
430+ }
422431 }
423− return 1;
432+ lua_pushstring(L, filename); /* will be 2nd argument to module */
433+ return 2;
424434 }
425435
426436
@@ -453,16 +463,19 @@ static int ll_require (lua_State *L) { · 26 unmodified lines
453463 luaL_error(L, "module " LUA_QS " not found:%s",
454464 name, lua_tostring(L, -2));
455465 lua_pushstring(L, name);
456− lua_call(L, 1, 1); /* call it */
457− if (lua_isfunction(L, -1)) /* did it find module? */
466+ lua_call(L, 1, 2); /* call it */
467+ if (lua_isfunction(L, -2)) /* did it find module? */
458468 break; /* module loaded successfully */
459− else if (lua_isstring(L, -1)) /* loader returned error message? */
460− lua_concat(L, 2); /* accumulate it */
469+ else if (lua_isstring(L, -2)) { /* loader returned error message? */
470+ lua_pop(L, 1); /* remove extra return */
471+ lua_concat(L, 2); /* accumulate error message */
472+ }
461473 else
462− lua_pop(L, 1);
474+ lua_pop(L, 2); /* remove both returns */
463475 }
464476 lua_pushstring(L, name); /* pass name as argument to module */
465− lua_call(L, 1, 1); /* run loaded module */
477+ lua_insert(L, -2); /* name is 1st argument (before search data) */
478+ lua_call(L, 2, 1); /* run loaded module */
466479 if (!lua_isnil(L, -1)) /* non-nil return? */
467480 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
468481 lua_getfield(L, 2, name);
@@ -639,10 +652,10 @@ LUAMOD_API int luaopen_package (lua_State *L) { · 170 unmodified lines
639652 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
640653 lua_setfield(L, -2, "config");
641654 /* set field `loaded' */
642− luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
655+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
643656 lua_setfield(L, -2, "loaded");
644657 /* set field `preload' */
645− luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
658+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
646659 lua_setfield(L, -2, "preload");
647660 lua_pushglobaltable(L);
648661 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
src/lobject.c+86−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.c,v 2.43 2010/10/29 15:54:55 roberto Exp $
2+** $Id: lobject.c,v 2.49 2011/05/31 18:24:36 roberto Exp $
33 ** Some generic functions over Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -70,28 +70,6 @@ int luaO_ceillog2 (unsigned int x) { · 64 unmodified lines
7070 }
7171
7272
73−int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
74− if (ttype(t1) != ttype(t2)) return 0;
75− else switch (ttype(t1)) {
76− case LUA_TNIL:
77− return 1;
78− case LUA_TNUMBER:
79− return luai_numeq(nvalue(t1), nvalue(t2));
80− case LUA_TBOOLEAN:
81− return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
82− case LUA_TLIGHTUSERDATA:
83− return pvalue(t1) == pvalue(t2);
84− case LUA_TSTRING:
85− return rawtsvalue(t1) == rawtsvalue(t2);
86− case LUA_TLCF:
87− return fvalue(t1) == fvalue(t2);
88− default:
89− lua_assert(iscollectable(t1));
90− return gcvalue(t1) == gcvalue(t2);
91− }
92−}
93−
94−
9573 lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
9674 switch (op) {
9775 case LUA_OPADD: return luai_numadd(NULL, v1, v2);
@@ -106,19 +84,87 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { · 8 unmodified lines
10684 }
10785
10886
109−static int checkend (const char *s, const char *endptr) {
110− if (endptr == s) return 0; /* no characters converted */
111− while (lisspace(cast(unsigned char, *endptr))) endptr++;
112− return (*endptr == '\0'); /* OK if no trailing characters */
87+int luaO_hexavalue (int c) {
88+ if (lisdigit(c)) return c - '0';
89+ else if (lisupper(c)) return c - 'A' + 10;
90+ else return c - 'a' + 10;
91+}
92+
93+
94+#if !defined(lua_strx2number)
95+
96+#include <math.h>
97+
98+
99+static int isneg (const char **s) {
100+ if (**s == '-') { (*s)++; return 1; }
101+ else if (**s == '+') (*s)++;
102+ return 0;
113103 }
114104
115105
116−int luaO_str2d (const char *s, lua_Number *result) {
106+static lua_Number readhexa (const char **s, lua_Number r, int *count) {
107+ while (lisxdigit(cast_uchar(**s))) { /* read integer part */
108+ r = (r * 16.0) + (double)luaO_hexavalue(*(*s)++);
109+ (*count)++;
110+ }
111+ return r;
112+}
113+
114+
115+/*
116+** convert an hexadecimal numeric string to a number, following
117+** C99 specification for 'strtod'
118+*/
119+static lua_Number lua_strx2number (const char *s, char **endptr) {
120+ lua_Number r = 0.0;
121+ int e = 0, i = 0;
122+ int neg = 0; /* 1 if number is negative */
123+ *endptr = cast(char *, s); /* nothing is valid yet */
124+ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
125+ neg = isneg(&s); /* check signal */
126+ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
127+ return 0.0; /* invalid format (no '0x') */
128+ s += 2; /* skip '0x' */
129+ r = readhexa(&s, r, &i); /* read integer part */
130+ if (*s == '.') {
131+ s++; /* skip dot */
132+ r = readhexa(&s, r, &e); /* read fractional part */
133+ }
134+ if (i == 0 && e == 0)
135+ return 0.0; /* invalid format (no digit) */
136+ e *= -4; /* each fractional digit divides value by 2^-4 */
137+ *endptr = cast(char *, s); /* valid up to here */
138+ if (*s == 'p' || *s == 'P') { /* exponent part? */
139+ int exp1 = 0;
140+ int neg1;
141+ s++; /* skip 'p' */
142+ neg1 = isneg(&s); /* signal */
143+ if (!lisdigit(cast_uchar(*s)))
144+ goto ret; /* must have at least one digit */
145+ while (lisdigit(cast_uchar(*s))) /* read exponent */
146+ exp1 = exp1 * 10 + *(s++) - '0';
147+ if (neg1) exp1 = -exp1;
148+ e += exp1;
149+ }
150+ *endptr = cast(char *, s); /* valid up to here */
151+ ret:
152+ if (neg) r = -r;
153+ return ldexp(r, e);
154+}
155+
156+#endif
157+
158+
159+int luaO_str2d (const char *s, size_t len, lua_Number *result) {
117160 char *endptr;
118− *result = lua_str2number(s, &endptr);
119− if (checkend(s, endptr)) return 1; /* conversion OK? */
120− *result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */
121− return checkend(s, endptr);
161+ if (strpbrk(s, "xX")) /* hexa? */
162+ *result = lua_strx2number(s, &endptr);
163+ else
164+ *result = lua_str2number(s, &endptr);
165+ if (endptr == s) return 0; /* nothing recognized */
166+ while (lisspace(cast_uchar(*endptr))) endptr++;
167+ return (endptr == s + len); /* OK if no trailing characters */
122168 }
123169
124170
@@ -196,19 +242,20 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { · 71 unmodified lines
196242 }
197243
198244
245+/* number of chars of a literal string without the ending \0 */
246+#define LL(x) (sizeof(x)/sizeof(char) - 1)
199247
200−#define LL(x) (sizeof(x) - 1)
201248 #define RETS "..."
202249 #define PRE "[string \""
203250 #define POS "\"]"
204251
205−#define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
252+#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
206253
207254 void luaO_chunkid (char *out, const char *source, size_t bufflen) {
208255 size_t l = strlen(source);
209256 if (*source == '=') { /* 'literal' source */
210257 if (l <= bufflen) /* small enough? */
211− memcpy(out, source + 1, l);
258+ memcpy(out, source + 1, l * sizeof(char));
212259 else { /* truncate it */
213260 addstr(out, source + 1, bufflen - 1);
214261 *out = '\0';
@@ -216,11 +263,11 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { · 1 unmodified line
216263 }
217264 else if (*source == '@') { /* file name */
218265 if (l <= bufflen) /* small enough? */
219− memcpy(out, source + 1, l);
266+ memcpy(out, source + 1, l * sizeof(char));
220267 else { /* add '...' before rest of name */
221268 addstr(out, RETS, LL(RETS));
222269 bufflen -= LL(RETS);
223− memcpy(out, source + 1 + l - bufflen, bufflen);
270+ memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
224271 }
225272 }
226273 else { /* string; format as [string "source"] */
@@ -236,6 +283,7 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { · 9 unmodified lines
236283 addstr(out, source, l);
237284 addstr(out, RETS, LL(RETS));
238285 }
239− memcpy(out, POS, LL(POS) + 1);
286+ memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
240287 }
241288 }
289+
src/lobject.h+241−87
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.h,v 2.42 2010/07/26 15:53:23 roberto Exp $
2+** $Id: lobject.h,v 2.60 2011/06/13 14:13:06 roberto Exp $
33 ** Type definitions for Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -23,12 +23,38 @@ · 17 unmodified lines
2323 #define LUA_TUPVAL (LUA_NUMTAGS+1)
2424 #define LUA_TDEADKEY (LUA_NUMTAGS+2)
2525
26+/*
27+** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
28+*/
29+#define LUA_TOTALTAGS (LUA_TUPVAL+2)
30+
31+
32+/*
33+** tags for Tagged Values have the following use of bits:
34+** bits 0-3: actual tag (a LUA_T* value)
35+** bits 4-5: variant bits
36+** bit 6: whether value is collectable
37+*/
2638
2739 /*
28−** Variant tag for light C functions (negative to be considered
29−** non collectable by 'iscollectable')
40+** LUA_TFUNCTION variants:
41+** 0 - Lua function
42+** 1 - light C function
43+** 2 - regular C function (closure)
3044 */
31−#define LUA_TLCF (~0x0F | LUA_TFUNCTION)
45+
46+/* Variant tags for functions */
47+#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
48+#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
49+#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
50+
51+
52+/* Bit mark for collectable types */
53+#define BIT_ISCOLLECTABLE (1 << 6)
54+
55+/* mark a tag as collectable */
56+#define ctb(t) ((t) | BIT_ISCOLLECTABLE)
57+
3258
3359 /*
3460 ** Union of all collectable objects
@@ -55,13 +81,10 @@ typedef struct GCheader { · 20 unmodified lines
5581 /*
5682 ** Union of all Lua values
5783 */
58−typedef union {
59− GCObject *gc; /* collectable objects */
60− void *p; /* light userdata */
61− lua_Number n; /* numbers */
62− int b; /* booleans */
63− lua_CFunction f; /* light C functions */
64−} Value;
84+typedef union Value Value;
85+
86+
87+#define numfield lua_Number n; /* numbers */
6588
6689
6790
@@ -72,125 +95,143 @@ typedef union { · 4 unmodified lines
7295
7396 #define TValuefields Value value_; int tt_
7497
75−typedef struct lua_TValue {
76− TValuefields;
77−} TValue;
98+typedef struct lua_TValue TValue;
7899
79100
80101 /* macro defining a nil value */
81−#define NILCONSTANT {NULL}, LUA_TNIL
102+#define NILCONSTANT {NULL}, LUA_TNIL
82103
83104
84−/*
85−** type tag of a TValue
86−*/
87−#define ttype(o) ((o)->tt_)
105+#define val_(o) ((o)->value_)
106+#define num_(o) (val_(o).n)
88107
89108
90−/*
91−** type tag of a TValue with no variants
92−*/
93−#define ttypenv(o) (ttype(o) & 0x0F)
109+/* raw type tag of a TValue */
110+#define rttype(o) ((o)->tt_)
111+
112+/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
113+#define ttype(o) (rttype(o) & 0x3F)
114+
115+
116+/* type tag of a TValue with no variants (bits 0-3) */
117+#define ttypenv(o) (rttype(o) & 0x0F)
94118
95119
96120 /* Macros to test type */
97−#define ttisnil(o) (ttype(o) == LUA_TNIL)
98−#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
99−#define ttisstring(o) (ttype(o) == LUA_TSTRING)
100−#define ttistable(o) (ttype(o) == LUA_TTABLE)
101−#define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION)
102−#define ttisclosure(o) (ttype(o) == LUA_TFUNCTION)
103−#define ttislcf(o) (ttype(o) == LUA_TLCF)
104−#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
105−#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
106−#define ttisthread(o) (ttype(o) == LUA_TTHREAD)
107−#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
108−#define ttisdeadkey(o) (ttype(o) == LUA_TDEADKEY)
121+#define checktag(o,t) (rttype(o) == (t))
122+#define ttisnumber(o) checktag((o), LUA_TNUMBER)
123+#define ttisnil(o) checktag((o), LUA_TNIL)
124+#define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
125+#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
126+#define ttisstring(o) checktag((o), ctb(LUA_TSTRING))
127+#define ttistable(o) checktag((o), ctb(LUA_TTABLE))
128+#define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION)
129+#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
130+#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
131+#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
132+#define ttislcf(o) checktag((o), LUA_TLCF)
133+#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
134+#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
135+#define ttisdeadkey(o) checktag((o), ctb(LUA_TDEADKEY))
136+
137+#define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
109138
110139 /* Macros to access values */
111−#define gcvalue(o) check_exp(iscollectable(o), (o)->value_.gc)
112−#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value_.p)
113−#define nvalue(o) check_exp(ttisnumber(o), (o)->value_.n)
114−#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value_.gc->ts)
140+#define nvalue(o) check_exp(ttisnumber(o), num_(o))
141+#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
142+#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
143+#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
115144 #define tsvalue(o) (&rawtsvalue(o)->tsv)
116−#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value_.gc->u)
145+#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
117146 #define uvalue(o) (&rawuvalue(o)->uv)
118−#define clvalue(o) check_exp(ttisclosure(o), &(o)->value_.gc->cl)
119−#define fvalue(o) check_exp(ttislcf(o), (o)->value_.f)
120−#define hvalue(o) check_exp(ttistable(o), &(o)->value_.gc->h)
121−#define bvalue(o) check_exp(ttisboolean(o), (o)->value_.b)
122−#define thvalue(o) check_exp(ttisthread(o), &(o)->value_.gc->th)
147+#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
148+#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
149+#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
150+#define fvalue(o) check_exp(ttislcf(o), val_(o).f)
151+#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
152+#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
153+#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
123154
124155 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
125156
126157
127−#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
158+#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
128159
129160
130161 /* Macros for internal tests */
131−#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
132−
133−#define checkconsistency(obj) lua_assert(!iscollectable(obj) || righttt(obj))
162+#define righttt(obj) (ttypenv(obj) == gcvalue(obj)->gch.tt)
134163
135164 #define checkliveness(g,obj) \
136− lua_assert(!iscollectable(obj) || (righttt(obj) && !isdead(g,gcvalue(obj))))
165+ lua_longassert(!iscollectable(obj) || \
166+ (righttt(obj) && !isdead(g,gcvalue(obj))))
137167
138168
139169 /* Macros to set values */
140−#define setnilvalue(obj) ((obj)->tt_=LUA_TNIL)
170+#define settt_(o,t) ((o)->tt_=(t))
141171
142172 #define setnvalue(obj,x) \
143− { TValue *i_o=(obj); i_o->value_.n=(x); i_o->tt_=LUA_TNUMBER; }
173+ { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
144174
145−#define setfvalue(obj,x) \
146− { TValue *i_o=(obj); i_o->value_.f=(x); i_o->tt_=LUA_TLCF; }
175+#define changenvalue(o,x) check_exp(ttisnumber(o), num_(o)=(x))
147176
148−#define changenvalue(o,x) check_exp((o)->tt_==LUA_TNUMBER, (o)->value_.n=(x))
177+#define setnilvalue(obj) settt_(obj, LUA_TNIL)
178+
179+#define setfvalue(obj,x) \
180+ { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
149181
150182 #define setpvalue(obj,x) \
151− { TValue *i_o=(obj); i_o->value_.p=(x); i_o->tt_=LUA_TLIGHTUSERDATA; }
183+ { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
152184
153185 #define setbvalue(obj,x) \
154− { TValue *i_o=(obj); i_o->value_.b=(x); i_o->tt_=LUA_TBOOLEAN; }
186+ { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
187+
188+#define setgcovalue(L,obj,x) \
189+ { TValue *io=(obj); GCObject *i_g=(x); \
190+ val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
155191
156192 #define setsvalue(L,obj,x) \
157− { TValue *i_o=(obj); \
158− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TSTRING; \
159− checkliveness(G(L),i_o); }
193+ { TValue *io=(obj); \
194+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TSTRING)); \
195+ checkliveness(G(L),io); }
160196
161197 #define setuvalue(L,obj,x) \
162− { TValue *i_o=(obj); \
163− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TUSERDATA; \
164− checkliveness(G(L),i_o); }
198+ { TValue *io=(obj); \
199+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
200+ checkliveness(G(L),io); }
165201
166202 #define setthvalue(L,obj,x) \
167− { TValue *i_o=(obj); \
168− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTHREAD; \
169− checkliveness(G(L),i_o); }
203+ { TValue *io=(obj); \
204+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
205+ checkliveness(G(L),io); }
206+
207+#define setclLvalue(L,obj,x) \
208+ { TValue *io=(obj); \
209+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
210+ checkliveness(G(L),io); }
170211
171−#define setclvalue(L,obj,x) \
172− { TValue *i_o=(obj); \
173− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TFUNCTION; \
174− checkliveness(G(L),i_o); }
212+#define setclCvalue(L,obj,x) \
213+ { TValue *io=(obj); \
214+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
215+ checkliveness(G(L),io); }
175216
176217 #define sethvalue(L,obj,x) \
177− { TValue *i_o=(obj); \
178− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTABLE; \
179− checkliveness(G(L),i_o); }
218+ { TValue *io=(obj); \
219+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
220+ checkliveness(G(L),io); }
180221
181222 #define setptvalue(L,obj,x) \
182− { TValue *i_o=(obj); \
183− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TPROTO; \
184− checkliveness(G(L),i_o); }
223+ { TValue *io=(obj); \
224+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TPROTO)); \
225+ checkliveness(G(L),io); }
185226
186−#define setdeadvalue(obj) ((obj)->tt_=LUA_TDEADKEY)
227+#define setdeadvalue(obj) settt_(obj, ctb(LUA_TDEADKEY))
187228
188229
189230
190231 #define setobj(L,obj1,obj2) \
191− { const TValue *o2=(obj2); TValue *o1=(obj1); \
192− o1->value_ = o2->value_; o1->tt_=o2->tt_; \
193− checkliveness(G(L),o1); }
232+ { const TValue *io2=(obj2); TValue *io1=(obj1); \
233+ io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
234+ checkliveness(G(L),io1); }
194235
195236
196237 /*
@@ -214,9 +255,121 @@ typedef struct lua_TValue { · 17 unmodified lines
214255
215256
216257
258+
259+/*
260+** {======================================================
261+** NaN Trick
262+** =======================================================
263+*/
264+
265+#if defined(LUA_NANTRICKLE) || defined(LUA_NANTRICKBE)
266+
267+/*
268+** numbers are represented in the 'd_' field. All other values have the
269+** value (NNMARK | tag) in 'tt_'. A number with such pattern would be
270+** a "signaled NaN", which is never generated by regular operations by
271+** the CPU (nor by 'strtod')
272+*/
273+#if !defined(NNMARK)
274+#define NNMARK 0x7FF7A500
275+#endif
276+
277+#undef TValuefields
278+#if defined(LUA_NANTRICKLE)
279+/* little endian */
280+#define TValuefields \
281+ union { struct { Value v_; int tt_; } i; double d_; } u
282+#else
283+/* big endian */
284+#define TValuefields \
285+ union { struct { int tt_; Value v_; } i; double d_; } u
286+#endif
287+
288+#undef numfield
289+#define numfield /* no such field; numbers are the entire struct */
290+
291+/* basic check to distinguish numbers from non-numbers */
292+#undef ttisnumber
293+#define ttisnumber(o) (((o)->u.i.tt_ & 0x7fffff00) != NNMARK)
294+
295+#define tag2tt(t) (NNMARK | (t))
296+
297+#undef NILCONSTANT
298+#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
299+
300+#undef val_
301+#define val_(o) ((o)->u.i.v_)
302+#undef num_
303+#define num_(o) ((o)->u.d_)
304+
305+#undef rttype
306+#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : (o)->u.i.tt_ & 0xff)
307+
308+#undef settt_
309+#define settt_(o,t) ((o)->u.i.tt_=tag2tt(t))
310+
311+#undef setnvalue
312+#define setnvalue(obj,x) \
313+ { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
314+
315+#undef setobj
316+#define setobj(L,obj1,obj2) \
317+ { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
318+ o1_->u = o2_->u; \
319+ checkliveness(G(L),o1_); }
320+
321+
322+/*
323+** these redefinitions are not mandatory, but these forms are more efficient
324+*/
325+
326+#undef checktag
327+#define checktag(o,t) ((o)->u.i.tt_ == tag2tt(t))
328+
329+#undef ttisequal
330+#define ttisequal(o1,o2) \
331+ (ttisnumber(o1) ? ttisnumber(o2) : ((o1)->u.i.tt_ == (o2)->u.i.tt_))
332+
333+
334+
335+#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
336+
337+
338+#else
339+
340+#define luai_checknum(L,o,c) { /* empty */ }
341+
342+#endif
343+/* }====================================================== */
344+
345+
346+
347+/*
348+** {======================================================
349+** types and prototypes
350+** =======================================================
351+*/
352+
353+
354+union Value {
355+ GCObject *gc; /* collectable objects */
356+ void *p; /* light userdata */
357+ int b; /* booleans */
358+ lua_CFunction f; /* light C functions */
359+ numfield /* numbers */
360+};
361+
362+
363+struct lua_TValue {
364+ TValuefields;
365+};
366+
367+
217368 typedef TValue *StkId; /* index to stack elements */
218369
219370
371+
372+
220373 /*
221374 ** Header for string value; string bytes follow the end of this structure
222375 */
@@ -226,7 +379,7 @@ typedef union TString { · 3 unmodified lines
226379 CommonHeader;
227380 lu_byte reserved;
228381 unsigned int hash;
229− size_t len;
382+ size_t len; /* number of characters in string */
230383 } tsv;
231384 } TString;
232385
@@ -247,7 +400,7 @@ typedef union Udata { · 14 unmodified lines
247400 CommonHeader;
248401 struct Table *metatable;
249402 struct Table *env;
250− size_t len;
403+ size_t len; /* number of bytes */
251404 } uv;
252405 } Udata;
253406
@@ -346,9 +499,9 @@ typedef union Closure { · 92 unmodified lines
346499 } Closure;
347500
348501
349−#define isLfunction(o) (ttisclosure(o) && !clvalue(o)->c.isC)
502+#define isLfunction(o) ttisLclosure(o)
350503
351−#define getproto(o) (clvalue(o)->l.p)
504+#define getproto(o) (clLvalue(o)->p)
352505
353506
354507 /*
@@ -403,12 +556,13 @@ typedef struct Table { · 48 unmodified lines
403556
404557 LUAI_DDEC const TValue luaO_nilobject_;
405558
559+
406560 LUAI_FUNC int luaO_int2fb (unsigned int x);
407561 LUAI_FUNC int luaO_fb2int (int x);
408−LUAI_FUNC int luaO_ceillog2 (lu_int32 x);
562+LUAI_FUNC int luaO_ceillog2 (unsigned int x);
409563 LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
410−LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
411−LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
564+LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
565+LUAI_FUNC int luaO_hexavalue (int c);
412566 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
413567 va_list argp);
414568 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
src/lopcodes.c+5−5
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lopcodes.c,v 1.44 2010/10/13 16:45:54 roberto Exp $
2+** $Id: lopcodes.c,v 1.48 2011/04/19 16:22:13 roberto Exp $
33 ** See Copyright Notice in lua.h
44 */
55
@@ -16,6 +16,7 @@ · 10 unmodified lines
1616 LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
1717 "MOVE",
1818 "LOADK",
19+ "LOADKX",
1920 "LOADBOOL",
2021 "LOADNIL",
2122 "GETUPVAL",
@@ -50,7 +51,6 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { · 28 unmodified lines
5051 "TFORCALL",
5152 "TFORLOOP",
5253 "SETLIST",
53− "CLOSE",
5454 "CLOSURE",
5555 "VARARG",
5656 "EXTRAARG",
@@ -64,8 +64,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { · 7 unmodified lines
6464 /* T A B C mode opcode */
6565 opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
6666 ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
67+ ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */
6768 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
68− ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */
69+ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */
6970 ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
7071 ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */
7172 ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
@@ -88,7 +89,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { · 16 unmodified lines
8889 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
8990 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
9091 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
91− ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */
92+ ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */
9293 ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
9394 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
9495 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
@@ -98,7 +99,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { · 3 unmodified lines
9899 ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */
99100 ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */
100101 ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
101− ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */
102102 ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
103103 ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
104104 ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */
src/lopcodes.h+6−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lopcodes.h,v 1.137 2010/10/25 12:24:55 roberto Exp $
2+** $Id: lopcodes.h,v 1.141 2011/04/19 16:22:13 roberto Exp $
33 ** Opcodes for Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -167,9 +167,10 @@ typedef enum { · 161 unmodified lines
167167 name args description
168168 ------------------------------------------------------------------------*/
169169 OP_MOVE,/* A B R(A) := R(B) */
170−OP_LOADK,/* A Bx R(A) := Kst(Bx - 1) */
170+OP_LOADK,/* A Bx R(A) := Kst(Bx) */
171+OP_LOADKX,/* A R(A) := Kst(extra arg) */
171172 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
172−OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
173+OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
173174 OP_GETUPVAL,/* A B R(A) := UpValue[B] */
174175
175176 OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */
@@ -195,8 +196,7 @@ OP_LEN,/* A B R(A) := length of R(B) */ · 19 unmodified lines
195196
196197 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
197198
198−OP_JMP,/* sBx pc+=sBx */
199−
199+OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */
200200 OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
201201 OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
202202 OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
@@ -217,7 +217,6 @@ OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/ · 14 unmodified lines
217217
218218 OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
219219
220−OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
221220 OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
222221
223222 OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
@@ -244,7 +243,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ · 20 unmodified lines
244243 (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
245244 'instruction' is EXTRAARG(real C).
246245
247− (*) In OP_LOADK, if (Bx == 0) then next 'instruction' is EXTRAARG(real Bx).
246+ (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
248247
249248 (*) For comparisons, A specifies what condition the test should accept
250249 (true or false).
src/loslib.c+11−20
@@ -1,5 +1,5 @@
11 /*
2−** $Id: loslib.c,v 1.32 2010/10/05 12:18:03 roberto Exp $
2+** $Id: loslib.c,v 1.34 2011/03/03 16:34:46 roberto Exp $
33 ** Standard Operating System library
44 ** See Copyright Notice in lua.h
55 */
@@ -59,37 +59,28 @@ · 53 unmodified lines
5959
6060
6161
62−static int os_pushresult (lua_State *L, int i, const char *filename) {
63− int en = errno; /* calls to Lua API may change this value */
64− if (i) {
65− lua_pushboolean(L, 1);
66− return 1;
67− }
62+static int os_execute (lua_State *L) {
63+ const char *cmd = luaL_optstring(L, 1, NULL);
64+ int stat = system(cmd);
65+ if (cmd != NULL)
66+ return luaL_execresult(L, stat);
6867 else {
69− lua_pushnil(L);
70− lua_pushfstring(L, "%s: %s", filename, strerror(en));
71− lua_pushinteger(L, en);
72− return 3;
68+ lua_pushboolean(L, stat); /* true if there is a shell */
69+ return 1;
7370 }
7471 }
7572
7673
77−static int os_execute (lua_State *L) {
78− lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
79− return 1;
80−}
81−
82−
8374 static int os_remove (lua_State *L) {
8475 const char *filename = luaL_checkstring(L, 1);
85− return os_pushresult(L, remove(filename) == 0, filename);
76+ return luaL_fileresult(L, remove(filename) == 0, filename);
8677 }
8778
8879
8980 static int os_rename (lua_State *L) {
9081 const char *fromname = luaL_checkstring(L, 1);
9182 const char *toname = luaL_checkstring(L, 2);
92− return os_pushresult(L, rename(fromname, toname) == 0, fromname);
83+ return luaL_fileresult(L, rename(fromname, toname) == 0, fromname);
9384 }
9485
9586
@@ -182,7 +173,7 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { · 86 unmodified lines
182173 return conv; /* to avoid warnings */
183174 }
184175
185−
176+
186177 static int os_date (lua_State *L) {
187178 const char *s = luaL_optstring(L, 1, "%c");
188179 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
src/lparser.c+320−145
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.c,v 2.92 2010/09/07 19:21:39 roberto Exp $
2+** $Id: lparser.c,v 2.109 2011/05/02 17:33:01 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -41,10 +41,11 @@ · 35 unmodified lines
4141 */
4242 typedef struct BlockCnt {
4343 struct BlockCnt *previous; /* chain */
44− int breaklist; /* list of jumps out of this loop */
45− lu_byte nactvar; /* # active locals outside the breakable structure */
44+ int firstlabel; /* index of first label in this block */
45+ int firstgoto; /* index of first pending goto in this block */
46+ lu_byte nactvar; /* # active locals outside the block */
4647 lu_byte upval; /* true if some variable in the block is an upvalue */
47− lu_byte isbreakable; /* true if `block' is a loop */
48+ lu_byte isloop; /* true if `block' is a loop */
4849 } BlockCnt;
4950
5051
@@ -52,7 +53,7 @@ typedef struct BlockCnt { · 1 unmodified line
5253 /*
5354 ** prototypes for recursive non-terminal functions
5455 */
55−static void chunk (LexState *ls);
56+static void statement (LexState *ls);
5657 static void expr (LexState *ls, expdesc *v);
5758
5859
@@ -66,6 +67,13 @@ static void anchor_token (LexState *ls) { · 7 unmodified lines
6667 }
6768
6869
70+/* semantic error */
71+static void semerror (LexState *ls, const char *msg) {
72+ ls->t.token = 0; /* remove 'near to' from final message */
73+ luaX_syntaxerror(ls, msg);
74+}
75+
76+
6977 static void error_expected (LexState *ls, int token) {
7078 luaX_syntaxerror(ls,
7179 luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
@@ -103,6 +111,7 @@ static void check (LexState *ls, int c) { · 31 unmodified lines
103111 error_expected(ls, c);
104112 }
105113
114+
106115 static void checknext (LexState *ls, int c) {
107116 check(ls, c);
108117 luaX_next(ls);
@@ -167,13 +176,13 @@ static int registerlocalvar (LexState *ls, TString *varname) { · 58 unmodified lines
167176
168177 static void new_localvar (LexState *ls, TString *name) {
169178 FuncState *fs = ls->fs;
170− Varlist *vl = ls->varl;
179+ Dyndata *dyd = ls->dyd;
171180 int reg = registerlocalvar(ls, name);
172− checklimit(fs, vl->nactvar + 1 - fs->firstlocal,
181+ checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal,
173182 MAXVARS, "local variables");
174− luaM_growvector(ls->L, vl->actvar, vl->nactvar + 1,
175− vl->actvarsize, vardesc, MAX_INT, "local variables");
176− vl->actvar[vl->nactvar++].idx = cast(unsigned short, reg);
183+ luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
184+ dyd->actvar.size, Vardesc, MAX_INT, "local variables");
185+ dyd->actvar.arr[dyd->actvar.n++].idx = cast(unsigned short, reg);
177186 }
178187
179188
@@ -186,7 +195,7 @@ static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { · 6 unmodified lines
186195
187196
188197 static LocVar *getlocvar (FuncState *fs, int i) {
189− int idx = fs->ls->varl->actvar[fs->firstlocal + i].idx;
198+ int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx;
190199 lua_assert(idx < fs->nlocvars);
191200 return &fs->f->locvars[idx];
192201 }
@@ -202,7 +211,7 @@ static void adjustlocalvars (LexState *ls, int nvars) { · 9 unmodified lines
202211
203212
204213 static void removevars (FuncState *fs, int tolevel) {
205− fs->ls->varl->nactvar -= (fs->nactvar - tolevel);
214+ fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
206215 while (fs->nactvar > tolevel)
207216 getlocvar(fs, --fs->nactvar)->endpc = fs->pc;
208217 }
@@ -245,12 +254,12 @@ static int searchvar (FuncState *fs, TString *n) { · 36 unmodified lines
245254
246255 /*
247256 Mark block where variable at given level was defined
248− (to emit OP_CLOSE later).
257+ (to emit close instructions later).
249258 */
250259 static void markupval (FuncState *fs, int level) {
251260 BlockCnt *bl = fs->bl;
252− while (bl && bl->nactvar > level) bl = bl->previous;
253− if (bl) bl->upval = 1;
261+ while (bl->nactvar > level) bl = bl->previous;
262+ bl->upval = 1;
254263 }
255264
256265
@@ -327,10 +336,107 @@ static void enterlevel (LexState *ls) { · 70 unmodified lines
327336 #define leavelevel(ls) (G((ls)->L)->nCcalls--)
328337
329338
330−static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
331− bl->breaklist = NO_JUMP;
332− bl->isbreakable = isbreakable;
339+static void closegoto (LexState *ls, int g, Labeldesc *label) {
340+ int i;
341+ FuncState *fs = ls->fs;
342+ Labellist *gl = &ls->dyd->gt;
343+ Labeldesc *gt = &gl->arr[g];
344+ lua_assert(eqstr(gt->name, label->name));
345+ if (gt->nactvar < label->nactvar) {
346+ const char *msg = luaO_pushfstring(ls->L,
347+ "<goto %s> at line %d jumps into the scope of local " LUA_QS,
348+ getstr(gt->name), gt->line, getstr(getlocvar(fs, gt->nactvar)->varname));
349+ semerror(ls, msg);
350+ }
351+ luaK_patchlist(fs, gt->pc, label->pc);
352+ /* remove goto from pending list */
353+ for (i = g; i < gl->n - 1; i++)
354+ gl->arr[i] = gl->arr[i + 1];
355+ gl->n--;
356+}
357+
358+
359+/*
360+** try to close a goto with existing labels; this solves backward jumps
361+*/
362+static int findlabel (LexState *ls, int g) {
363+ int i;
364+ BlockCnt *bl = ls->fs->bl;
365+ Dyndata *dyd = ls->dyd;
366+ Labeldesc *gt = &dyd->gt.arr[g];
367+ /* check labels in current block for a match */
368+ for (i = bl->firstlabel; i < dyd->label.n; i++) {
369+ Labeldesc *lb = &dyd->label.arr[i];
370+ if (eqstr(lb->name, gt->name)) { /* correct label? */
371+ if (gt->nactvar > lb->nactvar &&
372+ (bl->upval || dyd->label.n > bl->firstlabel))
373+ luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
374+ closegoto(ls, g, lb); /* close it */
375+ return 1;
376+ }
377+ }
378+ return 0; /* label not found; cannot close goto */
379+}
380+
381+
382+static int newlabelentry (LexState *ls, Labellist *l, TString *name,
383+ int line, int pc) {
384+ int n = l->n;
385+ luaM_growvector(ls->L, l->arr, n, l->size, Labeldesc, MAX_INT, "labels");
386+ l->arr[n].name = name;
387+ l->arr[n].line = line;
388+ l->arr[n].nactvar = ls->fs->nactvar;
389+ l->arr[n].pc = pc;
390+ l->n++;
391+ return n;
392+}
393+
394+
395+/*
396+** check whether new label 'lb' matches any pending gotos in current
397+** block; solves forward jumps
398+*/
399+static void findgotos (LexState *ls, Labeldesc *lb) {
400+ Labellist *gl = &ls->dyd->gt;
401+ int i = ls->fs->bl->firstgoto;
402+ while (i < gl->n) {
403+ if (eqstr(gl->arr[i].name, lb->name))
404+ closegoto(ls, i, lb);
405+ else
406+ i++;
407+ }
408+}
409+
410+
411+/*
412+** "export" pending gotos to outer level, to check them against
413+** outer labels; if the block being exited has upvalues, and
414+** the goto exists the scope of any variable (which can be the
415+** upvalue), close those variables being exited.
416+*/
417+static void movegotosout (FuncState *fs, BlockCnt *bl) {
418+ int i = bl->firstgoto;
419+ Labellist *gl = &fs->ls->dyd->gt;
420+ /* correct pending gotos to current block and try to close it
421+ with visible labels */
422+ while (i < gl->n) {
423+ Labeldesc *gt = &gl->arr[i];
424+ if (gt->nactvar > bl->nactvar) {
425+ if (bl->upval)
426+ luaK_patchclose(fs, gt->pc, bl->nactvar);
427+ gt->nactvar = bl->nactvar;
428+ }
429+ if (!findlabel(fs->ls, i))
430+ i++; /* move to next one */
431+ }
432+}
433+
434+
435+static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
436+ bl->isloop = isloop;
333437 bl->nactvar = fs->nactvar;
438+ bl->firstlabel = fs->ls->dyd->label.n;
439+ bl->firstgoto = fs->ls->dyd->gt.n;
334440 bl->upval = 0;
335441 bl->previous = fs->bl;
336442 fs->bl = bl;
@@ -338,17 +444,48 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { · 1 unmodified line
338444 }
339445
340446
447+/*
448+** create a label named "break" to resolve break statements
449+*/
450+static void breaklabel (LexState *ls) {
451+ TString *n = luaS_new(ls->L, "break");
452+ int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc);
453+ findgotos(ls, &ls->dyd->label.arr[l]);
454+}
455+
456+/*
457+** generates an error for an undefined 'goto'; choose appropriate
458+** message when label name is a reserved word (which can only be 'break')
459+*/
460+static void undefgoto (LexState *ls, Labeldesc *gt) {
461+ const char *msg = (gt->name->tsv.reserved > 0)
462+ ? "<%s> at line %d not inside a loop"
463+ : "label " LUA_QS " (<goto> at line %d) undefined";
464+ msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
465+ semerror(ls, msg);
466+}
467+
468+
341469 static void leaveblock (FuncState *fs) {
342470 BlockCnt *bl = fs->bl;
471+ LexState *ls = fs->ls;
472+ if (bl->previous && bl->upval) {
473+ /* create a 'jump to here' to close upvalues */
474+ int j = luaK_jump(fs);
475+ luaK_patchclose(fs, j, bl->nactvar);
476+ luaK_patchtohere(fs, j);
477+ }
478+ if (bl->isloop)
479+ breaklabel(ls); /* close pending breaks */
343480 fs->bl = bl->previous;
344481 removevars(fs, bl->nactvar);
345− if (bl->upval)
346− luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
347− /* a block either controls scope or breaks (never both) */
348− lua_assert(!bl->isbreakable || !bl->upval);
349482 lua_assert(bl->nactvar == fs->nactvar);
350483 fs->freereg = fs->nactvar; /* free registers */
351− luaK_patchtohere(fs, bl->breaklist);
484+ ls->dyd->label.n = bl->firstlabel; /* remove local labels */
485+ if (bl->previous) /* inner block? */
486+ movegotosout(fs, bl); /* update pending gotos to outer block */
487+ else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */
488+ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */
352489 }
353490
354491
@@ -368,10 +505,11 @@ static void codeclosure (LexState *ls, Proto *clp, expdesc *v) { · 13 unmodified lines
368505 f->p[fs->np++] = clp;
369506 luaC_objbarrier(ls->L, f, clp);
370507 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
508+ luaK_exp2nextreg(fs, v); /* fix it at stack top (for GC) */
371509 }
372510
373511
374−static void open_func (LexState *ls, FuncState *fs) {
512+static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
375513 lua_State *L = ls->L;
376514 Proto *f;
377515 fs->prev = ls->fs; /* linked list of funcstates */
@@ -387,7 +525,7 @@ static void open_func (LexState *ls, FuncState *fs) { · 9 unmodified lines
387525 fs->nups = 0;
388526 fs->nlocvars = 0;
389527 fs->nactvar = 0;
390− fs->firstlocal = ls->varl->nactvar;
528+ fs->firstlocal = ls->dyd->actvar.n;
391529 fs->bl = NULL;
392530 f = luaF_newproto(L);
393531 fs->f = f;
@@ -400,6 +538,7 @@ static void open_func (LexState *ls, FuncState *fs) { · 6 unmodified lines
400538 /* anchor table of constants (to avoid being collected) */
401539 sethvalue2s(L, L->top, fs->h);
402540 incr_top(L);
541+ enterblock(fs, bl, 0);
403542 }
404543
405544
@@ -408,7 +547,7 @@ static void close_func (LexState *ls) { · 2 unmodified lines
408547 FuncState *fs = ls->fs;
409548 Proto *f = fs->f;
410549 luaK_ret(fs, 0, 0); /* final return */
411− removevars(fs, 0);
550+ leaveblock(fs);
412551 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
413552 f->sizecode = fs->pc;
414553 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
@@ -435,42 +574,49 @@ static void close_func (LexState *ls) { · 20 unmodified lines
435574 ** opens the main function, which is a regular vararg function with an
436575 ** upvalue named LUA_ENV
437576 */
438−static void open_mainfunc (LexState *ls, FuncState *fs) {
577+static void open_mainfunc (LexState *ls, FuncState *fs, BlockCnt *bl) {
439578 expdesc v;
440− open_func(ls, fs);
579+ open_func(ls, fs, bl);
441580 fs->f->is_vararg = 1; /* main function is always vararg */
442581 init_exp(&v, VLOCAL, 0);
443582 newupvalue(fs, ls->envn, &v); /* create environment upvalue */
444583 }
445584
446585
447−Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Varlist *varl,
448− const char *name) {
449− LexState lexstate;
450− FuncState funcstate;
451− TString *tname = luaS_new(L, name);
452− setsvalue2s(L, L->top, tname); /* push name to protect it */
453− incr_top(L);
454− lexstate.buff = buff;
455− lexstate.varl = varl;
456− luaX_setinput(L, &lexstate, z, tname);
457− open_mainfunc(&lexstate, &funcstate);
458− luaX_next(&lexstate); /* read first token */
459− chunk(&lexstate); /* read main chunk */
460− check(&lexstate, TK_EOS);
461− close_func(&lexstate);
462− L->top--; /* pop name */
463− lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
464− return funcstate.f;
465−}
466−
467−
468586
469587 /*============================================================*/
470588 /* GRAMMAR RULES */
471589 /*============================================================*/
472590
473591
592+/*
593+** check whether current token is in the follow set of a block.
594+** 'until' closes syntactical blocks, but do not close scope,
595+** so it handled in separate.
596+*/
597+static int block_follow (LexState *ls, int withuntil) {
598+ switch (ls->t.token) {
599+ case TK_ELSE: case TK_ELSEIF:
600+ case TK_END: case TK_EOS:
601+ return 1;
602+ case TK_UNTIL: return withuntil;
603+ default: return 0;
604+ }
605+}
606+
607+
608+static void statlist (LexState *ls) {
609+ /* statlist -> { stat [`;'] } */
610+ while (!block_follow(ls, 1)) {
611+ if (ls->t.token == TK_RETURN) {
612+ statement(ls);
613+ return; /* 'return' must be last statement */
614+ }
615+ statement(ls);
616+ }
617+}
618+
619+
474620 static void fieldsel (LexState *ls, expdesc *v) {
475621 /* fieldsel -> ['.' | ':'] NAME */
476622 FuncState *fs = ls->fs;
@@ -586,7 +732,7 @@ static void field (LexState *ls, struct ConsControl *cc) { · 109 unmodified lines
586732
587733
588734 static void constructor (LexState *ls, expdesc *t) {
589− /* constructor -> '{' [ field { sep field } [sep] ] '}'
735+ /* constructor -> '{' [ field { sep field } [sep] ] '}'
590736 sep -> ',' | ';' */
591737 FuncState *fs = ls->fs;
592738 int line = ls->linenumber;
@@ -596,7 +742,7 @@ static void constructor (LexState *ls, expdesc *t) { · 3 unmodified lines
596742 cc.t = t;
597743 init_exp(t, VRELOCABLE, pc);
598744 init_exp(&cc.v, VVOID, 0); /* no value (yet) */
599− luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
745+ luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */
600746 checknext(ls, '{');
601747 do {
602748 lua_assert(cc.v.k == VVOID || cc.tostore > 0);
@@ -643,19 +789,20 @@ static void parlist (LexState *ls) { · 40 unmodified lines
643789 }
644790
645791
646−static void body (LexState *ls, expdesc *e, int needself, int line) {
647− /* body -> `(' parlist `)' chunk END */
792+static void body (LexState *ls, expdesc *e, int ismethod, int line) {
793+ /* body -> `(' parlist `)' block END */
648794 FuncState new_fs;
649− open_func(ls, &new_fs);
795+ BlockCnt bl;
796+ open_func(ls, &new_fs, &bl);
650797 new_fs.f->linedefined = line;
651798 checknext(ls, '(');
652− if (needself) {
653− new_localvarliteral(ls, "self");
799+ if (ismethod) {
800+ new_localvarliteral(ls, "self"); /* create 'self' parameter */
654801 adjustlocalvars(ls, 1);
655802 }
656803 parlist(ls);
657804 checknext(ls, ')');
658− chunk(ls);
805+ statlist(ls);
659806 new_fs.f->lastlinedefined = ls->linenumber;
660807 check_match(ls, TK_END, TK_FUNCTION, line);
661808 codeclosure(ls, new_fs.f, e);
@@ -663,8 +810,8 @@ static void body (LexState *ls, expdesc *e, int needself, int line) { · 1 unmodified line
663810 }
664811
665812
666−static int explist1 (LexState *ls, expdesc *v) {
667− /* explist1 -> expr { `,' expr } */
813+static int explist (LexState *ls, expdesc *v) {
814+ /* explist -> expr { `,' expr } */
668815 int n = 1; /* at least one expression */
669816 expr(ls, v);
670817 while (testnext(ls, ',')) {
@@ -681,12 +828,12 @@ static void funcargs (LexState *ls, expdesc *f, int line) { · 10 unmodified lines
681828 expdesc args;
682829 int base, nparams;
683830 switch (ls->t.token) {
684− case '(': { /* funcargs -> `(' [ explist1 ] `)' */
831+ case '(': { /* funcargs -> `(' [ explist ] `)' */
685832 luaX_next(ls);
686833 if (ls->t.token == ')') /* arg list is empty? */
687834 args.k = VVOID;
688835 else {
689− explist1(ls, &args);
836+ explist(ls, &args);
690837 luaK_setmultret(fs, &args);
691838 }
692839 check_match(ls, ')', '(', line);
@@ -937,23 +1084,12 @@ static void expr (LexState *ls, expdesc *v) { · 244 unmodified lines
9371084 */
9381085
9391086
940−static int block_follow (int token) {
941− switch (token) {
942− case TK_ELSE: case TK_ELSEIF: case TK_END:
943− case TK_UNTIL: case TK_EOS:
944− return 1;
945− default: return 0;
946− }
947−}
948−
949−
9501087 static void block (LexState *ls) {
951− /* block -> chunk */
1088+ /* block -> statlist */
9521089 FuncState *fs = ls->fs;
9531090 BlockCnt bl;
9541091 enterblock(fs, &bl, 0);
955− chunk(ls);
956− lua_assert(bl.breaklist == NO_JUMP);
1092+ statlist(ls);
9571093 leaveblock(fs);
9581094 }
9591095
@@ -1012,10 +1148,10 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { · 52 unmodified lines
10121148 "variable names");
10131149 assignment(ls, &nv, nvars+1);
10141150 }
1015− else { /* assignment -> `=' explist1 */
1151+ else { /* assignment -> `=' explist */
10161152 int nexps;
10171153 checknext(ls, '=');
1018− nexps = explist1(ls, &e);
1154+ nexps = explist(ls, &e);
10191155 if (nexps != nvars) {
10201156 adjust_assign(ls, nvars, nexps, &e);
10211157 if (nexps > nvars)
@@ -1042,19 +1178,38 @@ static int cond (LexState *ls) { · 20 unmodified lines
10421178 }
10431179
10441180
1045−static void breakstat (LexState *ls) {
1181+static void gotostat (LexState *ls, TString *label, int line) {
1182+ /* create new entry for this goto */
1183+ int g = newlabelentry(ls, &ls->dyd->gt, label, line, luaK_jump(ls->fs));
1184+ findlabel(ls, g);
1185+}
1186+
1187+
1188+static void labelstat (LexState *ls, TString *label, int line) {
1189+ /* label -> '@' NAME ':' */
10461190 FuncState *fs = ls->fs;
1047− BlockCnt *bl = fs->bl;
1048− int upval = 0;
1049− while (bl && !bl->isbreakable) {
1050− upval |= bl->upval;
1051− bl = bl->previous;
1191+ Labellist *ll = &ls->dyd->label;
1192+ int l, i; /* index of new label being created */
1193+ /* check for repeated labels on the same block */
1194+ for (i = ls->fs->bl->firstlabel; i < ll->n; i++) {
1195+ if (eqstr(label, ll->arr[i].name)) {
1196+ const char *msg = luaO_pushfstring(ls->L,
1197+ "label " LUA_QS " already defined on line %d",
1198+ getstr(label), ll->arr[i].line);
1199+ semerror(ls, msg);
1200+ }
10521201 }
1053− if (!bl)
1054− luaX_syntaxerror(ls, "no loop to break");
1055− if (upval)
1056− luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
1057− luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
1202+ checknext(ls, ':'); /* skip colon */
1203+ /* create new entry for this label */
1204+ l = newlabelentry(ls, ll, label, line, fs->pc);
1205+ /* skip other no-op statements */
1206+ while (ls->t.token == ';' || ls->t.token == '@')
1207+ statement(ls);
1208+ if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
1209+ /* assume that locals are already out of scope */
1210+ ll->arr[l].nactvar = fs->bl->nactvar;
1211+ }
1212+ findgotos(ls, &ll->arr[l]);
10581213 }
10591214
10601215
@@ -1086,19 +1241,13 @@ static void repeatstat (LexState *ls, int line) { · 25 unmodified lines
10861241 enterblock(fs, &bl1, 1); /* loop block */
10871242 enterblock(fs, &bl2, 0); /* scope block */
10881243 luaX_next(ls); /* skip REPEAT */
1089− chunk(ls);
1244+ statlist(ls);
10901245 check_match(ls, TK_UNTIL, TK_REPEAT, line);
10911246 condexit = cond(ls); /* read condition (inside scope block) */
1092− if (!bl2.upval) { /* no upvalues? */
1093− leaveblock(fs); /* finish scope */
1094− luaK_patchlist(fs, condexit, repeat_init); /* close the loop */
1095− }
1096− else { /* complete semantics when there are upvalues */
1097− breakstat(ls); /* if condition then break */
1098− luaK_patchtohere(ls->fs, condexit); /* else... */
1099− leaveblock(fs); /* finish scope... */
1100− luaK_jumpto(fs, repeat_init); /* and repeat */
1101− }
1247+ if (bl2.upval) /* upvalues? */
1248+ luaK_patchclose(fs, condexit, bl2.nactvar);
1249+ leaveblock(fs); /* finish scope */
1250+ luaK_patchlist(fs, condexit, repeat_init); /* close the loop */
11021251 leaveblock(fs); /* finish loop */
11031252 }
11041253
@@ -1163,7 +1312,7 @@ static void fornum (LexState *ls, TString *varname, int line) { · 58 unmodified lines
11631312
11641313
11651314 static void forlist (LexState *ls, TString *indexname) {
1166− /* forlist -> NAME {,NAME} IN explist1 forbody */
1315+ /* forlist -> NAME {,NAME} IN explist forbody */
11671316 FuncState *fs = ls->fs;
11681317 expdesc e;
11691318 int nvars = 4; /* gen, state, control, plus at least one declared var */
@@ -1181,7 +1330,7 @@ static void forlist (LexState *ls, TString *indexname) { · 11 unmodified lines
11811330 }
11821331 checknext(ls, TK_IN);
11831332 line = ls->linenumber;
1184− adjust_assign(ls, 3, explist1(ls, &e), &e);
1333+ adjust_assign(ls, 3, explist(ls, &e), &e);
11851334 luaK_checkstack(fs, 3); /* extra space to call generator */
11861335 forbody(ls, base, line, nvars - 3, 0);
11871336 }
@@ -1241,19 +1390,20 @@ static void ifstat (LexState *ls, int line) { · 53 unmodified lines
12411390
12421391
12431392 static void localfunc (LexState *ls) {
1244− expdesc v, b;
1393+ expdesc b;
12451394 FuncState *fs = ls->fs;
1246− new_localvar(ls, str_checkname(ls));
1247− init_exp(&v, VLOCAL, fs->freereg);
1248− luaK_reserveregs(fs, 1);
1249− adjustlocalvars(ls, 1);
1250− body(ls, &b, 0, ls->linenumber);
1251− luaK_storevar(fs, &v, &b);
1395+ int varidx = fs->nactvar; /* index of new local variable */
1396+ new_localvar(ls, str_checkname(ls)); /* new local variable */
1397+ adjustlocalvars(ls, 1); /* enter its scope */
1398+ body(ls, &b, 0, ls->linenumber); /* function created in next register */
1399+ lua_assert(b.k == VNONRELOC && b.u.ind.idx == varidx);
1400+ /* debug information will only see the variable after this point! */
1401+ getlocvar(fs, varidx)->startpc = fs->pc;
12521402 }
12531403
12541404
12551405 static void localstat (LexState *ls) {
1256− /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
1406+ /* stat -> LOCAL NAME {`,' NAME} [`=' explist] */
12571407 int nvars = 0;
12581408 int nexps;
12591409 expdesc e;
@@ -1262,7 +1412,7 @@ static void localstat (LexState *ls) { · 2 unmodified lines
12621412 nvars++;
12631413 } while (testnext(ls, ','));
12641414 if (testnext(ls, '='))
1265− nexps = explist1(ls, &e);
1415+ nexps = explist(ls, &e);
12661416 else {
12671417 e.k = VVOID;
12681418 nexps = 0;
@@ -1274,25 +1424,25 @@ static void localstat (LexState *ls) { · 5 unmodified lines
12741424
12751425 static int funcname (LexState *ls, expdesc *v) {
12761426 /* funcname -> NAME {fieldsel} [`:' NAME] */
1277− int needself = 0;
1427+ int ismethod = 0;
12781428 singlevar(ls, v);
12791429 while (ls->t.token == '.')
12801430 fieldsel(ls, v);
12811431 if (ls->t.token == ':') {
1282− needself = 1;
1432+ ismethod = 1;
12831433 fieldsel(ls, v);
12841434 }
1285− return needself;
1435+ return ismethod;
12861436 }
12871437
12881438
12891439 static void funcstat (LexState *ls, int line) {
12901440 /* funcstat -> FUNCTION funcname body */
1291− int needself;
1441+ int ismethod;
12921442 expdesc v, b;
12931443 luaX_next(ls); /* skip FUNCTION */
1294− needself = funcname(ls, &v);
1295− body(ls, &b, needself, line);
1444+ ismethod = funcname(ls, &v);
1445+ body(ls, &b, ismethod, line);
12961446 luaK_storevar(ls->fs, &v, &b);
12971447 luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
12981448 }
@@ -1313,14 +1463,14 @@ static void exprstat (LexState *ls) { · 14 unmodified lines
13131463
13141464
13151465 static void retstat (LexState *ls) {
1316− /* stat -> RETURN explist */
1466+ /* stat -> RETURN [explist] [';'] */
13171467 FuncState *fs = ls->fs;
13181468 expdesc e;
13191469 int first, nret; /* registers with returned values */
1320− if (block_follow(ls->t.token) || ls->t.token == ';')
1470+ if (block_follow(ls, 1) || ls->t.token == ';')
13211471 first = nret = 0; /* return no values */
13221472 else {
1323− nret = explist1(ls, &e); /* optional return values */
1473+ nret = explist(ls, &e); /* optional return values */
13241474 if (hasmultret(e.k)) {
13251475 luaK_setmultret(fs, &e);
13261476 if (e.k == VCALL && nret == 1) { /* tail call? */
@@ -1341,41 +1491,43 @@ static void retstat (LexState *ls) { · 14 unmodified lines
13411491 }
13421492 }
13431493 luaK_ret(fs, first, nret);
1494+ testnext(ls, ';'); /* skip optional semicolon */
13441495 }
13451496
13461497
1347−static int statement (LexState *ls) {
1498+static void statement (LexState *ls) {
13481499 int line = ls->linenumber; /* may be needed for error messages */
1500+ enterlevel(ls);
13491501 switch (ls->t.token) {
13501502 case ';': { /* stat -> ';' (empty statement) */
13511503 luaX_next(ls); /* skip ';' */
1352− return 0;
1504+ break;
13531505 }
13541506 case TK_IF: { /* stat -> ifstat */
13551507 ifstat(ls, line);
1356− return 0;
1508+ break;
13571509 }
13581510 case TK_WHILE: { /* stat -> whilestat */
13591511 whilestat(ls, line);
1360− return 0;
1512+ break;
13611513 }
13621514 case TK_DO: { /* stat -> DO block END */
13631515 luaX_next(ls); /* skip DO */
13641516 block(ls);
13651517 check_match(ls, TK_END, TK_DO, line);
1366− return 0;
1518+ break;
13671519 }
13681520 case TK_FOR: { /* stat -> forstat */
13691521 forstat(ls, line);
1370− return 0;
1522+ break;
13711523 }
13721524 case TK_REPEAT: { /* stat -> repeatstat */
13731525 repeatstat(ls, line);
1374− return 0;
1526+ break;
13751527 }
13761528 case TK_FUNCTION: { /* stat -> funcstat */
13771529 funcstat(ls, line);
1378− return 0;
1530+ break;
13791531 }
13801532 case TK_LOCAL: { /* stat -> localstat */
13811533 luaX_next(ls); /* skip LOCAL */
@@ -1383,39 +1535,62 @@ static int statement (LexState *ls) { · 1 unmodified line
13831535 localfunc(ls);
13841536 else
13851537 localstat(ls);
1386− return 0;
1538+ break;
1539+ }
1540+ case '@': { /* stat -> label */
1541+ luaX_next(ls); /* skip '@' */
1542+ labelstat(ls, str_checkname(ls), line);
1543+ break;
13871544 }
13881545 case TK_RETURN: { /* stat -> retstat */
13891546 luaX_next(ls); /* skip RETURN */
13901547 retstat(ls);
1391− return 1; /* must be last statement */
1548+ break;
13921549 }
13931550 case TK_BREAK: { /* stat -> breakstat */
13941551 luaX_next(ls); /* skip BREAK */
1395− breakstat(ls);
1396− return 1; /* must be last statement */
1552+ /* code it as "goto 'break'" */
1553+ gotostat(ls, luaS_new(ls->L, "break"), line);
1554+ break;
1555+ }
1556+ case TK_GOTO: { /* stat -> 'goto' NAME */
1557+ luaX_next(ls); /* skip GOTO */
1558+ gotostat(ls, str_checkname(ls), line);
1559+ break;
13971560 }
13981561 default: { /* stat -> func | assignment */
13991562 exprstat(ls);
1400− return 0;
1563+ break;
14011564 }
14021565 }
1566+ lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
1567+ ls->fs->freereg >= ls->fs->nactvar);
1568+ ls->fs->freereg = ls->fs->nactvar; /* free registers */
1569+ leavelevel(ls);
14031570 }
14041571
1572+/* }====================================================================== */
14051573
1406−static void chunk (LexState *ls) {
1407− /* chunk -> { stat [`;'] } */
1408− int islast = 0;
1409− enterlevel(ls);
1410− while (!islast && !block_follow(ls->t.token)) {
1411− islast = statement(ls);
1412− if (islast)
1413− testnext(ls, ';');
1414− lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
1415− ls->fs->freereg >= ls->fs->nactvar);
1416− ls->fs->freereg = ls->fs->nactvar; /* free registers */
1417− }
1418− leavelevel(ls);
1574+
1575+Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
1576+ Dyndata *dyd, const char *name, int firstchar) {
1577+ LexState lexstate;
1578+ FuncState funcstate;
1579+ BlockCnt bl;
1580+ TString *tname = luaS_new(L, name);
1581+ setsvalue2s(L, L->top, tname); /* push name to protect it */
1582+ incr_top(L);
1583+ lexstate.buff = buff;
1584+ lexstate.dyd = dyd;
1585+ dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
1586+ luaX_setinput(L, &lexstate, z, tname, firstchar);
1587+ open_mainfunc(&lexstate, &funcstate, &bl);
1588+ luaX_next(&lexstate); /* read first token */
1589+ statlist(&lexstate); /* main body */
1590+ check(&lexstate, TK_EOS);
1591+ close_func(&lexstate);
1592+ L->top--; /* pop name */
1593+ lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
1594+ return funcstate.f;
14191595 }
14201596
1421−/* }====================================================================== */
src/lparser.h+34−11
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.h,v 1.65 2010/07/07 16:27:29 roberto Exp $
2+** $Id: lparser.h,v 1.68 2011/02/23 13:13:10 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -53,19 +53,42 @@ typedef struct expdesc { · 47 unmodified lines
5353 } expdesc;
5454
5555
56−typedef struct vardesc {
57− unsigned short idx;
58−} vardesc;
56+/* description of active local variable */
57+typedef struct Vardesc {
58+ unsigned short idx; /* variable index in stack */
59+} Vardesc;
5960
6061
61−/* list of all active local variables */
62−typedef struct Varlist {
63− vardesc *actvar;
64− int nactvar;
65− int actvarsize;
66−} Varlist;
62+/* description of pending goto statements and label statements */
63+typedef struct Labeldesc {
64+ TString *name; /* label identifier */
65+ int pc; /* position in code */
66+ int line; /* line where it appeared */
67+ lu_byte nactvar; /* local level where it appears in current block */
68+} Labeldesc;
6769
6870
71+/* list of labels or gotos */
72+typedef struct Labellist {
73+ Labeldesc *arr; /* array */
74+ int n; /* number of entries in use */
75+ int size; /* array size */
76+} Labellist;
77+
78+
79+/* dynamic structures used by the parser */
80+typedef struct Dyndata {
81+ struct { /* list of active local variables */
82+ Vardesc *arr;
83+ int n;
84+ int size;
85+ } actvar;
86+ Labellist gt; /* list of pending gotos */
87+ Labellist label; /* list of active labels */
88+} Dyndata;
89+
90+
91+/* control of blocks */
6992 struct BlockCnt; /* defined in lparser.c */
7093
7194
@@ -91,7 +114,7 @@ typedef struct FuncState { · 19 unmodified lines
91114
92115
93116 LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
94− Varlist *varl, const char *name);
117+ Dyndata *dyd, const char *name, int firstchar);
95118
96119
97120 #endif
src/lstate.c+12−8
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstate.c,v 2.86 2010/09/03 14:14:01 roberto Exp $
2+** $Id: lstate.c,v 2.89 2010/12/20 19:40:07 roberto Exp $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -65,11 +65,14 @@ typedef struct LG { · 59 unmodified lines
6565 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
6666
6767
68−
6968 /*
70−** maximum number of nested calls made by error-handling function
69+** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
70+** invariant
7171 */
72−#define LUAI_EXTRACALLS 10
72+void luaE_setdebt (global_State *g, l_mem debt) {
73+ g->totalbytes -= (debt - g->GCdebt);
74+ g->GCdebt = debt;
75+}
7376
7477
7578 CallInfo *luaE_extendCI (lua_State *L) {
@@ -154,7 +157,7 @@ static void f_luaopen (lua_State *L, void *ud) { · 78 unmodified lines
154157 /* pre-create memory-error message */
155158 g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
156159 luaS_fix(g->memerrmsg); /* it should never be collected */
157− g->GCdebt = 0;
160+ g->gcrunning = 1; /* allow gc */
158161 }
159162
160163
@@ -187,7 +190,7 @@ static void close_state (lua_State *L) { · 26 unmodified lines
187190 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
188191 luaZ_freebuffer(L, &g->buff);
189192 freestack(L);
190− lua_assert(g->totalbytes == sizeof(LG));
193+ lua_assert(gettotalbytes(g) == sizeof(LG));
191194 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);
192195 }
193196
@@ -241,7 +244,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { · 47 unmodified lines
241244 g->mainthread = L;
242245 g->uvhead.u.l.prev = &g->uvhead;
243246 g->uvhead.u.l.next = &g->uvhead;
244− stopgc(g); /* no GC while building state */
247+ g->gcrunning = 0; /* no GC while building state */
245248 g->lastmajormem = 0;
246249 g->strt.size = 0;
247250 g->strt.nuse = 0;
@@ -252,11 +255,12 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { · 4 unmodified lines
252255 g->version = lua_version(NULL);
253256 g->gcstate = GCSpause;
254257 g->allgc = NULL;
255− g->udgc = NULL;
258+ g->finobj = NULL;
256259 g->tobefnz = NULL;
257260 g->gray = g->grayagain = NULL;
258261 g->weak = g->ephemeron = g->allweak = NULL;
259262 g->totalbytes = sizeof(LG);
263+ g->GCdebt = 0;
260264 g->gcpause = LUAI_GCPAUSE;
261265 g->gcmajorinc = LUAI_GCMAJOR;
262266 g->gcstepmul = LUAI_GCMUL;
src/lstate.h+11−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstate.h,v 2.68 2010/10/29 17:52:46 roberto Exp $
2+** $Id: lstate.h,v 2.72 2011/06/02 19:31:40 roberto Exp $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -32,9 +32,9 @@ · 26 unmodified lines
3232 ** when traversing the respective threads, but the thread may already be
3333 ** dead, while the upvalue is still accessible through closures.)
3434 **
35−** Userdata with finalizers are kept in the list g->udgc.
35+** Objects with finalizers are kept in the list g->finobj.
3636 **
37−** The list g->tobefnz links all userdata being finalized.
37+** The list g->tobefnz links all objects being finalized.
3838
3939 */
4040
@@ -104,7 +104,6 @@ typedef struct CallInfo { · 63 unmodified lines
104104 #define CIST_TAIL (1<<6) /* call was tail called */
105105
106106
107−#define ci_func(ci) (clvalue((ci)->func))
108107 #define isLua(ci) ((ci)->callstatus & CIST_LUA)
109108
110109
@@ -114,8 +113,8 @@ typedef struct CallInfo { · 3 unmodified lines
114113 typedef struct global_State {
115114 lua_Alloc frealloc; /* function to reallocate memory */
116115 void *ud; /* auxiliary data to `frealloc' */
117− lu_mem totalbytes; /* number of bytes currently allocated */
118− l_mem GCdebt; /* when positive, run a GC step */
116+ lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
117+ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
119118 lu_mem lastmajormem; /* memory in use after last major collection */
120119 stringtable strt; /* hash table for strings */
121120 TValue l_registry;
@@ -123,9 +122,10 @@ typedef struct global_State { · 1 unmodified line
123122 lu_byte currentwhite;
124123 lu_byte gcstate; /* state of garbage collector */
125124 lu_byte gckind; /* kind of GC running */
125+ lu_byte gcrunning; /* true if GC is running */
126126 int sweepstrgc; /* position of sweep in `strt' */
127127 GCObject *allgc; /* list of all collectable objects */
128− GCObject *udgc; /* list of collectable userdata with finalizers */
128+ GCObject *finobj; /* list of collectable objects with finalizers */
129129 GCObject **sweepgc; /* current position of sweep */
130130 GCObject *gray; /* list of gray objects */
131131 GCObject *grayagain; /* list of objects to be traversed atomically */
@@ -209,6 +209,10 @@ union GCObject { · 77 unmodified lines
209209 #define obj2gco(v) (cast(GCObject *, (v)))
210210
211211
212+/* actual number of total bytes allocated */
213+#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)
214+
215+LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
212216 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
213217 LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
214218 LUAI_FUNC void luaE_freeCI (lua_State *L);
src/lstring.c+4−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstring.c,v 2.18 2010/05/10 18:23:45 roberto Exp $
2+** $Id: lstring.c,v 2.19 2011/05/03 16:01:57 roberto Exp $
33 ** String table (keeps all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -84,8 +84,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { · 78 unmodified lines
8484 o != NULL;
8585 o = gch(o)->next) {
8686 TString *ts = rawgco2ts(o);
87− if (h == ts->tsv.hash && ts->tsv.len == l &&
88− (memcmp(str, getstr(ts), l) == 0)) {
87+ if (h == ts->tsv.hash &&
88+ ts->tsv.len == l &&
89+ (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
8990 if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
9091 changewhite(o); /* resurrect it */
9192 return ts;
src/lstrlib.c+51−22
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstrlib.c,v 1.159 2010/11/19 16:25:51 roberto Exp $
2+** $Id: lstrlib.c,v 1.168 2011/06/09 18:22:47 roberto Exp $
33 ** Standard library for string operations and pattern-matching
44 ** See Copyright Notice in lua.h
55 */
@@ -45,7 +45,7 @@ static int str_len (lua_State *L) { · 39 unmodified lines
4545 /* translate a relative string position: negative means back from end */
4646 static size_t posrelat (ptrdiff_t pos, size_t len) {
4747 if (pos >= 0) return (size_t)pos;
48− else if (pos == -pos || (size_t)-pos > len) return 0;
48+ else if (-(size_t)pos > len) return 0;
4949 else return len - ((size_t)-pos) + 1;
5050 }
5151
@@ -101,15 +101,29 @@ static int str_upper (lua_State *L) { · 49 unmodified lines
101101 return 1;
102102 }
103103
104+
105+/* reasonable limit to avoid arithmetic overflow */
106+#define MAXSIZE ((~(size_t)0) >> 1)
107+
104108 static int str_rep (lua_State *L) {
105− size_t l;
106− luaL_Buffer b;
109+ size_t l, lsep;
107110 const char *s = luaL_checklstring(L, 1, &l);
108111 int n = luaL_checkint(L, 2);
109− luaL_buffinit(L, &b);
110− while (n-- > 0)
111− luaL_addlstring(&b, s, l);
112− luaL_pushresult(&b);
112+ const char *sep = luaL_optlstring(L, 3, "", &lsep);
113+ if (n <= 0) lua_pushliteral(L, "");
114+ else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */
115+ return luaL_error(L, "resulting string too large");
116+ else {
117+ size_t totallen = n * l + (n - 1) * lsep;
118+ luaL_Buffer b;
119+ char *p = luaL_buffinitsize(L, &b, totallen);
120+ while (n-- > 1) { /* first n-1 copies (followed by separator) */
121+ memcpy(p, s, l * sizeof(char)); p += l;
122+ memcpy(p, sep, lsep * sizeof(char)); p += lsep;
123+ }
124+ memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
125+ luaL_pushresultsize(&b, totallen);
126+ }
113127 return 1;
114128 }
115129
@@ -125,7 +139,7 @@ static int str_byte (lua_State *L) { · 9 unmodified lines
125139 if (posi > pose) return 0; /* empty interval; return no values */
126140 n = (int)(pose - posi + 1);
127141 if (posi + n <= pose) /* overflow? */
128− luaL_error(L, "string slice too long");
142+ return luaL_error(L, "string slice too long");
129143 luaL_checkstack(L, n, "string slice too long");
130144 for (i=0; i<n; i++)
131145 lua_pushinteger(L, uchar(s[posi+i-1]));
@@ -161,7 +175,7 @@ static int str_dump (lua_State *L) { · 29 unmodified lines
161175 lua_settop(L, 1);
162176 luaL_buffinit(L,&b);
163177 if (lua_dump(L, writer, &b) != 0)
164− luaL_error(L, "unable to dump given function");
178+ return luaL_error(L, "unable to dump given function");
165179 luaL_pushresult(&b);
166180 return 1;
167181 }
@@ -510,7 +524,7 @@ static int nospecials (const char *p, size_t l) { · 342 unmodified lines
510524 size_t upto = 0;
511525 do {
512526 if (strpbrk(p + upto, SPECIALS))
513− return 0; /* pattern has a special character */
527+ return 0; /* pattern has a special character */
514528 upto += strlen(p + upto) + 1; /* may have more after \0 */
515529 } while (upto <= l);
516530 return 1; /* no special chars found */
@@ -729,7 +743,7 @@ static int str_gsub (lua_State *L) { · 212 unmodified lines
729743 ** the previous length
730744 */
731745 #if !defined(LUA_INTFRMLEN) /* { */
732−#if defined(LUA_USELONGLONG)
746+#if defined(LUA_USE_LONGLONG)
733747
734748 #define LUA_INTFRMLEN "ll"
735749 #define LUA_INTFRM_T long long
@@ -794,7 +808,7 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { · 58 unmodified lines
794808 static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
795809 const char *p = strfrmt;
796810 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
797− if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
811+ if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
798812 luaL_error(L, "invalid format (repeated flags)");
799813 if (isdigit(uchar(*p))) p++; /* skip width */
800814 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
@@ -806,7 +820,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { · 5 unmodified lines
806820 if (isdigit(uchar(*p)))
807821 luaL_error(L, "invalid format (width or precision too long)");
808822 *(form++) = '%';
809− memcpy(form, strfrmt, p - strfrmt + 1);
823+ memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
810824 form += p - strfrmt + 1;
811825 *form = '\0';
812826 return p;
@@ -861,6 +875,9 @@ static int str_format (lua_State *L) { · 48 unmodified lines
861875 break;
862876 }
863877 case 'e': case 'E': case 'f':
878+#if defined(LUA_USE_AFORMAT)
879+ case 'a': case 'A':
880+#endif
864881 case 'g': case 'G': {
865882 addlenmod(form, LUA_FLTFRMLEN);
866883 nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
@@ -872,16 +889,16 @@ static int str_format (lua_State *L) { · 5 unmodified lines
872889 }
873890 case 's': {
874891 size_t l;
875− const char *s = luaL_checklstring(L, arg, &l);
892+ const char *s = luaL_tolstring(L, arg, &l);
876893 if (!strchr(form, '.') && l >= 100) {
877894 /* no precision and string is too long to be formatted;
878895 keep original string */
879− lua_pushvalue(L, arg);
880896 luaL_addvalue(&b);
881897 break;
882898 }
883899 else {
884900 nb = sprintf(buff, form, s);
901+ lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
885902 break;
886903 }
887904 }
@@ -900,6 +917,15 @@ static int str_format (lua_State *L) { · 12 unmodified lines
900917 /* }====================================================== */
901918
902919
920+static int noindex (lua_State *L) {
921+ const char *f = lua_tostring(L, 2);
922+ if (f)
923+ return luaL_error(L, "no field '%s' in strings", f);
924+ else
925+ return luaL_error(L, "no such field in strings");
926+}
927+
928+
903929 static const luaL_Reg strlib[] = {
904930 {"byte", str_byte},
905931 {"char", str_char},
@@ -915,19 +941,22 @@ static const luaL_Reg strlib[] = { · 9 unmodified lines
915941 {"reverse", str_reverse},
916942 {"sub", str_sub},
917943 {"upper", str_upper},
944+ {"__index", noindex},
918945 {NULL, NULL}
919946 };
920947
921948
922949 static void createmetatable (lua_State *L) {
923− lua_createtable(L, 0, 1); /* create metatable for strings */
950+ /* setmetatable("", {__index = string}) */
924951 lua_pushliteral(L, ""); /* dummy string */
925− lua_pushvalue(L, -2);
926− lua_setmetatable(L, -2); /* set string metatable */
952+ lua_createtable(L, 0, 1); /* create metatable for strings */
953+ lua_pushvalue(L, -3); /* set the string library... */
954+ lua_setfield(L, -2, "__index"); /* ...as the __index metamethod */
955+ lua_setmetatable(L, -2); /* set metatable for strings */
927956 lua_pop(L, 1); /* pop dummy string */
928− lua_pushvalue(L, -2); /* string library... */
929− lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
930− lua_pop(L, 1); /* pop metatable */
957+ /* setmetatable(string, string) */
958+ lua_pushvalue(L, -1); /* push string library */
959+ lua_setmetatable(L, -2); /* set it as its own metatable */
931960 }
932961
933962
src/ltable.c+10−8
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltable.c,v 2.53 2010/11/11 15:38:43 roberto Exp $
2+** $Id: ltable.c,v 2.59 2011/06/09 18:23:27 roberto Exp $
33 ** Lua tables (hash)
44 ** See Copyright Notice in lua.h
55 */
@@ -33,6 +33,7 @@ · 27 unmodified lines
3333 #include "lstate.h"
3434 #include "lstring.h"
3535 #include "ltable.h"
36+#include "lvm.h"
3637
3738
3839 /*
@@ -87,8 +88,9 @@ static Node *hashnum (const Table *t, lua_Number n) { · 48 unmodified lines
8788 int i;
8889 luai_hashnum(i, n);
8990 if (i < 0) {
90− i = -i; /* must be a positive value */
91− if (i < 0) i = 0; /* handle INT_MIN */
91+ if ((unsigned int)i == -(unsigned int)i)
92+ i = 0; /* handle INT_MIN */
93+ i = -i; /* must be a positive value */
9294 }
9395 return hashmod(t, i);
9496 }
@@ -148,8 +150,8 @@ static int findindex (lua_State *L, Table *t, StkId key) { · 53 unmodified lines
148150 Node *n = mainposition(t, key);
149151 do { /* check whether `key' is somewhere in the chain */
150152 /* key may be dead already, but it is ok to use it in `next' */
151− if (luaO_rawequalObj(gkey(n), key) ||
152− (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
153+ if (luaV_rawequalobj(gkey(n), key) ||
154+ (ttisdeadkey(gkey(n)) && iscollectable(key) &&
153155 gcvalue(gkey(n)) == gcvalue(key))) {
154156 i = cast_int(n - gnode(t, 0)); /* key index in hash table */
155157 /* hash elements are numbered after array ones */
@@ -336,7 +338,7 @@ void luaH_resizearray (lua_State *L, Table *t, int nasize) { · 180 unmodified lines
336338
337339 static void rehash (lua_State *L, Table *t, const TValue *ek) {
338340 int nasize, na;
339− int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
341+ int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */
340342 int i;
341343 int totaluse;
342344 for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
@@ -467,7 +469,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { · 124 unmodified lines
467469 ** main search function
468470 */
469471 const TValue *luaH_get (Table *t, const TValue *key) {
470− switch (ttype(key)) {
472+ switch (ttypenv(key)) {
471473 case LUA_TNIL: return luaO_nilobject;
472474 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
473475 case LUA_TNUMBER: {
@@ -481,7 +483,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { · 7 unmodified lines
481483 default: {
482484 Node *n = mainposition(t, key);
483485 do { /* check whether `key' is somewhere in the chain */
484− if (luaO_rawequalObj(gkey(n), key))
486+ if (luaV_rawequalobj(gkey(n), key))
485487 return gval(n); /* that's it */
486488 else n = gnext(n);
487489 } while (n);
src/ltablib.c+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltablib.c,v 1.58 2010/11/23 17:21:14 roberto Exp $
2+** $Id: ltablib.c,v 1.59 2010/12/17 12:15:34 roberto Exp $
33 ** Library for Table Manipulation
44 ** See Copyright Notice in lua.h
55 */
@@ -17,7 +17,7 @@ · 11 unmodified lines
1717
1818
1919 #define aux_getn(L,n) \
20− (luaL_checktype(L, n, LUA_TTABLE), (int)lua_rawlen(L, n))
20+ (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
2121
2222
2323 static int deprecatedfunc (lua_State *L) {
@@ -104,7 +104,7 @@ static int tconcat (lua_State *L) { · 80 unmodified lines
104104 const char *sep = luaL_optlstring(L, 2, "", &lsep);
105105 luaL_checktype(L, 1, LUA_TTABLE);
106106 i = luaL_optint(L, 3, 1);
107− last = luaL_opt(L, luaL_checkint, 4, (int)lua_rawlen(L, 1));
107+ last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));
108108 luaL_buffinit(L, &b);
109109 for (; i < last; i++) {
110110 addfield(L, &b, i);
@@ -143,7 +143,7 @@ static int unpack (lua_State *L) { · 32 unmodified lines
143143 int i, e, n;
144144 luaL_checktype(L, 1, LUA_TTABLE);
145145 i = luaL_optint(L, 2, 1);
146− e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1));
146+ e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
147147 if (i > e) return 0; /* empty range */
148148 n = e - i + 1; /* number of elements */
149149 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
src/ltm.c+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltm.c,v 2.12 2010/04/13 20:48:12 roberto Exp $
2+** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp $
33 ** Tag methods
44 ** See Copyright Notice in lua.h
55 */
@@ -21,11 +21,11 @@ · 15 unmodified lines
2121
2222 static const char udatatypename[] = "userdata";
2323
24−LUAI_DDEF const char *const luaT_typenames_[] = {
24+LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
2525 "no value",
2626 "nil", "boolean", udatatypename, "number",
2727 "string", "table", "function", udatatypename, "thread",
28− "proto", "upval"
28+ "proto", "upval" /* these last two cases are used for tests only */
2929 };
3030
3131
@@ -62,7 +62,7 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { · 30 unmodified lines
6262
6363 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
6464 Table *mt;
65− switch (ttype(o)) {
65+ switch (ttypenv(o)) {
6666 case LUA_TTABLE:
6767 mt = hvalue(o)->metatable;
6868 break;
src/ltm.h+2−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltm.h,v 2.10 2010/04/13 20:48:12 roberto Exp $
2+** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 roberto Exp $
33 ** Tag methods
44 ** See Copyright Notice in lua.h
55 */
@@ -46,7 +46,7 @@ typedef enum { · 40 unmodified lines
4646 #define ttypename(x) luaT_typenames_[(x) + 1]
4747 #define objtypename(x) ttypename(ttypenv(x))
4848
49−LUAI_DDEC const char *const luaT_typenames_[];
49+LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
5050
5151
5252 LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
src/lua.c+10−11
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.c,v 1.194 2010/10/25 19:01:37 roberto Exp $
2+** $Id: lua.c,v 1.199 2011/05/26 16:09:40 roberto Exp $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -106,13 +106,11 @@ static void laction (int i) { · 100 unmodified lines
106106
107107
108108 static void print_usage (const char *badoption) {
109− if (badoption[1] == 'e' || badoption[1] == 'l') {
110− luai_writestringerror("%s: ", progname);
109+ luai_writestringerror("%s: ", progname);
110+ if (badoption[1] == 'e' || badoption[1] == 'l')
111111 luai_writestringerror("'%s' needs argument\n", badoption);
112− } else {
113− luai_writestringerror("%s: ", progname);
112+ else
114113 luai_writestringerror("unrecognized option '%s'\n", badoption);
115− }
116114 luai_writestringerror(
117115 "usage: %s [options] [script [args]]\n"
118116 "Available options are:\n"
@@ -185,7 +183,8 @@ static int docall (lua_State *L, int narg, int nres) { · 66 unmodified lines
185183
186184
187185 static void print_version (void) {
188− printf("%s\n", LUA_COPYRIGHT);
186+ luai_writestring(LUA_COPYRIGHT, sizeof(LUA_COPYRIGHT));
187+ luai_writeline();
189188 }
190189
191190
@@ -247,14 +246,14 @@ static const char *get_prompt (lua_State *L, int firstline) { · 55 unmodified lines
247246 }
248247
249248 /* mark in error messages for incomplete statements */
250−#define mark "<eof>"
251−#define marklen (sizeof(mark) - 1)
249+#define EOFMARK "<eof>"
250+#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
252251
253252 static int incomplete (lua_State *L, int status) {
254253 if (status == LUA_ERRSYNTAX) {
255254 size_t lmsg;
256255 const char *msg = lua_tolstring(L, -1, &lmsg);
257− if (lmsg >= marklen && strcmp(msg + lmsg - marklen, mark) == 0) {
256+ if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
258257 lua_pop(L, 1);
259258 return 1;
260259 }
@@ -322,7 +321,7 @@ static void dotty (lua_State *L) { · 61 unmodified lines
322321 }
323322 }
324323 lua_settop(L, 0); /* clear stack */
325− luai_writestring("\n", 1);
324+ luai_writeline();
326325 progname = oldprogname;
327326 }
328327
src/lua.h+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.h,v 1.276 2010/10/26 19:32:19 roberto Exp $
2+** $Id: lua.h,v 1.277 2011/04/18 14:15:48 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
@@ -18,12 +18,12 @@ · 12 unmodified lines
1818
1919 #define LUA_VERSION_MAJOR "5"
2020 #define LUA_VERSION_MINOR "2"
21−#define LUA_VERSION_RELEASE "0" " (alpha)"
21+#define LUA_VERSION_RELEASE "0" " (beta)"
2222
2323 #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
2424 #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
2525 #define LUA_VERSION_NUM 502
26−#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2010 Lua.org, PUC-Rio"
26+#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2011 Lua.org, PUC-Rio"
2727 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
2828
2929
@@ -415,7 +415,7 @@ struct lua_Debug { · 385 unmodified lines
415415
416416
417417 /******************************************************************************
418−* Copyright (C) 1994-2010 Lua.org, PUC-Rio. All rights reserved.
418+* Copyright (C) 1994-2011 Lua.org, PUC-Rio. All rights reserved.
419419 *
420420 * Permission is hereby granted, free of charge, to any person obtaining
421421 * a copy of this software and associated documentation files (the
src/luac.c+17−12
@@ -1,5 +1,5 @@
11 /*
2−** $Id: luac.c,v 1.65 2010/10/26 09:07:52 lhf Exp $
2+** $Id: luac.c,v 1.66 2011/05/10 01:08:57 lhf Exp $
33 ** Lua compiler (saves bytecodes to files; also list bytecodes)
44 ** See Copyright Notice in lua.h
55 */
@@ -143,9 +143,12 @@ static const Proto* combine(lua_State* L, int n) · 137 unmodified lines
143143 int i=n;
144144 if (lua_load(L,reader,&i,"=(" PROGNAME ")")!=LUA_OK) fatal(lua_tostring(L,-1));
145145 f=toproto(L,-1);
146− for (i=0; i<n; i++) f->p[i]=toproto(L,i-n-1);
146+ for (i=0; i<n; i++)
147+ {
148+ f->p[i]=toproto(L,i-n-1);
149+ if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
150+ }
147151 f->sizelineinfo=0;
148− f->sizeupvalues=0;
149152 return f;
150153 }
151154 }
@@ -200,7 +203,7 @@ int main(int argc, char* argv[]) · 48 unmodified lines
200203 }
201204
202205 /*
203−** $Id: print.c,v 1.66 2010/10/26 09:07:52 lhf Exp $
206+** $Id: print.c,v 1.67 2011/05/06 13:37:15 lhf Exp $
204207 ** print bytecodes
205208 ** See Copyright Notice in lua.h
206209 */
@@ -270,6 +273,7 @@ static void PrintConstant(const Proto* f, int i) · 63 unmodified lines
270273 }
271274
272275 #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
276+#define MYK(x) (-1-(x))
273277
274278 static void PrintCode(const Proto* f)
275279 {
@@ -293,23 +297,25 @@ static void PrintCode(const Proto* f) · 17 unmodified lines
293297 {
294298 case iABC:
295299 printf("%d",a);
296− if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
297− if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
300+ if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b);
301+ if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c);
298302 break;
299303 case iABx:
300− if (getBMode(o)==OpArgK) printf("%d %d",a,-bx); else printf("%d %d",a,bx);
304+ printf("%d",a);
305+ if (getBMode(o)==OpArgK) printf(" %d",MYK(bx));
306+ if (getBMode(o)==OpArgU) printf(" %d",bx);
301307 break;
302308 case iAsBx:
303− if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
309+ printf("%d %d",a,sbx);
304310 break;
305311 case iAx:
306− printf("%d",-1-ax);
312+ printf("%d",MYK(ax));
307313 break;
308314 }
309315 switch (o)
310316 {
311317 case OP_LOADK:
312− if (bx>0) { printf("\t; "); PrintConstant(f,bx-1); }
318+ printf("\t; "); PrintConstant(f,bx);
313319 break;
314320 case OP_GETUPVAL:
315321 case OP_SETUPVAL:
@@ -355,8 +361,7 @@ static void PrintCode(const Proto* f) · 39 unmodified lines
355361 printf("\t; %p",VOID(f->p[bx]));
356362 break;
357363 case OP_SETLIST:
358− if (c==0) printf("\t; %d",(int)code[++pc]);
359− else printf("\t; %d",c);
364+ if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c);
360365 break;
361366 case OP_EXTRAARG:
362367 printf("\t; "); PrintConstant(f,ax);
src/luaconf.h+57−6
@@ -1,5 +1,5 @@
11 /*
2−** $Id: luaconf.h,v 1.151 2010/11/12 15:48:30 roberto Exp $
2+** $Id: luaconf.h,v 1.159 2011/06/13 14:13:06 roberto Exp $
33 ** Configuration file for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -35,6 +35,7 @@ · 29 unmodified lines
3535
3636 #if defined(LUA_WIN)
3737 #define LUA_DL_DLL
38+#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
3839 #endif
3940
4041
@@ -43,12 +44,18 @@ · 2 unmodified lines
4344 #define LUA_USE_POSIX
4445 #define LUA_USE_DLOPEN /* needs an extra library: -ldl */
4546 #define LUA_USE_READLINE /* needs some extra libraries */
47+#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */
48+#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
49+#define LUA_USE_LONGLONG /* assume support for long long */
4650 #endif
4751
4852 #if defined(LUA_USE_MACOSX)
4953 #define LUA_USE_POSIX
5054 #define LUA_USE_DLOPEN /* does not need -ldl */
5155 #define LUA_USE_READLINE /* needs an extra library: -lreadline */
56+#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */
57+#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
58+#define LUA_USE_LONGLONG /* assume support for long long */
5259 #endif
5360
5461
@@ -202,10 +209,11 @@ · 147 unmodified lines
202209
203210
204211 /*
205−@@ luai_writestring defines how 'print' prints its results.
212+@@ luai_writestring/luai_writeline define how 'print' prints its results.
206213 */
207214 #include <stdio.h>
208215 #define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
216+#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout))
209217
210218 /*
211219 @@ luai_writestringerror defines how to print error messages.
@@ -253,6 +261,12 @@ · 41 unmodified lines
253261 */
254262 #define LUA_COMPAT_LOG10
255263
264+/*
265+@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base
266+** library. You can rewrite 'loadstring(s)' as 'load(s)'.
267+*/
268+#define LUA_COMPAT_LOADSTRING
269+
256270 /*
257271 @@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library.
258272 */
@@ -371,14 +385,27 @@ · 112 unmodified lines
371385 @@ LUA_NUMBER_FMT is the format for writing numbers.
372386 @@ lua_number2str converts a number to a string.
373387 @@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
374−@@ lua_str2number converts a string to a number.
375388 */
376389 #define LUA_NUMBER_SCAN "%lf"
377390 #define LUA_NUMBER_FMT "%.14g"
378391 #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
379392 #define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
393+
394+
395+/*
396+@@ lua_str2number converts a decimal numeric string to a number.
397+@@ lua_strx2number converts an hexadecimal numeric string to a number.
398+** In C99, 'strtod' do both conversions. C89, however, has no function
399+** to convert floating hexadecimal strings to numbers. For these
400+** systems, you can leave 'lua_strx2number' undefined and Lua will
401+** provide its own implementation.
402+*/
380403 #define lua_str2number(s,p) strtod((s), (p))
381404
405+#if defined(LUA_USE_STRTODHEX)
406+#define lua_strx2number(s,p) strtod((s), (p))
407+#endif
408+
382409
383410 /*
384411 @@ The luai_num* macros define the primitive operations over numbers.
@@ -438,11 +465,11 @@ · 53 unmodified lines
438465
439466 /*
440467 @@ LUA_IEEEENDIAN is the endianness of doubles in your machine
441−@@ (0 for little endian, 1 for big endian); if not defined, Lua will
442−@@ check it dynamically.
468+** (0 for little endian, 1 for big endian); if not defined, Lua will
469+** check it dynamically.
443470 */
444471 /* check for known architectures */
445−#if defined(__i386__) || defined(__i386) || defined(i386) || \
472+#if defined(__i386__) || defined(__i386) || defined(__X86__) || \
446473 defined (__x86_64)
447474 #define LUA_IEEEENDIAN 0
448475 #elif defined(__POWERPC__) || defined(__ppc__)
@@ -458,6 +485,30 @@ · 9 unmodified lines
458485 /* }================================================================== */
459486
460487
488+/*
489+@@ LUA_NANTRICKLE/LUA_NANTRICKBE controls the use of a trick to pack all
490+** types into a single double value, using NaN values to represent
491+** non-number values. The trick only works on 32-bit machines (ints and
492+** pointers are 32-bit values) with numbers represented as IEEE 754-2008
493+** doubles with conventional endianess (12345678 or 87654321), in CPUs
494+** that do not produce signaling NaN values (all NaNs are quiet).
495+*/
496+#if defined(LUA_CORE) /* { */
497+
498+#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
499+
500+/* little-endian architectures that satisfy those conditions */
501+#if defined(__i386__) || defined(__i386) || defined(__X86__)
502+
503+#define LUA_NANTRICKLE
504+
505+#endif
506+
507+#endif /* } */
508+
509+#endif /* } */
510+
511+
461512
462513
463514 /* =================================================================== */
src/lualib.h+2−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lualib.h,v 1.41 2010/10/25 14:32:36 roberto Exp $
2+** $Id: lualib.h,v 1.42 2011/05/25 14:12:28 roberto Exp $
33 ** Lua standard libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -50,7 +50,7 @@ LUALIB_API void (luaL_openlibs) (lua_State *L); · 44 unmodified lines
5050
5151
5252
53−#ifndef lua_assert
53+#if !defined(lua_assert)
5454 #define lua_assert(x) ((void)0)
5555 #endif
5656
src/lundump.c+30−23
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lundump.c,v 1.68 2010/10/26 00:23:46 lhf Exp $
2+** $Id: lundump.c,v 1.69 2011/05/06 13:35:17 lhf Exp $
33 ** load precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -38,6 +38,10 @@ static void error(LoadState* S, const char* why) · 32 unmodified lines
3838 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
3939 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
4040
41+#if !defined(luai_verifycode)
42+#define luai_verifycode(L,b,f) (f)
43+#endif
44+
4145 static void LoadBlock(LoadState* S, void* b, size_t size)
4246 {
4347 if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
@@ -54,6 +58,7 @@ static int LoadInt(LoadState* S) · 10 unmodified lines
5458 {
5559 int x;
5660 LoadVar(S,x);
61+ if (x<0) error(S,"corrupted");
5762 return x;
5863 }
5964
@@ -73,7 +78,7 @@ static TString* LoadString(LoadState* S) · 13 unmodified lines
7378 else
7479 {
7580 char* s=luaZ_openspace(S->L,S->b,size);
76− LoadBlock(S,s,size);
81+ LoadBlock(S,s,size*sizeof(char));
7782 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
7883 }
7984 }
@@ -177,16 +182,17 @@ static Proto* LoadFunction(LoadState* S) · 97 unmodified lines
177182
178183 /* the code below must be consistent with the code in luaU_header */
179184 #define N0 LUAC_HEADERSIZE
180−#define N1 (sizeof(LUA_SIGNATURE)-1)
185+#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
181186 #define N2 N1+2
182187 #define N3 N2+6
183188
184189 static void LoadHeader(LoadState* S)
185190 {
186− char h[LUAC_HEADERSIZE];
187− char s[LUAC_HEADERSIZE];
191+ lu_byte h[LUAC_HEADERSIZE];
192+ lu_byte s[LUAC_HEADERSIZE];
188193 luaU_header(h);
189− LoadBlock(S,s,LUAC_HEADERSIZE);
194+ memcpy(s,h,sizeof(char)); /* first char already read */
195+ LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
190196 if (memcmp(h,s,N0)==0) return;
191197 if (memcmp(h,s,N1)!=0) error(S,"not a");
192198 if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
@@ -209,29 +215,30 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) · 16 unmodified lines
209215 S.Z=Z;
210216 S.b=buff;
211217 LoadHeader(&S);
212− return LoadFunction(&S);
218+ return luai_verifycode(L,buff,LoadFunction(&S));
213219 }
214220
215−/* data to catch conversion errors */
216−#define TAIL "\x19\x93\r\n\x1a\n"
221+#define MYINT(s) (s[0]-'0')
222+#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
223+#define FORMAT 0 /* this is the official format */
217224
218225 /*
219226 * make header for precompiled chunks
220−* if you make any changes in the code below or in LUA_SIGNATURE in lua.h,
221−* be sure to update LoadHeader above and LUAC_HEADERSIZE in lundump.h
227+* if you change the code below be sure to update LoadHeader and FORMAT above
228+* and LUAC_HEADERSIZE in lundump.h
222229 */
223−void luaU_header (char* h)
230+void luaU_header (lu_byte* h)
224231 {
225232 int x=1;
226− memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
227− h+=sizeof(LUA_SIGNATURE)-1;
228− *h++=(char)0x52; /* Lua 5.2 */
229− *h++=(char)0; /* the official format */
230− *h++=(char)*(char*)&x; /* endianness */
231− *h++=(char)sizeof(int);
232− *h++=(char)sizeof(size_t);
233− *h++=(char)sizeof(Instruction);
234− *h++=(char)sizeof(lua_Number);
235− *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
236− memcpy(h,TAIL,sizeof(TAIL)-1);
233+ memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
234+ h+=sizeof(LUA_SIGNATURE)-sizeof(char);
235+ *h++=cast_byte(VERSION);
236+ *h++=cast_byte(FORMAT);
237+ *h++=cast_byte(*(char*)&x); /* endianness */
238+ *h++=cast_byte(sizeof(int));
239+ *h++=cast_byte(sizeof(size_t));
240+ *h++=cast_byte(sizeof(Instruction));
241+ *h++=cast_byte(sizeof(lua_Number));
242+ *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
243+ memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
237244 }
src/lundump.h+7−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lundump.h,v 1.43 2010/10/26 00:23:46 lhf Exp $
2+** $Id: lundump.h,v 1.44 2011/05/06 13:35:17 lhf Exp $
33 ** load precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -14,12 +14,15 @@ · 8 unmodified lines
1414 LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
1515
1616 /* make header; from lundump.c */
17−LUAI_FUNC void luaU_header (char* h);
17+LUAI_FUNC void luaU_header (lu_byte* h);
1818
1919 /* dump one chunk; from ldump.c */
2020 LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);
2121
22−/* size of header of binary files */
23−#define LUAC_HEADERSIZE 18
22+/* data to catch conversion errors */
23+#define LUAC_TAIL "\x19\x93\r\n\x1a\n"
24+
25+/* size in bytes of header of binary files */
26+#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char))
2427
2528 #endif
src/lvm.c+97−53
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.c,v 2.125 2010/10/29 17:52:46 roberto Exp $
2+** $Id: lvm.c,v 2.141 2011/06/09 18:24:22 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -35,7 +35,7 @@ · 29 unmodified lines
3535 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
3636 lua_Number num;
3737 if (ttisnumber(obj)) return obj;
38− if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
38+ if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
3939 setnvalue(n, num);
4040 return n;
4141 }
@@ -65,7 +65,7 @@ static void traceexec (lua_State *L) { · 23 unmodified lines
6565 luaD_hook(L, LUA_HOOKCOUNT, -1);
6666 }
6767 if (mask & LUA_MASKLINE) {
68− Proto *p = ci_func(ci)->l.p;
68+ Proto *p = ci_func(ci)->p;
6969 int npc = pcRel(ci->u.l.savedpc, p);
7070 int newline = getfuncline(p, npc);
7171 if (npc == 0 || /* call linehook when enter a new function, */
@@ -161,7 +161,6 @@ static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, · 89 unmodified lines
161161 if (ttisnil(tm))
162162 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
163163 if (ttisnil(tm)) return 0;
164− if (event == TM_UNM) p2 = luaO_nilobject;
165164 callTM(L, tm, p1, p2, res, 1);
166165 return 1;
167166 }
@@ -175,7 +174,7 @@ static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2, · 7 unmodified lines
175174 if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
176175 tm2 = fasttm(L, mt2, event);
177176 if (tm2 == NULL) return NULL; /* no metamethod */
178− if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
177+ if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
179178 return tm1;
180179 return NULL;
181180 }
@@ -238,9 +237,12 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { · 56 unmodified lines
238237 }
239238
240239
241−int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) {
240+/*
241+** equality of Lua values. L == NULL means raw equality (no metamethods)
242+*/
243+int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) {
242244 const TValue *tm;
243− lua_assert(ttype(t1) == ttype(t2));
245+ lua_assert(ttisequal(t1, t2));
244246 switch (ttype(t1)) {
245247 case LUA_TNIL: return 1;
246248 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
@@ -250,15 +252,19 @@ int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) { · 3 unmodified lines
250252 case LUA_TSTRING: return eqstr(rawtsvalue(t1), rawtsvalue(t2));
251253 case LUA_TUSERDATA: {
252254 if (uvalue(t1) == uvalue(t2)) return 1;
255+ else if (L == NULL) return 0;
253256 tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
254257 break; /* will try TM */
255258 }
256259 case LUA_TTABLE: {
257260 if (hvalue(t1) == hvalue(t2)) return 1;
261+ else if (L == NULL) return 0;
258262 tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
259263 break; /* will try TM */
260264 }
261− default: return gcvalue(t1) == gcvalue(t2);
265+ default:
266+ lua_assert(iscollectable(t1));
267+ return gcvalue(t1) == gcvalue(t2);
262268 }
263269 if (tm == NULL) return 0; /* no TM? */
264270 callTM(L, tm, t1, t2, L->top, 1); /* call TM */
@@ -286,18 +292,20 @@ void luaV_concat (lua_State *L, int total) { · 21 unmodified lines
286292 char *buffer;
287293 int i;
288294 /* collect total length */
289− for (n = 1; n < total && tostring(L, top-n-1); n++) {
290− size_t l = tsvalue(top-n-1)->len;
291− if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
295+ for (i = 1; i < total && tostring(L, top-i-1); i++) {
296+ size_t l = tsvalue(top-i-1)->len;
297+ if (l >= (MAX_SIZET/sizeof(char)) - tl)
298+ luaG_runerror(L, "string length overflow");
292299 tl += l;
293300 }
294301 buffer = luaZ_openspace(L, &G(L)->buff, tl);
295302 tl = 0;
296− for (i=n; i>0; i--) { /* concat all strings */
303+ n = i;
304+ do { /* concat all strings */
297305 size_t l = tsvalue(top-i)->len;
298− memcpy(buffer+tl, svalue(top-i), l);
306+ memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
299307 tl += l;
300− }
308+ } while (--i > 0);
301309 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
302310 }
303311 total -= n-1; /* got 'n' strings to create 1 new */
@@ -308,7 +316,7 @@ void luaV_concat (lua_State *L, int total) { · 4 unmodified lines
308316
309317 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
310318 const TValue *tm;
311− switch (ttype(rb)) {
319+ switch (ttypenv(rb)) {
312320 case LUA_TTABLE: {
313321 Table *h = hvalue(rb);
314322 tm = fasttm(L, h->metatable, TM_LEN);
@@ -327,7 +335,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { · 12 unmodified lines
327335 break;
328336 }
329337 }
330− callTM(L, tm, rb, luaO_nilobject, ra, 1);
338+ callTM(L, tm, rb, rb, ra, 1);
331339 }
332340
333341
@@ -378,7 +386,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, · 44 unmodified lines
378386 Upvaldesc *uv = p->upvalues;
379387 int i;
380388 Closure *ncl = luaF_newLclosure(L, p);
381− setclvalue(L, ra, ncl); /* anchor new closure in stack */
389+ setclLvalue(L, ra, ncl); /* anchor new closure in stack */
382390 for (i = 0; i < nup; i++) { /* fill in its upvalues */
383391 if (uv[i].instack) /* upvalue refers to local variable? */
384392 ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
@@ -454,6 +462,11 @@ void luaV_finishOp (lua_State *L) { · 69 unmodified lines
454462 ** some macros for common tasks in `luaV_execute'
455463 */
456464
465+#if !defined luai_runtimecheck
466+#define luai_runtimecheck(L, c) /* void */
467+#endif
468+
469+
457470 #define RA(i) (base+GETARG_A(i))
458471 /* to be used after possible stack reallocation */
459472 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
@@ -466,12 +479,19 @@ void luaV_finishOp (lua_State *L) { · 6 unmodified lines
466479 (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
467480
468481
469−#define dojump(i) (ci->u.l.savedpc += (i))
482+/* execute a jump instruction */
483+#define dojump(ci,i,e) \
484+ { int a = GETARG_A(i); \
485+ if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
486+ ci->u.l.savedpc += GETARG_sBx(i) + e; }
487+
488+/* for test instructions, execute the jump instruction that follows it */
489+#define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
470490
471491
472492 #define Protect(x) { {x;}; base = ci->u.l.base; }
473493
474−#define checkGC(L) Protect(luaC_checkGC(L); luai_threadyield(L);)
494+#define checkGC(L,c) Protect(luaC_condGC(L, c); luai_threadyield(L);)
475495
476496
477497 #define arith_op(op,tm) { \
@@ -493,9 +513,8 @@ void luaV_execute (lua_State *L) { · 15 unmodified lines
493513 TValue *k;
494514 StkId base;
495515 newframe: /* reentry point when frame changes (call/return) */
496− lua_assert(isLua(ci));
497516 lua_assert(ci == L->ci);
498− cl = &clvalue(ci->func)->l;
517+ cl = clLvalue(ci->func);
499518 k = cl->p->k;
500519 base = ci->u.l.base;
501520 /* main loop of interpreter */
@@ -515,7 +534,13 @@ void luaV_execute (lua_State *L) { · 13 unmodified lines
515534 setobjs2s(L, ra, RB(i));
516535 )
517536 vmcase(OP_LOADK,
518− TValue *rb = KBx(i);
537+ TValue *rb = k + GETARG_Bx(i);
538+ setobj2s(L, ra, rb);
539+ )
540+ vmcase(OP_LOADKX,
541+ TValue *rb;
542+ lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
543+ rb = k + GETARG_Ax(*ci->u.l.savedpc++);
519544 setobj2s(L, ra, rb);
520545 )
521546 vmcase(OP_LOADBOOL,
@@ -523,10 +548,10 @@ void luaV_execute (lua_State *L) { · 1 unmodified line
523548 if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
524549 )
525550 vmcase(OP_LOADNIL,
526− TValue *rb = RB(i);
551+ int b = GETARG_B(i);
527552 do {
528− setnilvalue(rb--);
529− } while (rb >= ra);
553+ setnilvalue(ra++);
554+ } while (b--);
530555 )
531556 vmcase(OP_GETUPVAL,
532557 int b = GETARG_B(i);
@@ -558,7 +583,11 @@ void luaV_execute (lua_State *L) { · 25 unmodified lines
558583 sethvalue(L, ra, t);
559584 if (b != 0 || c != 0)
560585 luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
561− checkGC(L);
586+ checkGC(L,
587+ L->top = ra + 1; /* limit of live values */
588+ luaC_step(L);
589+ L->top = ci->top; /* restore top */
590+ )
562591 )
563592 vmcase(OP_SELF,
564593 StkId rb = RB(i);
@@ -594,7 +623,8 @@ void luaV_execute (lua_State *L) { · 29 unmodified lines
594623 }
595624 )
596625 vmcase(OP_NOT,
597− int res = l_isfalse(RB(i)); /* next assignment may change this value */
626+ TValue *rb = RB(i);
627+ int res = l_isfalse(rb); /* next assignment may change this value */
598628 setbvalue(ra, res);
599629 )
600630 vmcase(OP_LEN,
@@ -603,49 +633,61 @@ void luaV_execute (lua_State *L) { · 2 unmodified lines
603633 vmcase(OP_CONCAT,
604634 int b = GETARG_B(i);
605635 int c = GETARG_C(i);
636+ StkId rb;
606637 L->top = base + c + 1; /* mark the end of concat operands */
607− Protect(luaV_concat(L, c-b+1); checkGC(L);)
638+ Protect(luaV_concat(L, c - b + 1));
639+ ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
640+ rb = b + base;
641+ setobjs2s(L, ra, rb);
642+ checkGC(L,
643+ L->top = (ra >= rb ? ra + 1 : rb); /* limit of live values */
644+ luaC_step(L);
645+ )
608646 L->top = ci->top; /* restore top */
609− setobjs2s(L, RA(i), base+b);
610647 )
611648 vmcase(OP_JMP,
612− dojump(GETARG_sBx(i));
649+ dojump(ci, i, 0);
613650 )
614651 vmcase(OP_EQ,
615652 TValue *rb = RKB(i);
616653 TValue *rc = RKC(i);
617654 Protect(
618− if (equalobj(L, rb, rc) == GETARG_A(i))
619− dojump(GETARG_sBx(*ci->u.l.savedpc));
655+ if (equalobj(L, rb, rc) != GETARG_A(i))
656+ ci->u.l.savedpc++;
657+ else
658+ donextjump(ci);
620659 )
621− ci->u.l.savedpc++;
622660 )
623661 vmcase(OP_LT,
624662 Protect(
625− if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
626− dojump(GETARG_sBx(*ci->u.l.savedpc));
663+ if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
664+ ci->u.l.savedpc++;
665+ else
666+ donextjump(ci);
627667 )
628− ci->u.l.savedpc++;
629668 )
630669 vmcase(OP_LE,
631670 Protect(
632− if (luaV_lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
633− dojump(GETARG_sBx(*ci->u.l.savedpc));
671+ if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
672+ ci->u.l.savedpc++;
673+ else
674+ donextjump(ci);
634675 )
635− ci->u.l.savedpc++;
636676 )
637677 vmcase(OP_TEST,
638− if (GETARG_C(i) ? !l_isfalse(ra) : l_isfalse(ra))
639− dojump(GETARG_sBx(*ci->u.l.savedpc));
640− ci->u.l.savedpc++;
678+ if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
679+ ci->u.l.savedpc++;
680+ else
681+ donextjump(ci);
641682 )
642683 vmcase(OP_TESTSET,
643684 TValue *rb = RB(i);
644− if (GETARG_C(i) ? !l_isfalse(rb) : l_isfalse(rb)) {
685+ if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
686+ ci->u.l.savedpc++;
687+ else {
645688 setobjs2s(L, ra, rb);
646− dojump(GETARG_sBx(*ci->u.l.savedpc));
689+ donextjump(ci);
647690 }
648− ci->u.l.savedpc++;
649691 )
650692 vmcase(OP_CALL,
651693 int b = GETARG_B(i);
@@ -711,7 +753,7 @@ void luaV_execute (lua_State *L) { · 59 unmodified lines
711753 lua_Number limit = nvalue(ra+1);
712754 if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
713755 : luai_numle(L, limit, idx)) {
714− dojump(GETARG_sBx(i)); /* jump back */
756+ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
715757 setnvalue(ra, idx); /* update internal index... */
716758 setnvalue(ra+3, idx); /* ...and external index */
717759 }
@@ -727,7 +769,7 @@ void luaV_execute (lua_State *L) { · 9 unmodified lines
727769 else if (!tonumber(pstep, ra+2))
728770 luaG_runerror(L, LUA_QL("for") " step must be a number");
729771 setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
730− dojump(GETARG_sBx(i));
772+ ci->u.l.savedpc += GETARG_sBx(i);
731773 )
732774 vmcase(OP_TFORCALL,
733775 StkId cb = ra + 3; /* call base */
@@ -746,7 +788,7 @@ void luaV_execute (lua_State *L) { · 12 unmodified lines
746788 l_tforloop:
747789 if (!ttisnil(ra + 1)) { /* continue loop? */
748790 setobjs2s(L, ra, ra + 1); /* save control variable */
749− dojump(GETARG_sBx(i)); /* jump back */
791+ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
750792 }
751793 )
752794 vmcase(OP_SETLIST,
@@ -759,6 +801,7 @@ void luaV_execute (lua_State *L) { · 6 unmodified lines
759801 lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
760802 c = GETARG_Ax(*ci->u.l.savedpc++);
761803 }
804+ luai_runtimecheck(L, ttistable(ra));
762805 h = hvalue(ra);
763806 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
764807 if (last > h->sizearray) /* needs more space? */
@@ -770,17 +813,18 @@ void luaV_execute (lua_State *L) { · 5 unmodified lines
770813 }
771814 L->top = ci->top; /* correct top (in case of previous open call) */
772815 )
773− vmcase(OP_CLOSE,
774− luaF_close(L, ra);
775− )
776816 vmcase(OP_CLOSURE,
777817 Proto *p = cl->p->p[GETARG_Bx(i)];
778818 Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
779819 if (ncl == NULL) /* no match? */
780820 pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
781821 else
782− setclvalue(L, ra, ncl); /* push cashed closure */
783− checkGC(L);
822+ setclLvalue(L, ra, ncl); /* push cashed closure */
823+ checkGC(L,
824+ L->top = ra + 1; /* limit of live values */
825+ luaC_step(L);
826+ L->top = ci->top; /* restore top */
827+ )
784828 )
785829 vmcase(OP_VARARG,
786830 int b = GETARG_B(i) - 1;
src/lvm.h+7−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.h,v 2.14 2009/12/17 16:20:01 roberto Exp $
2+** $Id: lvm.h,v 2.17 2011/05/31 18:27:56 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -17,12 +17,15 @@ · 11 unmodified lines
1717
1818 #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
1919
20−#define equalobj(L,o1,o2) \
21− (ttype(o1) == ttype(o2) && luaV_equalval_(L, o1, o2))
20+#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2))
21+
22+#define luaV_rawequalobj(t1,t2) \
23+ (ttisequal(t1,t2) && luaV_equalobj_(NULL,t1,t2))
2224
2325
2426 /* not to called directly */
25−LUAI_FUNC int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2);
27+LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2);
28+
2629
2730 LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
2831 LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
src/lzio.c+12−18
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lzio.c,v 1.31 2005/06/03 20:15:29 roberto Exp $
2+** $Id: lzio.c,v 1.33 2011/02/23 13:13:10 roberto Exp $
33 ** a generic input stream interface
44 ** See Copyright Notice in lua.h
55 */
@@ -25,26 +25,14 @@ int luaZ_fill (ZIO *z) { · 19 unmodified lines
2525 lua_unlock(L);
2626 buff = z->reader(L, z->data, &size);
2727 lua_lock(L);
28− if (buff == NULL || size == 0) return EOZ;
29− z->n = size - 1;
28+ if (buff == NULL || size == 0)
29+ return EOZ;
30+ z->n = size - 1; /* discount char being returned */
3031 z->p = buff;
3132 return char2int(*(z->p++));
3233 }
3334
3435
35−int luaZ_lookahead (ZIO *z) {
36− if (z->n == 0) {
37− if (luaZ_fill(z) == EOZ)
38− return EOZ;
39− else {
40− z->n++; /* luaZ_fill removed first byte; put back it */
41− z->p--;
42− }
43− }
44− return char2int(*z->p);
45−}
46−
47−
4836 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
4937 z->L = L;
5038 z->reader = reader;
@@ -58,8 +46,14 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { · 7 unmodified lines
5846 size_t luaZ_read (ZIO *z, void *b, size_t n) {
5947 while (n) {
6048 size_t m;
61− if (luaZ_lookahead(z) == EOZ)
62− return n; /* return number of missing bytes */
49+ if (z->n == 0) { /* no bytes in buffer? */
50+ if (luaZ_fill(z) == EOZ) /* try to read more */
51+ return n; /* no more input; return number of missing bytes */
52+ else {
53+ z->n++; /* luaZ_fill consumed first byte; put it back */
54+ z->p--;
55+ }
56+ }
6357 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
6458 memcpy(b, z->p, m);
6559 z->n -= m;
src/lzio.h+2−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lzio.h,v 1.22 2009/05/18 17:26:25 roberto Exp $
2+** $Id: lzio.h,v 1.24 2011/02/23 13:13:10 roberto Exp $
33 ** Buffered streams
44 ** See Copyright Notice in lua.h
55 */
@@ -50,7 +50,6 @@ LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); · 44 unmodified lines
5050 LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
5151 void *data);
5252 LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
53−LUAI_FUNC int luaZ_lookahead (ZIO *z);
5453
5554
5655
@@ -59,7 +58,7 @@ LUAI_FUNC int luaZ_lookahead (ZIO *z); · 2 unmodified lines
5958 struct Zio {
6059 size_t n; /* bytes still unread */
6160 const char *p; /* current position in buffer */
62− lua_Reader reader;
61+ lua_Reader reader; /* reader function */
6362 void* data; /* additional data */
6463 lua_State *L; /* Lua state (for reader) */
6564 };