Coerce doubles into integers wherever possible, use hash code for doubles that is compatible with integers.
This commit is contained in:
@@ -350,7 +350,7 @@ public class LexState {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
d = Double.parseDouble(str);
|
d = Double.parseDouble(str);
|
||||||
seminfo.r = new LDouble(d);
|
seminfo.r = LDouble.numberOf(d);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ public class MathLib extends LFunction {
|
|||||||
|
|
||||||
private static void setResult( LuaState vm, double d ) {
|
private static void setResult( LuaState vm, double d ) {
|
||||||
vm.resettop();
|
vm.resettop();
|
||||||
vm.pushlvalue( LDouble.valueOf(d) );
|
vm.pushlvalue( LDouble.numberOf(d) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public int invoke(LuaState vm) {
|
public int invoke(LuaState vm) {
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ public class LDouble extends LNumber {
|
|||||||
if ( m_value == 0 ) {
|
if ( m_value == 0 ) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
|
int iz = (int) m_value;
|
||||||
|
if ( iz == m_value ) return iz;
|
||||||
long bits = Double.doubleToLongBits( m_value );
|
long bits = Double.doubleToLongBits( m_value );
|
||||||
return ( (int) bits >> 32 ) + ( (int) bits );
|
return ( (int) bits >> 32 ) + ( (int) bits );
|
||||||
}
|
}
|
||||||
@@ -93,7 +95,7 @@ public class LDouble extends LNumber {
|
|||||||
case Lua.OP_MUL: return LDouble.numberOf( lhs * rhs );
|
case Lua.OP_MUL: return LDouble.numberOf( lhs * rhs );
|
||||||
case Lua.OP_DIV: return LDouble.numberOf( lhs / rhs );
|
case Lua.OP_DIV: return LDouble.numberOf( lhs / rhs );
|
||||||
case Lua.OP_MOD: return LDouble.numberOf( lhs - Math.floor(lhs/rhs) * rhs );
|
case Lua.OP_MOD: return LDouble.numberOf( lhs - Math.floor(lhs/rhs) * rhs );
|
||||||
case Lua.OP_POW: return Platform.getInstance().mathPow( LDouble.valueOf(lhs), LDouble.valueOf(rhs));
|
case Lua.OP_POW: return Platform.getInstance().mathPow( LDouble.numberOf(lhs), LDouble.numberOf(rhs));
|
||||||
}
|
}
|
||||||
LuaState.vmerror( "bad bin opcode" );
|
LuaState.vmerror( "bad bin opcode" );
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -391,7 +391,7 @@ public class LString extends LValue {
|
|||||||
} catch ( NumberFormatException nfe ) {
|
} catch ( NumberFormatException nfe ) {
|
||||||
if ( base == 10 ) {
|
if ( base == 10 ) {
|
||||||
try {
|
try {
|
||||||
return new LDouble( Double.parseDouble( str ) );
|
return LDouble.numberOf( Double.parseDouble( str ) );
|
||||||
} catch ( NumberFormatException nfe2 ) {
|
} catch ( NumberFormatException nfe2 ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ public class LoadState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
double value = Double.longBitsToDouble(bits);
|
double value = Double.longBitsToDouble(bits);
|
||||||
return new LDouble( value );
|
return LDouble.numberOf( value );
|
||||||
}
|
}
|
||||||
|
|
||||||
LNumber loadNumber() throws IOException {
|
LNumber loadNumber() throws IOException {
|
||||||
|
|||||||
@@ -1445,7 +1445,7 @@ public class LuaState extends Lua {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void pushnumber(double d) {
|
public void pushnumber(double d) {
|
||||||
pushlvalue(new LDouble(d));
|
pushlvalue(LDouble.numberOf(d));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class CoerceJavaToLua {
|
|||||||
Coercion doubleCoercion = new Coercion() {
|
Coercion doubleCoercion = new Coercion() {
|
||||||
public LValue coerce( Object javaValue ) {
|
public LValue coerce( Object javaValue ) {
|
||||||
Number n = (Number) javaValue;
|
Number n = (Number) javaValue;
|
||||||
return new LDouble( n.doubleValue() );
|
return LDouble.numberOf( n.doubleValue() );
|
||||||
}
|
}
|
||||||
} ;
|
} ;
|
||||||
Coercion stringCoercion = new Coercion() {
|
Coercion stringCoercion = new Coercion() {
|
||||||
|
|||||||
@@ -227,9 +227,7 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
|
|||||||
}
|
}
|
||||||
private LValue toLua(Object javaValue) {
|
private LValue toLua(Object javaValue) {
|
||||||
if ( javaValue instanceof Number ) {
|
if ( javaValue instanceof Number ) {
|
||||||
double d = ((Number)javaValue).doubleValue();
|
return LDouble.numberOf(((Number)javaValue).doubleValue());
|
||||||
int id = (int) d;
|
|
||||||
return (d==id? LInteger.valueOf(id): new LDouble(d));
|
|
||||||
} else if ( javaValue instanceof String ) {
|
} else if ( javaValue instanceof String ) {
|
||||||
return LString.valueOf(javaValue.toString());
|
return LString.valueOf(javaValue.toString());
|
||||||
} else if ( javaValue == null ) {
|
} else if ( javaValue == null ) {
|
||||||
|
|||||||
@@ -54,13 +54,7 @@ public class LoadStateTest extends TestCase {
|
|||||||
|
|
||||||
private LNumber simpleBitsToLuaNumber( long bits ) {
|
private LNumber simpleBitsToLuaNumber( long bits ) {
|
||||||
double value = Double.longBitsToDouble( bits );
|
double value = Double.longBitsToDouble( bits );
|
||||||
int valueAsInt = (int) value;
|
return LDouble.numberOf(value);
|
||||||
|
|
||||||
if ( value == (double) valueAsInt ) {
|
|
||||||
return LInteger.valueOf( valueAsInt );
|
|
||||||
} else {
|
|
||||||
return new LDouble( value );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLongBitsToLuaNumberSpeed() throws InterruptedException {
|
public void testLongBitsToLuaNumberSpeed() throws InterruptedException {
|
||||||
|
|||||||
@@ -228,8 +228,8 @@ public class MathLibTest extends TestCase {
|
|||||||
|
|
||||||
private void tryMathOp(int id, double a, double b) {
|
private void tryMathOp(int id, double a, double b) {
|
||||||
try {
|
try {
|
||||||
double expected = j2se.mathop(id, LDouble.valueOf(a), LDouble.valueOf(b)).toJavaDouble();
|
double expected = j2se.mathop(id, LDouble.numberOf(a), LDouble.numberOf(b)).toJavaDouble();
|
||||||
double actual = j2me.mathop(id, LDouble.valueOf(a), LDouble.valueOf(b)).toJavaDouble();
|
double actual = j2me.mathop(id, LDouble.numberOf(a), LDouble.numberOf(b)).toJavaDouble();
|
||||||
if ( supportedOnJ2me )
|
if ( supportedOnJ2me )
|
||||||
assertEquals( expected, actual, 1.e-5 );
|
assertEquals( expected, actual, 1.e-5 );
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -209,4 +209,16 @@ for i,test in ipairs(tests) do
|
|||||||
print( 't[nil]', pcall( function() return testtable[nil] end ) )
|
print( 't[nil]', pcall( function() return testtable[nil] end ) )
|
||||||
print( 't[nil]=nil', pcall( function() testtable[nil]=nil end ) )
|
print( 't[nil]=nil', pcall( function() testtable[nil]=nil end ) )
|
||||||
print( 't[nil]', pcall( function() return testtable[nil] end ) )
|
print( 't[nil]', pcall( function() return testtable[nil] end ) )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- tables with doubles
|
||||||
|
local t = { [1]='a', [2]='b', [3.0]='c', [7]='d', [9]='e', [20]='f', [30.0]='g', [12.5]='h' }
|
||||||
|
print(#t)
|
||||||
|
print(t[1], t[2], t[3], t[7], t[9], t[20], t[30])
|
||||||
|
print(t[1.0], t[2.0], t[3.0], t[7.0], t[9.0], t[20.0], t[30.0], t[12.5])
|
||||||
|
local i,j,k,l,m,n,o = math.ceil(0.7),math.floor(2.1),math.abs(-3.0),math.sqrt(49.0),math.ceil(8.6),math.floor(20.5),math.max(1.2,30.0)
|
||||||
|
print(i, j, k, l, m, n, o)
|
||||||
|
print(t[i], t[j], t[k], t[l], t[m], t[n], t[o])
|
||||||
|
local half,two = .5,2
|
||||||
|
print(t[1.5-half], t[1.5+half], t[6/2], t[3.5*2], t[8.5+half], t[20.5-half], t[60*half], t[13-half])
|
||||||
|
print(#t)
|
||||||
|
|||||||
Reference in New Issue
Block a user