Add LValue.toJavaX and VM.pushboxedx() functions for all java basic type conversions

This commit is contained in:
James Roseborough
2007-10-18 17:47:01 +00:00
parent ccde158514
commit 8e2ff119f9
23 changed files with 344 additions and 66 deletions

View File

@@ -1,4 +1,4 @@
bin
target
build
luaj-vm-1.0.jar
luaj*.jar

View File

@@ -88,10 +88,10 @@ public class DumpState {
// do nothing more
} else if (o instanceof LBoolean) {
writer.write(Lua.LUA_TBOOLEAN);
dumpChar(o.luaAsBoolean() ? 1 : 0);
dumpChar(o.toJavaBoolean() ? 1 : 0);
} else if (o instanceof LNumber) {
writer.write(Lua.LUA_TNUMBER);
dumpNumber(o.luaAsDouble());
dumpNumber(o.toJavaDouble());
} else if (o instanceof LString) {
writer.write(Lua.LUA_TSTRING);
dumpString((LString) o);

View File

@@ -432,7 +432,7 @@ public class FuncState extends LuaC {
int numberK(LNumber r) {
if ( r instanceof LDouble ) {
double d = r.luaAsDouble();
double d = r.toJavaDouble();
int i = (int) d;
if ( d == (double) i )
r = LInteger.valueOf(i);
@@ -833,7 +833,7 @@ public class FuncState extends LuaC {
r = null;
break;
}
if (Double.NaN == r.luaAsDouble())
if (Double.NaN == r.toJavaDouble())
return false; /* do not attempt to produce NaN */
e1.u.setNval( r );
return true;

View File

@@ -370,7 +370,7 @@ public class LuaCompat extends LFunction {
if ( arg instanceof LNumber ) {
final int start;
final int numResults;
if ( ( start = arg.luaAsInt() ) > 0 &&
if ( ( start = arg.toJavaInt() ) > 0 &&
( numResults = Math.max( vm.getArgCount() - start,
vm.getExpectedResultCount() ) ) > 0 ) {
// since setResult trashes the arguments, we have to save them somewhere.
@@ -384,7 +384,7 @@ public class LuaCompat extends LFunction {
}
return;
}
} else if ( arg.luaAsString().equals( "#" ) ) {
} else if ( arg.toJavaString().equals( "#" ) ) {
vm.setResult( LInteger.valueOf( vm.getArgCount() - 1 ) );
}
vm.setResult();
@@ -405,7 +405,7 @@ public class LuaCompat extends LFunction {
private void modf( VM vm ) {
LValue arg = vm.getArg( 0 );
double v = arg.luaAsDouble();
double v = arg.toJavaDouble();
double intPart = ( v > 0 ) ? Math.floor( v ) : Math.ceil( v );
double fracPart = v - intPart;
vm.setResult();
@@ -439,7 +439,7 @@ public class LuaCompat extends LFunction {
if ( f instanceof Closure ) {
c = (Closure) f;
} else {
int callStackDepth = f.luaAsInt();
int callStackDepth = f.toJavaInt();
if ( callStackDepth > 0 ) {
CallInfo frame = state.getStackFrame( callStackDepth );
if ( frame != null ) {

View File

@@ -536,7 +536,7 @@ public class StrLib {
}
repl = vm.topointer( -1 );
if ( !repl.luaAsBoolean() ) {
if ( !repl.toJavaBoolean() ) {
repl = s.substring( soffset, end );
} else if ( ! ( repl instanceof LString || repl instanceof LNumber ) ) {
vm.error( "invalid replacement value (a "+repl.luaGetTypeName()+")" );

View File

@@ -25,7 +25,7 @@ public class CoerceLuaToJava {
static {
Coercion boolCoercion = new Coercion() {
public Object coerce(LValue value) {
return value.luaAsBoolean()? Boolean.TRUE: Boolean.FALSE;
return value.toJavaBoolean()? Boolean.TRUE: Boolean.FALSE;
}
public int score(LValue value) {
if ( value instanceof LBoolean || value == LNil.NIL )
@@ -37,7 +37,7 @@ public class CoerceLuaToJava {
};
Coercion intCoercion = new Coercion() {
public Object coerce(LValue value) {
return Integer.valueOf( value.luaAsInt() );
return Integer.valueOf( value.toJavaInt() );
}
public int score(LValue value) {
if ( value instanceof LInteger )
@@ -51,7 +51,7 @@ public class CoerceLuaToJava {
};
Coercion doubleCoercion = new Coercion() {
public Object coerce(LValue value) {
return Double.valueOf( value.luaAsDouble() );
return Double.valueOf( value.toJavaDouble() );
}
public int score(LValue value) {
if ( value instanceof LDouble )
@@ -65,7 +65,7 @@ public class CoerceLuaToJava {
};
Coercion stringCoercion = new Coercion() {
public Object coerce(LValue value) {
return value.luaAsString().toJavaString();
return value.toJavaString();
}
public int score(LValue value) {
if ( value instanceof LUserData )
@@ -78,13 +78,13 @@ public class CoerceLuaToJava {
if ( value instanceof LUserData )
return ((LUserData)value).m_instance;
if ( value instanceof LString )
return value.luaAsString().toJavaString();
return value.toJavaString();
if ( value instanceof LInteger )
return Integer.valueOf(value.luaAsInt());
return Integer.valueOf(value.toJavaInt());
if ( value instanceof LDouble )
return Double.valueOf(value.luaAsDouble());
return Double.valueOf(value.toJavaDouble());
if ( value instanceof LBoolean )
return Boolean.valueOf(value.luaAsBoolean());
return Boolean.valueOf(value.toJavaBoolean());
if ( value == LNil.NIL )
return null;
return value;

View File

@@ -122,7 +122,7 @@ public final class LuaJava extends LFunction {
this.clazz = clazz;
}
public void luaGetTable(VM vm, LValue table, LValue key) {
final String s = key.luaAsString().toJavaString();
final String s = key.toJavaString();
try {
Field f = clazz.getField(s);
Object o = f.get(m_instance);
@@ -136,7 +136,7 @@ public final class LuaJava extends LFunction {
}
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
Class c = m_instance.getClass();
String s = key.luaAsString().toJavaString();
String s = key.toJavaString();
try {
Field f = c.getField(s);
Object v = CoerceLuaToJava.coerceArg(val, f.getType());

View File

@@ -55,7 +55,7 @@ final class Builtin extends JavaFunction {
for ( int i=1; i<=n; i++ ) {
if ( i > 1 )
stdout.print( "\t" );
stdout.print( vm.topointer(i).luaAsString() );
stdout.print( vm.topointer(i).toJavaString() );
}
stdout.println();
return 0;

View File

@@ -1407,4 +1407,103 @@ public interface VM {
*/
public void yield(int nresults);
// ============================= conversion to and from Java boxed types ====================
/**
* Push a Java Boolean value, or nil if the value is null.
* @param b Boolean value to convert, or null to to nil.
*/
public void pushboolean( Boolean b );
/**
* Push a Java Byte value, or nil if the value is null.
* @param b Byte value to convert, or null to to nil.
*/
public void pushinteger( Byte b );
/**
* Push a Java Character value, or nil if the value is null.
* @param c Character value to convert, or null to to nil.
*/
public void pushinteger( Character c );
/**
* Push a Java Double as a double, or nil if the value is null.
* @param d Double value to convert, or null to to nil.
*/
public void pushnumber( Double d );
/**
* Push a Java Float value, or nil if the value is null.
* @param f Float value to convert, or null to to nil.
*/
public void pushnumber( Float f );
/**
* Push a Java Integer value, or nil if the value is null.
* @param i Integer value to convert, or null to to nil.
*/
public void pushinteger( Integer i );
/**
* Push a Java Short value, or nil if the value is null.
* @param s Short value to convert, or null to to nil.
*/
public void pushinteger( Short s );
/**
* Push a Java Long value, or nil if the value is null.
* @param l Long value to convert, or null to to nil.
*/
public void pushnumber( Long l );
/**
* Push a Java Object as userdata, or nil if the value is null.
* @param o Object value to push, or null to to nil.
*/
public void pushuserdata( Object o );
/**
* Convert a value to a Java Boolean value, or null if the value is nil.
* @param index index of the parameter to convert.
* @return Boolean value at the index, or null if the value was not a boolean.
*/
public Boolean toboxedboolean(int index);
/**
* Convert a value to a Java Byte value, or null if the value is not a number.
* @param index index of the parameter to convert.
* @return Byte value at the index, or null if the value was not a number.
*/
public Byte toboxedbyte(int index);
/**
* Convert a value to a Java Double value, or null if the value is not a number.
* @param index index of the parameter to convert.
* @return Double value at the index, or null if the value was not a number.
*/
public Double toboxeddouble(int index);
/**
* Convert a value to a Java Float value, or null if the value is not a number.
* @param index index of the parameter to convert.
* @return Float value at the index, or null if the value was not a boolean.
*/
public Float toboxedfloat(int index);
/**
* Convert a value to a Java Integer value, or null if the value is not a number.
* @param index index of the parameter to convert.
* @return Integer value at the index, or null if the value was not a number.
*/
public Integer toboxedinteger(int index);
/**
* Convert a value to a Java Long value, or null if the value is nil.
* @param index index of the parameter to convert.
* @return Long value at the index, or null if the value was not a number.
*/
public Long toboxedlong(int index);
}

View File

@@ -89,7 +89,7 @@ public class DebugStackState extends StackState implements DebugRequestListener
if ( p.lineinfo != null && p.lineinfo.length > call.pc )
line = String.valueOf( p.lineinfo[call.pc] );
// TODO: reverse lookup on function name ????
func = call.closure.luaAsString().toJavaString();
func = call.closure.toJavaString();
}
return source+":"+line+"("+func+")";
}

View File

@@ -8,23 +8,29 @@ public final class LBoolean extends LValue {
public static final LBoolean FALSE = new LBoolean("false",false);
private final String m_sname;
private final LString m_name;
private final boolean m_value;
private LBoolean( String name, boolean value ) {
this.m_sname = name;
this.m_name = new LString( name );
this.m_value = value;
}
public final String toJavaString() {
return m_sname;
}
public final LString luaAsString() {
return m_name;
}
public final boolean luaAsBoolean() {
public final boolean toJavaBoolean() {
return m_value;
}
public final int luaAsInt() {
public final int toJavaInt() {
return m_value? 1: 0;
}

View File

@@ -18,16 +18,17 @@ public class LDouble extends LNumber {
return (int) m_value;
}
public LString luaAsString() {
public String toJavaString() {
long l = (long) m_value;
if ( m_value == (double) l ) {
// TODO: is this a good idea?
return new LString( Long.toString( l ) );
return Long.toString( l );
} else {
return LString.valueOf( m_value );
return Double.toString( m_value );
}
}
public boolean isInteger() {
// Cast to int and then back to double and see if the value
// survives the round trip.
@@ -81,11 +82,11 @@ public class LDouble extends LNumber {
*/
public int luaAsInt() {
public int toJavaInt() {
return (int) m_value;
}
public double luaAsDouble() {
public double toJavaDouble() {
return m_value;
}

View File

@@ -6,10 +6,10 @@ import lua.VM;
public class LFunction extends LValue {
public LString luaAsString() {
return new LString( "function: "+hashCode() );
public String toJavaString() {
return "function: "+hashCode();
}
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
vm.push( this );
vm.push( table );

View File

@@ -34,13 +34,17 @@ public class LInteger extends LNumber {
return v;
}
public int luaAsInt() {
public int toJavaInt() {
return m_value;
}
public LString luaAsString() {
return LString.valueOf(m_value);
}
public String toJavaString() {
return String.valueOf(m_value);
}
public boolean isInteger() {
return true;

View File

@@ -9,7 +9,7 @@ public final class LNil extends LValue {
return luaGetTypeName();
}
public boolean luaAsBoolean() {
public boolean toJavaBoolean() {
return false;
}
@@ -17,8 +17,39 @@ public final class LNil extends LValue {
return Lua.LUA_TNIL;
}
public int luaAsInt() {
public int toJavaInt() {
return 0;
}
public String toJavaString() {
return "nil";
}
public Byte toJavaBoxedByte() {
return null;
}
public Character toJavaBoxedCharacter() {
return null;
}
public Double toJavaBoxedDouble() {
return null;
}
public Float toJavaBoxedFloat() {
return null;
}
public Integer toJavaBoxedInteger() {
return null;
}
public Long toJavaBoxedLong() {
return null;
}
public Short toJavaBoxedShort() {
return null;
}
}

View File

@@ -22,4 +22,40 @@ public class LNumber extends LValue {
* override.
*/
public abstract boolean isInteger();
/** Convert to a Byte value */
public Byte toJavaBoxedByte() {
return new Byte(toJavaByte());
}
/** Convert to a boxed Character value */
public Character toJavaBoxedCharacter() {
return new Character(toJavaChar());
}
/** Convert to a boxed Double value */
public Double toJavaBoxedDouble() {
return new Double(toJavaDouble());
}
/** Convert to a boxed Float value */
public Float toJavaBoxedFloat() {
return new Float(toJavaFloat());
}
/** Convert to a boxed Integer value */
public Integer toJavaBoxedInteger() {
return new Integer(toJavaInt());
}
/** Convert to a boxed Long value */
public Long toJavaBoxedLong() {
return new Long(toJavaLong());
}
/** Convert to a boxed Short value */
public Short toJavaBoxedShort() {
return new Short(toJavaShort());
}
}

View File

@@ -165,6 +165,10 @@ public class LString extends LValue {
return new LString( String.valueOf( x ) );
}
public static LString valueOf(String s) {
return new LString( s );
}
public static LString concat( final LString[] strings ) {
int length = 0;
for ( int i = 0; i < strings.length; ++i ) {
@@ -396,4 +400,5 @@ public class LString extends LValue {
return m_bytes[m_offset + index] & 0x0FF;
}
}

View File

@@ -105,7 +105,7 @@ public class LTable extends LValue {
public void put( LValue key, LValue val ) {
if ( key.isInteger() ) {
// call the integer-specific put method
put( key.luaAsInt(), val );
put( key.toJavaInt(), val );
} else if ( val == null || val == LNil.NIL ) {
// Remove the key if the value is nil. This comes after the check
// for an integer key so that values are properly removed from
@@ -171,7 +171,7 @@ public class LTable extends LValue {
*/
public LValue get( LValue key ) {
if ( m_vector.length > 0 && key.isInteger() ) {
final int index = key.luaAsInt() - 1;
final int index = key.toJavaInt() - 1;
if ( index >= 0 && index < m_vector.length ) {
return m_vector[index];
}
@@ -201,7 +201,7 @@ public class LTable extends LValue {
*/
public boolean containsKey( LValue key ) {
if ( m_vector.length > 0 && key.isInteger() ) {
final int index = key.luaAsInt() - 1;
final int index = key.toJavaInt() - 1;
if ( index >= 0 && index < m_vector.length ) {
final LValue v = m_vector[index];
return v != LNil.NIL;
@@ -255,8 +255,8 @@ public class LTable extends LValue {
(LTable) metatable : null;
}
public LString luaAsString() {
return new LString("table: "+id());
public String toJavaString() {
return "table: "+id();
}
public int luaGetType() {

View File

@@ -8,7 +8,7 @@ public class LThread extends LValue {
return Lua.LUA_TTHREAD;
}
public LString luaAsString() {
return new LString("thread: "+hashCode());
public String toJavaString() {
return "thread: "+hashCode();
}
}

View File

@@ -11,8 +11,8 @@ public class LUserData extends LValue {
m_instance = obj;
}
public LString luaAsString() {
return new LString(m_instance.toString());
public String toJavaString() {
return String.valueOf(m_instance);
}
public boolean equals(Object obj) {
@@ -31,4 +31,8 @@ public class LUserData extends LValue {
public LTable luaGetMetatable() {
return m_metatable;
}
public Object toJavaInstance() {
return m_instance;
}
}

View File

@@ -16,15 +16,15 @@ public class LValue {
throw new java.lang.RuntimeException( "not supported" );
}
protected void luaConversionError(String target) {
throw new java.lang.RuntimeException( "bad conversion: "+luaGetTypeName()+" to "+target );
}
public String id() {
return Integer.toHexString(hashCode());
}
// test if value is true
public boolean luaAsBoolean() {
return true;
}
/** Return true if this value can be represented as an "int" */
public boolean isInteger() {
return false;
@@ -120,26 +120,17 @@ public class LValue {
vm.push(LNil.NIL);
}
/** Get the value as a String
/** Get the value as a LString
*/
public abstract LString luaAsString();
public LString luaAsString() {
return new LString(toJavaString());
}
/** Override standard toString with lua String conversion by default */
public String toString() {
return luaAsString().toJavaString();
return toJavaString();
}
/** Return value as an integer */
public int luaAsInt() {
luaUnsupportedOperation();
return 0;
}
/** Return value as a double */
public double luaAsDouble() {
return luaAsInt();
}
/** Arithmetic negative */
public LValue luaUnaryMinus() {
return luaUnsupportedOperation();
@@ -186,4 +177,105 @@ public class LValue {
return LString.LTYPENAMES[luaGetType()];
}
/** Convert to a Java String */
public String toJavaString() {
return null;
}
/** Return value as a boolean */
public boolean toJavaBoolean() {
return true;
}
/** Return value as a byte */
public byte toJavaByte() {
return (byte) toJavaInt();
}
/** Return value as a char */
public char toJavaChar() {
return (char) toJavaInt();
}
/** Return value as a double */
public double toJavaDouble() {
return toJavaInt();
}
/** Return value as a float */
public float toJavaFloat() {
return (float) toJavaDouble();
}
/** Return value as an integer */
public int toJavaInt() {
luaConversionError("number");
return 0;
}
/** Return value as a long */
public long toJavaLong() {
return (long) toJavaDouble();
}
/** Return value as a double */
public short toJavaShort() {
return (short) toJavaInt();
}
/** Convert to a Boolean value */
public Boolean toJavaBoxedBoolean() {
luaConversionError("Boolean");
return null;
}
/** Convert to a Byte value */
public Byte toJavaBoxedByte() {
luaConversionError("Byte");
return null;
}
/** Convert to a boxed Character value */
public Character toJavaBoxedCharacter() {
luaConversionError("Character");
return null;
}
/** Convert to a boxed Double value */
public Double toJavaBoxedDouble() {
luaConversionError("Double");
return null;
}
/** Convert to a boxed Float value */
public Float toJavaBoxedFloat() {
luaConversionError("Float");
return null;
}
/** Convert to a boxed Integer value */
public Integer toJavaBoxedInteger() {
luaConversionError("Integer");
return null;
}
/** Convert to a boxed Long value */
public Long toJavaBoxedLong() {
luaConversionError("Long");
return null;
}
/** Convert to a boxed Short value */
public Short toJavaBoxedShort() {
luaConversionError("Short");
return null;
}
/** Convert to a Java Object iff this is a LUserData value */
public Object toJavaInstance() {
luaConversionError("instance");
return null;
}
}

View File

@@ -39,7 +39,7 @@ public class LoadStateTest extends TestCase {
long bits = Double.doubleToLongBits( v );
LNumber luaNumber = LoadState.longBitsToLuaNumber( bits );
assertEquals( v, luaNumber.luaAsDouble(), 0 );
assertEquals( v, luaNumber.toJavaDouble(), 0 );
if ( v != Integer.MIN_VALUE ) {
// Special case of MIN_VALUE is probably not worth dealing with.

View File

@@ -66,7 +66,7 @@ public class LTableTest extends TestCase {
LValue k = keys[i];
if ( k instanceof LInteger ) {
final int ik = k.luaAsInt();
final int ik = k.toJavaInt();
assertTrue( ik >= 0 && ik < 10 );
final int mask = 1 << ik;
assertTrue( ( intKeys & mask ) == 0 );