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

@@ -82,10 +82,15 @@ import org.luaj.vm2.compiler.DumpState;
* @see LuaC
* @see LuaJC
*/
public class LoadState implements Globals.Undumper {
public class LoadState {
/** 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 */
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.
* @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
if ( stream.read() != LUA_SIGNATURE[0]
|| stream.read() != LUA_SIGNATURE[1]
@@ -432,8 +437,4 @@ public class LoadState implements Globals.Undumper {
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
*/
public LuaValue remove(int pos) {
int n = length();
int n = rawlen();
if ( pos == 0 )
pos = n;
else if (pos > n)
@@ -319,7 +319,7 @@ public class LuaTable extends LuaValue implements Metatable {
*/
public void insert(int pos, LuaValue value) {
if ( pos == 0 )
pos = length()+1;
pos = rawlen()+1;
while ( ! value.isnil() ) {
LuaValue v = rawget( pos );
rawset(pos++, value);
@@ -347,6 +347,17 @@ public class LuaTable extends LuaValue implements Metatable {
}
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 n = a+1,m=0;
while ( !rawget(n).isnil() ) {
@@ -362,14 +373,6 @@ public class LuaTable extends LuaValue implements Metatable {
}
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
@@ -879,12 +882,12 @@ public class LuaTable extends LuaValue implements Metatable {
/** Unpack all the elements of this table */
public Varargs unpack() {
return unpack(1, this.length());
return unpack(1, this.rawlen());
}
/** Unpack all the elements of this table from element i */
public Varargs unpack(int i) {
return unpack(i, this.length());
return unpack(i, this.rawlen());
}
/** Unpack the elements from i to j inclusive */

View File

@@ -2965,12 +2965,11 @@ public class LuaValue extends Varargs {
* @see #lteq(LuaValue)
*/
public LuaValue comparemt( LuaValue tag, LuaValue op1 ) {
LuaValue h = metatag(tag);
if ( !h.isnil() )
return h.call(this, op1);
h = op1.metatag(tag);
if ( !h.isnil() )
LuaValue h;
if (!(h = metatag(tag)).isnil() || !(h = op1.metatag(tag)).isnil())
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());
}

View File

@@ -21,9 +21,6 @@
******************************************************************************/
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
* in a Java library function execution environment.