Make LInteger constructor private and use LInteger.valueOf() everywhere, caching common values.
This commit is contained in:
@@ -435,7 +435,7 @@ public class FuncState extends LuaC {
|
|||||||
double d = r.luaAsDouble();
|
double d = r.luaAsDouble();
|
||||||
int i = (int) d;
|
int i = (int) d;
|
||||||
if ( d == (double) i )
|
if ( d == (double) i )
|
||||||
r = new LInteger(i);
|
r = LInteger.valueOf(i);
|
||||||
}
|
}
|
||||||
return this.addk(r);
|
return this.addk(r);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -677,7 +677,7 @@ public class LexState extends LuaC {
|
|||||||
_nval = r;
|
_nval = r;
|
||||||
}
|
}
|
||||||
public LNumber nval() {
|
public LNumber nval() {
|
||||||
return (_nval == null? new LInteger(s.info): _nval);
|
return (_nval == null? LInteger.valueOf(s.info): _nval);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final U u = new U();
|
final U u = new U();
|
||||||
@@ -1621,7 +1621,7 @@ public class LexState extends LuaC {
|
|||||||
if (this.testnext(','))
|
if (this.testnext(','))
|
||||||
this.exp1(); /* optional step */
|
this.exp1(); /* optional step */
|
||||||
else { /* default step = 1 */
|
else { /* default step = 1 */
|
||||||
fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(new LInteger(1)));
|
fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(LInteger.valueOf(1)));
|
||||||
fs.reserveregs(1);
|
fs.reserveregs(1);
|
||||||
}
|
}
|
||||||
this.forbody(base, line, 1, true);
|
this.forbody(base, line, 1, true);
|
||||||
|
|||||||
@@ -375,7 +375,7 @@ public class LuaCompat extends LFunction {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if ( arg.luaAsString().equals( "#" ) ) {
|
} else if ( arg.luaAsString().equals( "#" ) ) {
|
||||||
vm.setResult( new LInteger( vm.getArgCount() - 1 ) );
|
vm.setResult( LInteger.valueOf( vm.getArgCount() - 1 ) );
|
||||||
}
|
}
|
||||||
vm.setResult();
|
vm.setResult();
|
||||||
}
|
}
|
||||||
@@ -516,7 +516,7 @@ public class LuaCompat extends LFunction {
|
|||||||
String filename = vm.getArgAsString(0);
|
String filename = vm.getArgAsString(0);
|
||||||
if ( loadfile( vm, filename ) ) {
|
if ( loadfile( vm, filename ) ) {
|
||||||
int s = vm.lua_pcall(1, 0);
|
int s = vm.lua_pcall(1, 0);
|
||||||
vm.setResult( new LInteger( s!=0? 1: 0 ) );
|
vm.setResult( LInteger.valueOf( s!=0? 1: 0 ) );
|
||||||
} else {
|
} else {
|
||||||
vm.lua_error("cannot open "+filename);
|
vm.lua_error("cannot open "+filename);
|
||||||
}
|
}
|
||||||
@@ -709,7 +709,7 @@ public class LuaCompat extends LFunction {
|
|||||||
*/
|
*/
|
||||||
private void maxn(VM vm) {
|
private void maxn(VM vm) {
|
||||||
LTable table = (LTable) vm.getArg(0);
|
LTable table = (LTable) vm.getArg(0);
|
||||||
vm.setResult( new LInteger( table.luaMaxN() ) );
|
vm.setResult( LInteger.valueOf( table.luaMaxN() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class StrLib {
|
|||||||
return;
|
return;
|
||||||
int n = j - i + 1;
|
int n = j - i + 1;
|
||||||
for ( int k=0; k < n; k++ )
|
for ( int k=0; k < n; k++ )
|
||||||
vm.push( new LInteger( ls.luaByte(k+i-1) ) );
|
vm.push( LInteger.valueOf( ls.luaByte(k+i-1) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -252,7 +252,7 @@ public class StrLib {
|
|||||||
lbuf.append( src.substring( soffset, srclen ) );
|
lbuf.append( src.substring( soffset, srclen ) );
|
||||||
vm.setResult();
|
vm.setResult();
|
||||||
vm.push( lbuf.toLuaString() );
|
vm.push( lbuf.toLuaString() );
|
||||||
vm.push( new LInteger( n ) );
|
vm.push( LInteger.valueOf( n ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -262,7 +262,7 @@ public class StrLib {
|
|||||||
* Embedded zeros are counted, so "a\000bc\000" has length 5.
|
* Embedded zeros are counted, so "a\000bc\000" has length 5.
|
||||||
*/
|
*/
|
||||||
static void len( VM vm ) {
|
static void len( VM vm ) {
|
||||||
vm.setResult( new LInteger( vm.getArgAsLuaString(0).length()) );
|
vm.setResult( LInteger.valueOf( vm.getArgAsLuaString(0).length()) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -566,7 +566,7 @@ public class StrLib {
|
|||||||
vm.lua_error( "unfinished capture" );
|
vm.lua_error( "unfinished capture" );
|
||||||
}
|
}
|
||||||
if ( l == CAP_POSITION ) {
|
if ( l == CAP_POSITION ) {
|
||||||
vm.push( new LInteger( cinit[i] + 1 ) );
|
vm.push( LInteger.valueOf( cinit[i] + 1 ) );
|
||||||
} else {
|
} else {
|
||||||
int begin = cinit[i];
|
int begin = cinit[i];
|
||||||
vm.push( s.substring( begin, begin + l ) );
|
vm.push( s.substring( begin, begin + l ) );
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class CoerceJavaToLua {
|
|||||||
Coercion intCoercion = new Coercion() {
|
Coercion intCoercion = new Coercion() {
|
||||||
public LValue coerce( Object javaValue ) {
|
public LValue coerce( Object javaValue ) {
|
||||||
Number n = (Number) javaValue;
|
Number n = (Number) javaValue;
|
||||||
return new LInteger( n.intValue() );
|
return LInteger.valueOf( n.intValue() );
|
||||||
}
|
}
|
||||||
} ;
|
} ;
|
||||||
Coercion doubleCoercion = new Coercion() {
|
Coercion doubleCoercion = new Coercion() {
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ public class LoadState {
|
|||||||
|
|
||||||
static LNumber longBitsToLuaNumber( long bits ) {
|
static LNumber longBitsToLuaNumber( long bits ) {
|
||||||
if ( ( bits & ( ( 1L << 63 ) - 1 ) ) == 0L ) {
|
if ( ( bits & ( ( 1L << 63 ) - 1 ) ) == 0L ) {
|
||||||
return new LInteger( 0 );
|
return LInteger.valueOf( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int e = (int)((bits >> 52) & 0x7ffL) - 1023;
|
int e = (int)((bits >> 52) & 0x7ffL) - 1023;
|
||||||
@@ -118,7 +118,7 @@ public class LoadState {
|
|||||||
long intPrecMask = ( 1L << shift ) - 1;
|
long intPrecMask = ( 1L << shift ) - 1;
|
||||||
if ( ( f & intPrecMask ) == 0 ) {
|
if ( ( f & intPrecMask ) == 0 ) {
|
||||||
int intValue = (int)( f >> shift ) | ( 1 << e );
|
int intValue = (int)( f >> shift ) | ( 1 << e );
|
||||||
return new LInteger( ( ( bits >> 63 ) != 0 ) ? -intValue : intValue );
|
return LInteger.valueOf( ( ( bits >> 63 ) != 0 ) ? -intValue : intValue );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +129,7 @@ public class LoadState {
|
|||||||
LNumber loadNumber() throws IOException {
|
LNumber loadNumber() throws IOException {
|
||||||
if ( this.luacIsNumberIntegral ) {
|
if ( this.luacIsNumberIntegral ) {
|
||||||
int value = loadInt();
|
int value = loadInt();
|
||||||
return new LInteger( value );
|
return LInteger.valueOf( value );
|
||||||
} else {
|
} else {
|
||||||
return longBitsToLuaNumber( loadInt64() );
|
return longBitsToLuaNumber( loadInt64() );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,24 @@ import lua.Lua;
|
|||||||
public class LInteger extends LNumber {
|
public class LInteger extends LNumber {
|
||||||
private final int m_value;
|
private final int m_value;
|
||||||
|
|
||||||
public LInteger(int value) {
|
/* local cache of commonly used LInteger values */
|
||||||
|
private static final int INTS_MIN = -16;
|
||||||
|
private static final int INTS_MAX = 32;
|
||||||
|
private static final LInteger s_ints[] = new LInteger[1+INTS_MAX-INTS_MIN];
|
||||||
|
static {
|
||||||
|
for ( int i=INTS_MIN; i<=INTS_MAX; i++ )
|
||||||
|
s_ints[i-INTS_MIN] = new LInteger(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get an LInteger corresponding to a particular int value */
|
||||||
|
public static LInteger valueOf(int n) {
|
||||||
|
if ( n >= INTS_MIN && n <= INTS_MAX )
|
||||||
|
return s_ints[n-INTS_MIN];
|
||||||
|
return new LInteger(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** use LInteger.valueOf() instead */
|
||||||
|
private LInteger(int value) {
|
||||||
this.m_value = value;
|
this.m_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,12 +54,12 @@ public class LInteger extends LNumber {
|
|||||||
// binary operations on integers
|
// binary operations on integers
|
||||||
public LValue luaBinOpInteger(int opcode, int rhs) {
|
public LValue luaBinOpInteger(int opcode, int rhs) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
case Lua.OP_ADD: return new LInteger( m_value + rhs );
|
case Lua.OP_ADD: return LInteger.valueOf( m_value + rhs );
|
||||||
case Lua.OP_SUB: return new LInteger( m_value - rhs );
|
case Lua.OP_SUB: return LInteger.valueOf( m_value - rhs );
|
||||||
case Lua.OP_MUL: return new LInteger( m_value * rhs );
|
case Lua.OP_MUL: return LInteger.valueOf( m_value * rhs );
|
||||||
case Lua.OP_DIV: return new LInteger( m_value / rhs );
|
case Lua.OP_DIV: return LInteger.valueOf( m_value / rhs );
|
||||||
case Lua.OP_MOD: return new LInteger( m_value - ((int) Math.floor(m_value/(double)rhs)) * rhs );
|
case Lua.OP_MOD: return LInteger.valueOf( m_value - ((int) Math.floor(m_value/(double)rhs)) * rhs );
|
||||||
case Lua.OP_POW: return new LInteger( ipow(m_value, rhs) );
|
case Lua.OP_POW: return LInteger.valueOf( ipow(m_value, rhs) );
|
||||||
}
|
}
|
||||||
return luaUnsupportedOperation();
|
return luaUnsupportedOperation();
|
||||||
}
|
}
|
||||||
@@ -83,8 +100,7 @@ public class LInteger extends LNumber {
|
|||||||
|
|
||||||
/** Arithmetic negative */
|
/** Arithmetic negative */
|
||||||
public LValue luaUnaryMinus() {
|
public LValue luaUnaryMinus() {
|
||||||
return new LInteger( -m_value );
|
return LInteger.valueOf( -m_value );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ public class LString extends LValue {
|
|||||||
if ( base >= 2 && base <= 36 ) {
|
if ( base >= 2 && base <= 36 ) {
|
||||||
String str = toJavaString().trim();
|
String str = toJavaString().trim();
|
||||||
try {
|
try {
|
||||||
return new LInteger( Integer.parseInt( str, base ) );
|
return LInteger.valueOf( Integer.parseInt( str, base ) );
|
||||||
} catch ( NumberFormatException nfe ) {
|
} catch ( NumberFormatException nfe ) {
|
||||||
if ( base == 10 ) {
|
if ( base == 10 ) {
|
||||||
try {
|
try {
|
||||||
@@ -284,7 +284,7 @@ public class LString extends LValue {
|
|||||||
|
|
||||||
/** Built-in opcode LEN, for Strings and Tables */
|
/** Built-in opcode LEN, for Strings and Tables */
|
||||||
public LValue luaLength() {
|
public LValue luaLength() {
|
||||||
return new LInteger( length() );
|
return LInteger.valueOf( length() );
|
||||||
}
|
}
|
||||||
|
|
||||||
public LString luaGetType() {
|
public LString luaGetType() {
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ public class LTable extends LValue {
|
|||||||
int slot = findSlot( key );
|
int slot = findSlot( key );
|
||||||
if ( fillHashSlot( slot, value ) )
|
if ( fillHashSlot( slot, value ) )
|
||||||
return;
|
return;
|
||||||
m_hashKeys[ slot ] = new LInteger( key );
|
m_hashKeys[ slot ] = LInteger.valueOf( key );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -241,10 +241,10 @@ public class LTable extends LValue {
|
|||||||
for ( int i = Math.max( 0, m_arrayEntries-1 ); i < m_vector.length; ++i ) {
|
for ( int i = Math.max( 0, m_arrayEntries-1 ); i < m_vector.length; ++i ) {
|
||||||
if ( m_vector[i] != LNil.NIL &&
|
if ( m_vector[i] != LNil.NIL &&
|
||||||
( i+1 == m_vector.length || m_vector[i+1] == LNil.NIL ) ) {
|
( i+1 == m_vector.length || m_vector[i+1] == LNil.NIL ) ) {
|
||||||
return new LInteger( i+1 );
|
return LInteger.valueOf( i+1 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new LInteger( 0 );
|
return LInteger.valueOf( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Valid for tables */
|
/** Valid for tables */
|
||||||
@@ -289,7 +289,7 @@ public class LTable extends LValue {
|
|||||||
int i;
|
int i;
|
||||||
while ( ( i = arrayIndex++ ) < m_vector.length ) {
|
while ( ( i = arrayIndex++ ) < m_vector.length ) {
|
||||||
if ( m_vector[i] != LNil.NIL ) {
|
if ( m_vector[i] != LNil.NIL ) {
|
||||||
vm.push( new LInteger( arrayIndex ) );
|
vm.push( LInteger.valueOf( arrayIndex ) );
|
||||||
vm.push( m_vector[ i ] );
|
vm.push( m_vector[ i ] );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -319,7 +319,7 @@ public class LTable extends LValue {
|
|||||||
|
|
||||||
for ( int i = 0; i < m_vector.length; ++i ) {
|
for ( int i = 0; i < m_vector.length; ++i ) {
|
||||||
if ( m_vector[ i ] != LNil.NIL ) {
|
if ( m_vector[ i ] != LNil.NIL ) {
|
||||||
keys[ out++ ] = new LInteger( i + 1 );
|
keys[ out++ ] = LInteger.valueOf( i + 1 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,7 +496,7 @@ public class LTable extends LValue {
|
|||||||
if (checkLoadFactor())
|
if (checkLoadFactor())
|
||||||
rehash();
|
rehash();
|
||||||
final int slot = findSlot( i+1 );
|
final int slot = findSlot( i+1 );
|
||||||
m_hashKeys[ slot ] = new LInteger( i+1 );
|
m_hashKeys[ slot ] = LInteger.valueOf( i+1 );
|
||||||
m_hashValues[ slot ] = v;
|
m_hashValues[ slot ] = v;
|
||||||
++m_hashEntries;
|
++m_hashEntries;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class LoadStateTest extends TestCase {
|
|||||||
int valueAsInt = (int) value;
|
int valueAsInt = (int) value;
|
||||||
|
|
||||||
if ( value == (double) valueAsInt ) {
|
if ( value == (double) valueAsInt ) {
|
||||||
return new LInteger( valueAsInt );
|
return LInteger.valueOf( valueAsInt );
|
||||||
} else {
|
} else {
|
||||||
return new LDouble( value );
|
return new LDouble( value );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class LTableTest extends TestCase {
|
|||||||
for ( int i = 0; i < 10; ++i ) {
|
for ( int i = 0; i < 10; ++i ) {
|
||||||
LString str = new LString( String.valueOf( i ) );
|
LString str = new LString( String.valueOf( i ) );
|
||||||
t.put( i, str );
|
t.put( i, str );
|
||||||
t.put( str, new LInteger( i ) );
|
t.put( str, LInteger.valueOf( i ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue( t.getArrayCapacity() >= 9 ); // 1, 2, ..., 9
|
assertTrue( t.getArrayCapacity() >= 9 ); // 1, 2, ..., 9
|
||||||
@@ -105,7 +105,7 @@ public class LTableTest extends TestCase {
|
|||||||
t.put( "test", LNil.NIL );
|
t.put( "test", LNil.NIL );
|
||||||
assertEquals( 0, t.size() );
|
assertEquals( 0, t.size() );
|
||||||
|
|
||||||
t.put( 10, new LInteger( 5 ) );
|
t.put( 10, LInteger.valueOf( 5 ) );
|
||||||
t.put( 10, LNil.NIL );
|
t.put( 10, LNil.NIL );
|
||||||
assertEquals( 0, t.size() );
|
assertEquals( 0, t.size() );
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ public class LTableTest extends TestCase {
|
|||||||
LTable t = new LTable(0, 1);
|
LTable t = new LTable(0, 1);
|
||||||
|
|
||||||
t.put( "test", new LString("foo") );
|
t.put( "test", new LString("foo") );
|
||||||
t.put( "string", new LInteger( 10 ) );
|
t.put( "string", LInteger.valueOf( 10 ) );
|
||||||
assertEquals( 2, t.size() );
|
assertEquals( 2, t.size() );
|
||||||
|
|
||||||
t.put( "string", LNil.NIL );
|
t.put( "string", LNil.NIL );
|
||||||
@@ -124,7 +124,7 @@ public class LTableTest extends TestCase {
|
|||||||
t.put( "test", LNil.NIL );
|
t.put( "test", LNil.NIL );
|
||||||
assertEquals( 1, t.size() );
|
assertEquals( 1, t.size() );
|
||||||
|
|
||||||
t.put( 10, new LInteger( 5 ) );
|
t.put( 10, LInteger.valueOf( 5 ) );
|
||||||
assertEquals( 2, t.size() );
|
assertEquals( 2, t.size() );
|
||||||
|
|
||||||
t.put( 10, LNil.NIL );
|
t.put( 10, LNil.NIL );
|
||||||
|
|||||||
Reference in New Issue
Block a user