Make LInteger constructor private and use LInteger.valueOf() everywhere, caching common values.
This commit is contained in:
@@ -5,7 +5,24 @@ import lua.Lua;
|
||||
public class LInteger extends LNumber {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -37,12 +54,12 @@ public class LInteger extends LNumber {
|
||||
// binary operations on integers
|
||||
public LValue luaBinOpInteger(int opcode, int rhs) {
|
||||
switch ( opcode ) {
|
||||
case Lua.OP_ADD: return new LInteger( m_value + rhs );
|
||||
case Lua.OP_SUB: return new LInteger( m_value - rhs );
|
||||
case Lua.OP_MUL: return new LInteger( m_value * rhs );
|
||||
case Lua.OP_DIV: return new LInteger( m_value / rhs );
|
||||
case Lua.OP_MOD: return new LInteger( m_value - ((int) Math.floor(m_value/(double)rhs)) * rhs );
|
||||
case Lua.OP_POW: return new LInteger( ipow(m_value, rhs) );
|
||||
case Lua.OP_ADD: return LInteger.valueOf( m_value + rhs );
|
||||
case Lua.OP_SUB: return LInteger.valueOf( m_value - rhs );
|
||||
case Lua.OP_MUL: return LInteger.valueOf( m_value * rhs );
|
||||
case Lua.OP_DIV: return LInteger.valueOf( m_value / rhs );
|
||||
case Lua.OP_MOD: return LInteger.valueOf( m_value - ((int) Math.floor(m_value/(double)rhs)) * rhs );
|
||||
case Lua.OP_POW: return LInteger.valueOf( ipow(m_value, rhs) );
|
||||
}
|
||||
return luaUnsupportedOperation();
|
||||
}
|
||||
@@ -83,8 +100,7 @@ public class LInteger extends LNumber {
|
||||
|
||||
/** Arithmetic negative */
|
||||
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 ) {
|
||||
String str = toJavaString().trim();
|
||||
try {
|
||||
return new LInteger( Integer.parseInt( str, base ) );
|
||||
return LInteger.valueOf( Integer.parseInt( str, base ) );
|
||||
} catch ( NumberFormatException nfe ) {
|
||||
if ( base == 10 ) {
|
||||
try {
|
||||
@@ -284,7 +284,7 @@ public class LString extends LValue {
|
||||
|
||||
/** Built-in opcode LEN, for Strings and Tables */
|
||||
public LValue luaLength() {
|
||||
return new LInteger( length() );
|
||||
return LInteger.valueOf( length() );
|
||||
}
|
||||
|
||||
public LString luaGetType() {
|
||||
|
||||
@@ -163,7 +163,7 @@ public class LTable extends LValue {
|
||||
int slot = findSlot( key );
|
||||
if ( fillHashSlot( slot, value ) )
|
||||
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 ) {
|
||||
if ( m_vector[i] != 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 */
|
||||
@@ -289,7 +289,7 @@ public class LTable extends LValue {
|
||||
int i;
|
||||
while ( ( i = arrayIndex++ ) < m_vector.length ) {
|
||||
if ( m_vector[i] != LNil.NIL ) {
|
||||
vm.push( new LInteger( arrayIndex ) );
|
||||
vm.push( LInteger.valueOf( arrayIndex ) );
|
||||
vm.push( m_vector[ i ] );
|
||||
return false;
|
||||
}
|
||||
@@ -319,7 +319,7 @@ public class LTable extends LValue {
|
||||
|
||||
for ( int i = 0; i < m_vector.length; ++i ) {
|
||||
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())
|
||||
rehash();
|
||||
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_hashEntries;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user