Fix corner cases in _LE and _LEN metatag processing.

This commit is contained in:
James Roseborough
2014-10-27 13:52:26 +00:00
parent 384bf690c8
commit c0e043403f
5 changed files with 51 additions and 58 deletions

View File

@@ -16,7 +16,7 @@
Getting Started with LuaJ Getting Started with LuaJ
</h1> </h1>
James Roseborough, Ian Farmer, Version 3.0 James Roseborough, Ian Farmer, Version 3.0.1
<p> <p>
<small> <small>
Copyright &copy; 2009-2014 Luaj.org. Copyright &copy; 2009-2014 Luaj.org.
@@ -120,7 +120,7 @@ in comparison with the standard C distribution.
<td>16.794</td> <td>16.794</td>
<td>11.274</td> <td>11.274</td>
<td>Java</td> <td>Java</td>
<td>java -cp luaj-jse-3.0.jar;bcel-5.2.jar lua <b>-b</b> fannkuch.lua 10</td></tr> <td>java -cp luaj-jse-3.0.1.jar;bcel-5.2.jar lua <b>-b</b> fannkuch.lua 10</td></tr>
<tr valign="top"> <tr valign="top">
<td></td> <td></td>
<td></td> <td></td>
@@ -130,7 +130,7 @@ in comparison with the standard C distribution.
<td>36.894</td> <td>36.894</td>
<td>15.163</td> <td>15.163</td>
<td></td> <td></td>
<td>java -cp luaj-jse-3.0.jar lua -n fannkuch.lua 10</td></tr> <td>java -cp luaj-jse-3.0.1.jar lua -n fannkuch.lua 10</td></tr>
<tr valign="top"> <tr valign="top">
<td>lua</td> <td>lua</td>
<td>5.1.4</td> <td>5.1.4</td>
@@ -186,7 +186,7 @@ It is also faster than Java-lua implementations Jill, Kahlua, and Mochalua for a
From the main distribution directory line type: From the main distribution directory line type:
<pre> <pre>
java -cp lib/luaj-jse-3.0.jar lua examples/lua/hello.lua java -cp lib/luaj-jse-3.0.1.jar lua examples/lua/hello.lua
</pre> </pre>
<p> <p>
@@ -198,7 +198,7 @@ You should see the following output:
To see how luaj can be used to acccess most Java API's including swing, try: To see how luaj can be used to acccess most Java API's including swing, try:
<pre> <pre>
java -cp lib/luaj-jse-3.0.jar lua examples/lua/swingapp.lua java -cp lib/luaj-jse-3.0.1.jar lua examples/lua/swingapp.lua
</pre> </pre>
<p> <p>
@@ -213,8 +213,8 @@ Links to sources:<pre>
From the main distribution directory line type: From the main distribution directory line type:
<pre> <pre>
java -cp lib/luaj-jse-3.0.jar luac examples/lua/hello.lua java -cp lib/luaj-jse-3.0.1.jar luac examples/lua/hello.lua
java -cp lib/luaj-jse-3.0.jar lua luac.out java -cp lib/luaj-jse-3.0.1.jar lua luac.out
</pre> </pre>
<p> <p>
@@ -228,8 +228,8 @@ Luaj can compile lua sources or binaries directly to java bytecode if the bcel l
<pre> <pre>
ant bcel-lib ant bcel-lib
java -cp &quot;lib/luaj-jse-3.0.jar;lib/bcel-5.2.jar&quot; luajc -s examples/lua -d . hello.lua java -cp &quot;lib/luaj-jse-3.0.1.jar;lib/bcel-5.2.jar&quot; luajc -s examples/lua -d . hello.lua
java -cp &quot;lib/luaj-jse-3.0.jar;.&quot; lua -l hello java -cp &quot;lib/luaj-jse-3.0.1.jar;.&quot; lua -l hello
</pre> </pre>
<p> <p>
@@ -240,7 +240,7 @@ but the compiled classes must be in the class path at runtime, unless runtime ji
<p> <p>
Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-b</em></b> option and providing the <em>bcel</em> library in the class path: Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-b</em></b> option and providing the <em>bcel</em> library in the class path:
<pre> <pre>
java -cp &quot;lib/luaj-jse-3.0.jar;lib/bcel-5.2.jar&quot; lua -b examples/lua/hello.lua java -cp &quot;lib/luaj-jse-3.0.1.jar;lib/bcel-5.2.jar&quot; lua -b examples/lua/hello.lua
</pre> </pre>
@@ -284,7 +284,7 @@ A simple example may be found in
</pre> </pre>
<p> <p>
You must include the library <b>lib/luaj-jse-3.0.jar</b> in your class path. You must include the library <b>lib/luaj-jse-3.0.1.jar</b> in your class path.
<h2>Run a script in a MIDlet</h2> <h2>Run a script in a MIDlet</h2>
@@ -311,7 +311,7 @@ A simple example may be found in
</pre> </pre>
<p> <p>
You must include the library <b>lib/luaj-jme-3.0.jar</b> in your midlet jar. You must include the library <b>lib/luaj-jme-3.0.1.jar</b> in your midlet jar.
<p> <p>
An ant script to build and run the midlet is in An ant script to build and run the midlet is in
@@ -341,7 +341,7 @@ You can also look up the engine by language "lua" or mimetypes "text/lua" or "ap
All standard aspects of script engines including compiled statements are supported. All standard aspects of script engines including compiled statements are supported.
<p> <p>
You must include the library <b>lib/luaj-jse-3.0.jar</b> in your class path. You must include the library <b>lib/luaj-jse-3.0.1.jar</b> in your class path.
<p> <p>
A working example may be found in A working example may be found in
@@ -352,8 +352,8 @@ A working example may be found in
To compile and run it using Java 1.6 or higher: To compile and run it using Java 1.6 or higher:
<pre> <pre>
javac -cp lib/luaj-jse-3.0.jar examples/jse/ScriptEngineSample.java javac -cp lib/luaj-jse-3.0.1.jar examples/jse/ScriptEngineSample.java
java -cp &quot;lib/luaj-jse-3.0.jar;examples/jse&quot; ScriptEngineSample java -cp &quot;lib/luaj-jse-3.0.1.jar;examples/jse&quot; ScriptEngineSample
</pre> </pre>
<h2>Excluding the lua bytecode compiler</h2> <h2>Excluding the lua bytecode compiler</h2>
@@ -570,7 +570,7 @@ The following lua script will open a swing frame on Java SE:
See a longer sample in <em>examples/lua/swingapp.lua</em> for details, including a simple animation loop, rendering graphics, mouse and key handling, and image loading. See a longer sample in <em>examples/lua/swingapp.lua</em> for details, including a simple animation loop, rendering graphics, mouse and key handling, and image loading.
Or try running it using: Or try running it using:
<pre> <pre>
java -cp lib/luaj-jse-3.0.jar lua examples/lua/swingapp.lua java -cp lib/luaj-jse-3.0.1.jar lua examples/lua/swingapp.lua
</pre> </pre>
<p> <p>
@@ -817,7 +817,7 @@ For JSE projects, add this dependency for the luaj-jse jar:
&lt;dependency&gt; &lt;dependency&gt;
&lt;groupId&gt;org.luaj&lt;/groupId&gt; &lt;groupId&gt;org.luaj&lt;/groupId&gt;
&lt;artifactId&gt;luaj-jse&lt;/artifactId&gt; &lt;artifactId&gt;luaj-jse&lt;/artifactId&gt;
&lt;version&gt;3.0&lt;/version&gt; &lt;version&gt;3.0.1&lt;/version&gt;
&lt;/dependency&gt; &lt;/dependency&gt;
</pre> </pre>
while for JME projects, use the luaj-jme jar: while for JME projects, use the luaj-jme jar:
@@ -825,7 +825,7 @@ while for JME projects, use the luaj-jme jar:
&lt;dependency&gt; &lt;dependency&gt;
&lt;groupId&gt;org.luaj&lt;/groupId&gt; &lt;groupId&gt;org.luaj&lt;/groupId&gt;
&lt;artifactId&gt;luaj-jme&lt;/artifactId&gt; &lt;artifactId&gt;luaj-jme&lt;/artifactId&gt;
&lt;version&gt;3.0&lt;/version&gt; &lt;version&gt;3.0.1&lt;/version&gt;
&lt;/dependency&gt; &lt;/dependency&gt;
</pre> </pre>
@@ -855,7 +855,7 @@ Unit test scripts can be found in these locations
test/lua/*.lua test/lua/*.lua
test/lua/errors/*.lua test/lua/errors/*.lua
test/lua/perf/*.lua test/lua/perf/*.lua
test/lua/luaj3.0-tests.zip test/lua/luaj3.0.1-tests.zip
</pre> </pre>
<h2>Code coverage</h2> <h2>Code coverage</h2>
@@ -916,25 +916,19 @@ Files are no longer hosted at LuaForge.
<li>Improve compatibility of table.remove() </li> <li>Improve compatibility of table.remove() </li>
<li>Disallow base library setfenv() calls on Java functions </li> <li>Disallow base library setfenv() calls on Java functions </li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-alpha1</b></td><td><ul> <tr valign="top"><td>&nbsp;&nbsp;<b>3.0</b></td><td><ul>
<li>Convert internal and external API's to match lua 5.2.x environment changes </li> <li>Convert internal and external API's to match lua 5.2.x environment changes </li>
<li>Add bit32 library </li> <li>Add bit32 library </li>
<li>Add explicit Globals object to manage global state, especially to imrpove thread safety </li> <li>Add explicit Globals object to manage global state, especially to imrpove thread safety </li>
<li>Drop support for lua source to java surce (lua2java) in favor of direct java bytecode output (luajc) </li> <li>Drop support for lua source to java surce (lua2java) in favor of direct java bytecode output (luajc) </li>
<li>Remove compatibility functions like table.getn(), table.maxn(), table.foreach(), and math.log10() </li> <li>Remove compatibility functions like table.getn(), table.maxn(), table.foreach(), and math.log10() </li>
<li>Add ability to create runnable jar file from lua script with sample build file build-app.xml </li> <li>Add ability to create runnable jar file from lua script with sample build file build-app.xml </li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-alpha2</b></td><td><ul>
<li>Supply environment as second argument to LibFunction when loading via require() </li> <li>Supply environment as second argument to LibFunction when loading via require() </li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-alpha3</b></td><td><ul>
<li>Fix bug 3597515 memory leak due to string caching by simplifying caching logic.</li> <li>Fix bug 3597515 memory leak due to string caching by simplifying caching logic.</li>
<li>Fix bug 3565008 so that short substrings are backed by short arrays.</li> <li>Fix bug 3565008 so that short substrings are backed by short arrays.</li>
<li>Fix bug 3495802 to return correct offset of substrings from string.find().</li> <li>Fix bug 3495802 to return correct offset of substrings from string.find().</li>
<li>Add artifacts to Maven central repository.</li> <li>Add artifacts to Maven central repository.</li>
<li>Limit pluggable scripting to use compatible bindings and contexts, implement redirection.</li> <li>Limit pluggable scripting to use compatible bindings and contexts, implement redirection.</li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-beta1</b></td><td><ul>
<li>Fix bug that didn't read package.path from environment.</li> <li>Fix bug that didn't read package.path from environment.</li>
<li>Fix pluggable scripting engine lookup, simplify implementation, and add unit tests.</li> <li>Fix pluggable scripting engine lookup, simplify implementation, and add unit tests.</li>
<li>Coerce script engine eval() return values to Java.</li> <li>Coerce script engine eval() return values to Java.</li>
@@ -948,8 +942,6 @@ Files are no longer hosted at LuaForge.
<li>Improved behavior of next() (fixes issue #7).</li> <li>Improved behavior of next() (fixes issue #7).</li>
<li>Existing tables can now be made weak (fixes issue #16).</li> <li>Existing tables can now be made weak (fixes issue #16).</li>
<li>More compatible allocation of table entries in array vs. hash (fixes issue #8).</li> <li>More compatible allocation of table entries in array vs. hash (fixes issue #8).</li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-beta2</b></td><td><ul>
<li>Fix os.time() to return a number of seconds instead of milliseconds.</li> <li>Fix os.time() to return a number of seconds instead of milliseconds.</li>
<li>Implement formatting with os.date(), and table argument for os.time().</li> <li>Implement formatting with os.date(), and table argument for os.time().</li>
<li>LuaValue.checkfunction() now returns LuaFunction.</li> <li>LuaValue.checkfunction() now returns LuaFunction.</li>
@@ -959,9 +951,6 @@ Files are no longer hosted at LuaForge.
<li>Let errors thrown in debug hooks bubble up to the running coroutine.</li> <li>Let errors thrown in debug hooks bubble up to the running coroutine.</li>
<li>Make error message handler function in xpcall per-thread instead of per-globals.</li> <li>Make error message handler function in xpcall per-thread instead of per-globals.</li>
<li>Establish "org.luaj.debug" and "org.luaj.luajc" system properties to configure scripting engine.</li> <li>Establish "org.luaj.debug" and "org.luaj.luajc" system properties to configure scripting engine.</li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0</b></td><td><ul>
<li>Fix maven sample code.</li>
<li>Add sample code for Android Application that uses luaj.</li> <li>Add sample code for Android Application that uses luaj.</li>
<li>Add sample code for Applet that uses luaj.</li> <li>Add sample code for Applet that uses luaj.</li>
<li>Fix balanced match for empty string (fixes issue #23).</li> <li>Fix balanced match for empty string (fixes issue #23).</li>
@@ -974,6 +963,10 @@ Files are no longer hosted at LuaForge.
<li>In CoerceJavaToLua.coerse(), coerce LuaValue to same value (fixes issue #29).</li> <li>In CoerceJavaToLua.coerse(), coerce LuaValue to same value (fixes issue #29).</li>
<li>Fix line number reporting in debug stack traces (fixes issue #30).</li> <li>Fix line number reporting in debug stack traces (fixes issue #30).</li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0.1</b></td><td><ul>
<li>Fix __len metatag processing for tables.</li>
<li>Add fallback to __lt when pocessing __le metatag.</li>
</ul></td></tr> </ul></td></tr>
</table></td></tr></table> </table></td></tr></table>

View File

@@ -82,10 +82,15 @@ import org.luaj.vm2.compiler.DumpState;
* @see LuaC * @see LuaC
* @see LuaJC * @see LuaJC
*/ */
public class LoadState implements Globals.Undumper { public class LoadState {
/** Shared instance of Globals.Undumper to use loading prototypes from binary lua files */ /** Shared instance of Globals.Undumper to use loading prototypes from binary lua files */
public static final Globals.Undumper instance = new LoadState(); public static final Globals.Undumper instance = new Globals.Undumper() {
public Prototype undump(InputStream stream, String chunkname)
throws IOException {
return LoadState.undump(stream, chunkname);
}
};
/** format corresponding to non-number-patched lua, all numbers are floats or doubles */ /** format corresponding to non-number-patched lua, all numbers are floats or doubles */
public static final int NUMBER_FORMAT_FLOATS_OR_DOUBLES = 0; public static final int NUMBER_FORMAT_FLOATS_OR_DOUBLES = 0;
@@ -387,7 +392,7 @@ public class LoadState implements Globals.Undumper {
* @return {@link Prototype} that was loaded, or null if the first 4 bytes were not the lua signature. * @return {@link Prototype} that was loaded, or null if the first 4 bytes were not the lua signature.
* @throws IOException if an IOException occurs * @throws IOException if an IOException occurs
*/ */
public Prototype undump(InputStream stream, String chunkname) throws IOException { public static Prototype undump(InputStream stream, String chunkname) throws IOException {
// check rest of signature // check rest of signature
if ( stream.read() != LUA_SIGNATURE[0] if ( stream.read() != LUA_SIGNATURE[0]
|| stream.read() != LUA_SIGNATURE[1] || stream.read() != LUA_SIGNATURE[1]
@@ -432,8 +437,4 @@ public class LoadState implements Globals.Undumper {
this.is = new DataInputStream( stream ); this.is = new DataInputStream( stream );
} }
private LoadState() {
this.name = "";
this.is = null;
}
} }

View File

@@ -299,7 +299,7 @@ public class LuaTable extends LuaValue implements Metatable {
* @return The removed item, or {@link #NONE} if not removed * @return The removed item, or {@link #NONE} if not removed
*/ */
public LuaValue remove(int pos) { public LuaValue remove(int pos) {
int n = length(); int n = rawlen();
if ( pos == 0 ) if ( pos == 0 )
pos = n; pos = n;
else if (pos > n) else if (pos > n)
@@ -319,7 +319,7 @@ public class LuaTable extends LuaValue implements Metatable {
*/ */
public void insert(int pos, LuaValue value) { public void insert(int pos, LuaValue value) {
if ( pos == 0 ) if ( pos == 0 )
pos = length()+1; pos = rawlen()+1;
while ( ! value.isnil() ) { while ( ! value.isnil() ) {
LuaValue v = rawget( pos ); LuaValue v = rawget( pos );
rawset(pos++, value); rawset(pos++, value);
@@ -347,6 +347,17 @@ public class LuaTable extends LuaValue implements Metatable {
} }
public int length() { public int length() {
return m_metatable != null? len().toint(): rawlen();
}
public LuaValue len() {
final LuaValue h = metatag(LEN);
if (h.toboolean())
return h.call(this);
return LuaInteger.valueOf(rawlen());
}
public int rawlen() {
int a = getArrayLength(); int a = getArrayLength();
int n = a+1,m=0; int n = a+1,m=0;
while ( !rawget(n).isnil() ) { while ( !rawget(n).isnil() ) {
@@ -362,14 +373,6 @@ public class LuaTable extends LuaValue implements Metatable {
} }
return m; return m;
} }
public LuaValue len() {
return LuaInteger.valueOf(length());
}
public int rawlen() {
return length();
}
/** /**
* Get the next element after a particular key in the table * Get the next element after a particular key in the table
@@ -879,12 +882,12 @@ public class LuaTable extends LuaValue implements Metatable {
/** Unpack all the elements of this table */ /** Unpack all the elements of this table */
public Varargs unpack() { public Varargs unpack() {
return unpack(1, this.length()); return unpack(1, this.rawlen());
} }
/** Unpack all the elements of this table from element i */ /** Unpack all the elements of this table from element i */
public Varargs unpack(int i) { public Varargs unpack(int i) {
return unpack(i, this.length()); return unpack(i, this.rawlen());
} }
/** Unpack the elements from i to j inclusive */ /** Unpack the elements from i to j inclusive */

View File

@@ -2965,12 +2965,11 @@ public class LuaValue extends Varargs {
* @see #lteq(LuaValue) * @see #lteq(LuaValue)
*/ */
public LuaValue comparemt( LuaValue tag, LuaValue op1 ) { public LuaValue comparemt( LuaValue tag, LuaValue op1 ) {
LuaValue h = metatag(tag); LuaValue h;
if ( !h.isnil() ) if (!(h = metatag(tag)).isnil() || !(h = op1.metatag(tag)).isnil())
return h.call(this, op1);
h = op1.metatag(tag);
if ( !h.isnil() )
return h.call(this, op1); return h.call(this, op1);
if (LuaValue.LE.raweq(tag) && (!(h = metatag(LT)).isnil() || !(h = op1.metatag(LT)).isnil()))
return h.call(op1, this).not();
return error("attempt to compare "+tag+" on "+typename()+" and "+op1.typename()); return error("attempt to compare "+tag+" on "+typename()+" and "+op1.typename());
} }

View File

@@ -21,9 +21,6 @@
******************************************************************************/ ******************************************************************************/
package org.luaj.vm2; package org.luaj.vm2;
import org.luaj.vm2.Varargs.ArrayPartVarargs;
import org.luaj.vm2.Varargs.PairVarargs;
/** /**
* Subclass of {@link Varargs} that represents a lua tail call * Subclass of {@link Varargs} that represents a lua tail call
* in a Java library function execution environment. * in a Java library function execution environment.