Add LValue.toJavaX and VM.pushboxedx() functions for all java basic type conversions
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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+")";
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 );
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user