Add LValue.toJavaX and VM.pushboxedx() functions for all java basic type conversions
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
bin
|
bin
|
||||||
target
|
target
|
||||||
build
|
build
|
||||||
luaj-vm-1.0.jar
|
luaj*.jar
|
||||||
|
|||||||
@@ -88,10 +88,10 @@ public class DumpState {
|
|||||||
// do nothing more
|
// do nothing more
|
||||||
} else if (o instanceof LBoolean) {
|
} else if (o instanceof LBoolean) {
|
||||||
writer.write(Lua.LUA_TBOOLEAN);
|
writer.write(Lua.LUA_TBOOLEAN);
|
||||||
dumpChar(o.luaAsBoolean() ? 1 : 0);
|
dumpChar(o.toJavaBoolean() ? 1 : 0);
|
||||||
} else if (o instanceof LNumber) {
|
} else if (o instanceof LNumber) {
|
||||||
writer.write(Lua.LUA_TNUMBER);
|
writer.write(Lua.LUA_TNUMBER);
|
||||||
dumpNumber(o.luaAsDouble());
|
dumpNumber(o.toJavaDouble());
|
||||||
} else if (o instanceof LString) {
|
} else if (o instanceof LString) {
|
||||||
writer.write(Lua.LUA_TSTRING);
|
writer.write(Lua.LUA_TSTRING);
|
||||||
dumpString((LString) o);
|
dumpString((LString) o);
|
||||||
|
|||||||
@@ -432,7 +432,7 @@ public class FuncState extends LuaC {
|
|||||||
|
|
||||||
int numberK(LNumber r) {
|
int numberK(LNumber r) {
|
||||||
if ( r instanceof LDouble ) {
|
if ( r instanceof LDouble ) {
|
||||||
double d = r.luaAsDouble();
|
double d = r.toJavaDouble();
|
||||||
int i = (int) d;
|
int i = (int) d;
|
||||||
if ( d == (double) i )
|
if ( d == (double) i )
|
||||||
r = LInteger.valueOf(i);
|
r = LInteger.valueOf(i);
|
||||||
@@ -833,7 +833,7 @@ public class FuncState extends LuaC {
|
|||||||
r = null;
|
r = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (Double.NaN == r.luaAsDouble())
|
if (Double.NaN == r.toJavaDouble())
|
||||||
return false; /* do not attempt to produce NaN */
|
return false; /* do not attempt to produce NaN */
|
||||||
e1.u.setNval( r );
|
e1.u.setNval( r );
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -370,7 +370,7 @@ public class LuaCompat extends LFunction {
|
|||||||
if ( arg instanceof LNumber ) {
|
if ( arg instanceof LNumber ) {
|
||||||
final int start;
|
final int start;
|
||||||
final int numResults;
|
final int numResults;
|
||||||
if ( ( start = arg.luaAsInt() ) > 0 &&
|
if ( ( start = arg.toJavaInt() ) > 0 &&
|
||||||
( numResults = Math.max( vm.getArgCount() - start,
|
( numResults = Math.max( vm.getArgCount() - start,
|
||||||
vm.getExpectedResultCount() ) ) > 0 ) {
|
vm.getExpectedResultCount() ) ) > 0 ) {
|
||||||
// since setResult trashes the arguments, we have to save them somewhere.
|
// since setResult trashes the arguments, we have to save them somewhere.
|
||||||
@@ -384,7 +384,7 @@ public class LuaCompat extends LFunction {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if ( arg.luaAsString().equals( "#" ) ) {
|
} else if ( arg.toJavaString().equals( "#" ) ) {
|
||||||
vm.setResult( LInteger.valueOf( vm.getArgCount() - 1 ) );
|
vm.setResult( LInteger.valueOf( vm.getArgCount() - 1 ) );
|
||||||
}
|
}
|
||||||
vm.setResult();
|
vm.setResult();
|
||||||
@@ -405,7 +405,7 @@ public class LuaCompat extends LFunction {
|
|||||||
|
|
||||||
private void modf( VM vm ) {
|
private void modf( VM vm ) {
|
||||||
LValue arg = vm.getArg( 0 );
|
LValue arg = vm.getArg( 0 );
|
||||||
double v = arg.luaAsDouble();
|
double v = arg.toJavaDouble();
|
||||||
double intPart = ( v > 0 ) ? Math.floor( v ) : Math.ceil( v );
|
double intPart = ( v > 0 ) ? Math.floor( v ) : Math.ceil( v );
|
||||||
double fracPart = v - intPart;
|
double fracPart = v - intPart;
|
||||||
vm.setResult();
|
vm.setResult();
|
||||||
@@ -439,7 +439,7 @@ public class LuaCompat extends LFunction {
|
|||||||
if ( f instanceof Closure ) {
|
if ( f instanceof Closure ) {
|
||||||
c = (Closure) f;
|
c = (Closure) f;
|
||||||
} else {
|
} else {
|
||||||
int callStackDepth = f.luaAsInt();
|
int callStackDepth = f.toJavaInt();
|
||||||
if ( callStackDepth > 0 ) {
|
if ( callStackDepth > 0 ) {
|
||||||
CallInfo frame = state.getStackFrame( callStackDepth );
|
CallInfo frame = state.getStackFrame( callStackDepth );
|
||||||
if ( frame != null ) {
|
if ( frame != null ) {
|
||||||
|
|||||||
@@ -536,7 +536,7 @@ public class StrLib {
|
|||||||
}
|
}
|
||||||
|
|
||||||
repl = vm.topointer( -1 );
|
repl = vm.topointer( -1 );
|
||||||
if ( !repl.luaAsBoolean() ) {
|
if ( !repl.toJavaBoolean() ) {
|
||||||
repl = s.substring( soffset, end );
|
repl = s.substring( soffset, end );
|
||||||
} else if ( ! ( repl instanceof LString || repl instanceof LNumber ) ) {
|
} else if ( ! ( repl instanceof LString || repl instanceof LNumber ) ) {
|
||||||
vm.error( "invalid replacement value (a "+repl.luaGetTypeName()+")" );
|
vm.error( "invalid replacement value (a "+repl.luaGetTypeName()+")" );
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public class CoerceLuaToJava {
|
|||||||
static {
|
static {
|
||||||
Coercion boolCoercion = new Coercion() {
|
Coercion boolCoercion = new Coercion() {
|
||||||
public Object coerce(LValue value) {
|
public Object coerce(LValue value) {
|
||||||
return value.luaAsBoolean()? Boolean.TRUE: Boolean.FALSE;
|
return value.toJavaBoolean()? Boolean.TRUE: Boolean.FALSE;
|
||||||
}
|
}
|
||||||
public int score(LValue value) {
|
public int score(LValue value) {
|
||||||
if ( value instanceof LBoolean || value == LNil.NIL )
|
if ( value instanceof LBoolean || value == LNil.NIL )
|
||||||
@@ -37,7 +37,7 @@ public class CoerceLuaToJava {
|
|||||||
};
|
};
|
||||||
Coercion intCoercion = new Coercion() {
|
Coercion intCoercion = new Coercion() {
|
||||||
public Object coerce(LValue value) {
|
public Object coerce(LValue value) {
|
||||||
return Integer.valueOf( value.luaAsInt() );
|
return Integer.valueOf( value.toJavaInt() );
|
||||||
}
|
}
|
||||||
public int score(LValue value) {
|
public int score(LValue value) {
|
||||||
if ( value instanceof LInteger )
|
if ( value instanceof LInteger )
|
||||||
@@ -51,7 +51,7 @@ public class CoerceLuaToJava {
|
|||||||
};
|
};
|
||||||
Coercion doubleCoercion = new Coercion() {
|
Coercion doubleCoercion = new Coercion() {
|
||||||
public Object coerce(LValue value) {
|
public Object coerce(LValue value) {
|
||||||
return Double.valueOf( value.luaAsDouble() );
|
return Double.valueOf( value.toJavaDouble() );
|
||||||
}
|
}
|
||||||
public int score(LValue value) {
|
public int score(LValue value) {
|
||||||
if ( value instanceof LDouble )
|
if ( value instanceof LDouble )
|
||||||
@@ -65,7 +65,7 @@ public class CoerceLuaToJava {
|
|||||||
};
|
};
|
||||||
Coercion stringCoercion = new Coercion() {
|
Coercion stringCoercion = new Coercion() {
|
||||||
public Object coerce(LValue value) {
|
public Object coerce(LValue value) {
|
||||||
return value.luaAsString().toJavaString();
|
return value.toJavaString();
|
||||||
}
|
}
|
||||||
public int score(LValue value) {
|
public int score(LValue value) {
|
||||||
if ( value instanceof LUserData )
|
if ( value instanceof LUserData )
|
||||||
@@ -78,13 +78,13 @@ public class CoerceLuaToJava {
|
|||||||
if ( value instanceof LUserData )
|
if ( value instanceof LUserData )
|
||||||
return ((LUserData)value).m_instance;
|
return ((LUserData)value).m_instance;
|
||||||
if ( value instanceof LString )
|
if ( value instanceof LString )
|
||||||
return value.luaAsString().toJavaString();
|
return value.toJavaString();
|
||||||
if ( value instanceof LInteger )
|
if ( value instanceof LInteger )
|
||||||
return Integer.valueOf(value.luaAsInt());
|
return Integer.valueOf(value.toJavaInt());
|
||||||
if ( value instanceof LDouble )
|
if ( value instanceof LDouble )
|
||||||
return Double.valueOf(value.luaAsDouble());
|
return Double.valueOf(value.toJavaDouble());
|
||||||
if ( value instanceof LBoolean )
|
if ( value instanceof LBoolean )
|
||||||
return Boolean.valueOf(value.luaAsBoolean());
|
return Boolean.valueOf(value.toJavaBoolean());
|
||||||
if ( value == LNil.NIL )
|
if ( value == LNil.NIL )
|
||||||
return null;
|
return null;
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ public final class LuaJava extends LFunction {
|
|||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
}
|
}
|
||||||
public void luaGetTable(VM vm, LValue table, LValue key) {
|
public void luaGetTable(VM vm, LValue table, LValue key) {
|
||||||
final String s = key.luaAsString().toJavaString();
|
final String s = key.toJavaString();
|
||||||
try {
|
try {
|
||||||
Field f = clazz.getField(s);
|
Field f = clazz.getField(s);
|
||||||
Object o = f.get(m_instance);
|
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) {
|
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
|
||||||
Class c = m_instance.getClass();
|
Class c = m_instance.getClass();
|
||||||
String s = key.luaAsString().toJavaString();
|
String s = key.toJavaString();
|
||||||
try {
|
try {
|
||||||
Field f = c.getField(s);
|
Field f = c.getField(s);
|
||||||
Object v = CoerceLuaToJava.coerceArg(val, f.getType());
|
Object v = CoerceLuaToJava.coerceArg(val, f.getType());
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ final class Builtin extends JavaFunction {
|
|||||||
for ( int i=1; i<=n; i++ ) {
|
for ( int i=1; i<=n; i++ ) {
|
||||||
if ( i > 1 )
|
if ( i > 1 )
|
||||||
stdout.print( "\t" );
|
stdout.print( "\t" );
|
||||||
stdout.print( vm.topointer(i).luaAsString() );
|
stdout.print( vm.topointer(i).toJavaString() );
|
||||||
}
|
}
|
||||||
stdout.println();
|
stdout.println();
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1407,4 +1407,103 @@ public interface VM {
|
|||||||
*/
|
*/
|
||||||
public void yield(int nresults);
|
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 )
|
if ( p.lineinfo != null && p.lineinfo.length > call.pc )
|
||||||
line = String.valueOf( p.lineinfo[call.pc] );
|
line = String.valueOf( p.lineinfo[call.pc] );
|
||||||
// TODO: reverse lookup on function name ????
|
// TODO: reverse lookup on function name ????
|
||||||
func = call.closure.luaAsString().toJavaString();
|
func = call.closure.toJavaString();
|
||||||
}
|
}
|
||||||
return source+":"+line+"("+func+")";
|
return source+":"+line+"("+func+")";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,23 +8,29 @@ public final class LBoolean extends LValue {
|
|||||||
|
|
||||||
public static final LBoolean FALSE = new LBoolean("false",false);
|
public static final LBoolean FALSE = new LBoolean("false",false);
|
||||||
|
|
||||||
|
private final String m_sname;
|
||||||
private final LString m_name;
|
private final LString m_name;
|
||||||
private final boolean m_value;
|
private final boolean m_value;
|
||||||
|
|
||||||
private LBoolean( String name, boolean value ) {
|
private LBoolean( String name, boolean value ) {
|
||||||
|
this.m_sname = name;
|
||||||
this.m_name = new LString( name );
|
this.m_name = new LString( name );
|
||||||
this.m_value = value;
|
this.m_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final String toJavaString() {
|
||||||
|
return m_sname;
|
||||||
|
}
|
||||||
|
|
||||||
public final LString luaAsString() {
|
public final LString luaAsString() {
|
||||||
return m_name;
|
return m_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final boolean luaAsBoolean() {
|
public final boolean toJavaBoolean() {
|
||||||
return m_value;
|
return m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final int luaAsInt() {
|
public final int toJavaInt() {
|
||||||
return m_value? 1: 0;
|
return m_value? 1: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,16 +18,17 @@ public class LDouble extends LNumber {
|
|||||||
return (int) m_value;
|
return (int) m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LString luaAsString() {
|
public String toJavaString() {
|
||||||
long l = (long) m_value;
|
long l = (long) m_value;
|
||||||
if ( m_value == (double) l ) {
|
if ( m_value == (double) l ) {
|
||||||
// TODO: is this a good idea?
|
// TODO: is this a good idea?
|
||||||
return new LString( Long.toString( l ) );
|
return Long.toString( l );
|
||||||
} else {
|
} else {
|
||||||
return LString.valueOf( m_value );
|
return Double.toString( m_value );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isInteger() {
|
public boolean isInteger() {
|
||||||
// Cast to int and then back to double and see if the value
|
// Cast to int and then back to double and see if the value
|
||||||
// survives the round trip.
|
// survives the round trip.
|
||||||
@@ -81,11 +82,11 @@ public class LDouble extends LNumber {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public int luaAsInt() {
|
public int toJavaInt() {
|
||||||
return (int) m_value;
|
return (int) m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double luaAsDouble() {
|
public double toJavaDouble() {
|
||||||
return m_value;
|
return m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import lua.VM;
|
|||||||
|
|
||||||
public class LFunction extends LValue {
|
public class LFunction extends LValue {
|
||||||
|
|
||||||
public LString luaAsString() {
|
public String toJavaString() {
|
||||||
return new LString( "function: "+hashCode() );
|
return "function: "+hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
|
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class LInteger extends LNumber {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int luaAsInt() {
|
public int toJavaInt() {
|
||||||
return m_value;
|
return m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +42,10 @@ public class LInteger extends LNumber {
|
|||||||
return LString.valueOf(m_value);
|
return LString.valueOf(m_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toJavaString() {
|
||||||
|
return String.valueOf(m_value);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isInteger() {
|
public boolean isInteger() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ public final class LNil extends LValue {
|
|||||||
return luaGetTypeName();
|
return luaGetTypeName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean luaAsBoolean() {
|
public boolean toJavaBoolean() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,8 +17,39 @@ public final class LNil extends LValue {
|
|||||||
return Lua.LUA_TNIL;
|
return Lua.LUA_TNIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int luaAsInt() {
|
public int toJavaInt() {
|
||||||
return 0;
|
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.
|
* override.
|
||||||
*/
|
*/
|
||||||
public abstract boolean isInteger();
|
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 ) );
|
return new LString( String.valueOf( x ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static LString valueOf(String s) {
|
||||||
|
return new LString( s );
|
||||||
|
}
|
||||||
|
|
||||||
public static LString concat( final LString[] strings ) {
|
public static LString concat( final LString[] strings ) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
for ( int i = 0; i < strings.length; ++i ) {
|
for ( int i = 0; i < strings.length; ++i ) {
|
||||||
@@ -396,4 +400,5 @@ public class LString extends LValue {
|
|||||||
return m_bytes[m_offset + index] & 0x0FF;
|
return m_bytes[m_offset + index] & 0x0FF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ public class LTable extends LValue {
|
|||||||
public void put( LValue key, LValue val ) {
|
public void put( LValue key, LValue val ) {
|
||||||
if ( key.isInteger() ) {
|
if ( key.isInteger() ) {
|
||||||
// call the integer-specific put method
|
// call the integer-specific put method
|
||||||
put( key.luaAsInt(), val );
|
put( key.toJavaInt(), val );
|
||||||
} else if ( val == null || val == LNil.NIL ) {
|
} else if ( val == null || val == LNil.NIL ) {
|
||||||
// Remove the key if the value is nil. This comes after the check
|
// Remove the key if the value is nil. This comes after the check
|
||||||
// for an integer key so that values are properly removed from
|
// 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 ) {
|
public LValue get( LValue key ) {
|
||||||
if ( m_vector.length > 0 && key.isInteger() ) {
|
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 ) {
|
if ( index >= 0 && index < m_vector.length ) {
|
||||||
return m_vector[index];
|
return m_vector[index];
|
||||||
}
|
}
|
||||||
@@ -201,7 +201,7 @@ public class LTable extends LValue {
|
|||||||
*/
|
*/
|
||||||
public boolean containsKey( LValue key ) {
|
public boolean containsKey( LValue key ) {
|
||||||
if ( m_vector.length > 0 && key.isInteger() ) {
|
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 ) {
|
if ( index >= 0 && index < m_vector.length ) {
|
||||||
final LValue v = m_vector[index];
|
final LValue v = m_vector[index];
|
||||||
return v != LNil.NIL;
|
return v != LNil.NIL;
|
||||||
@@ -255,8 +255,8 @@ public class LTable extends LValue {
|
|||||||
(LTable) metatable : null;
|
(LTable) metatable : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LString luaAsString() {
|
public String toJavaString() {
|
||||||
return new LString("table: "+id());
|
return "table: "+id();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int luaGetType() {
|
public int luaGetType() {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ public class LThread extends LValue {
|
|||||||
return Lua.LUA_TTHREAD;
|
return Lua.LUA_TTHREAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LString luaAsString() {
|
public String toJavaString() {
|
||||||
return new LString("thread: "+hashCode());
|
return "thread: "+hashCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ public class LUserData extends LValue {
|
|||||||
m_instance = obj;
|
m_instance = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LString luaAsString() {
|
public String toJavaString() {
|
||||||
return new LString(m_instance.toString());
|
return String.valueOf(m_instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
@@ -31,4 +31,8 @@ public class LUserData extends LValue {
|
|||||||
public LTable luaGetMetatable() {
|
public LTable luaGetMetatable() {
|
||||||
return m_metatable;
|
return m_metatable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object toJavaInstance() {
|
||||||
|
return m_instance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,13 @@ public class LValue {
|
|||||||
throw new java.lang.RuntimeException( "not supported" );
|
throw new java.lang.RuntimeException( "not supported" );
|
||||||
}
|
}
|
||||||
|
|
||||||
public String id() {
|
protected void luaConversionError(String target) {
|
||||||
return Integer.toHexString(hashCode());
|
throw new java.lang.RuntimeException( "bad conversion: "+luaGetTypeName()+" to "+target );
|
||||||
}
|
}
|
||||||
|
|
||||||
// test if value is true
|
|
||||||
public boolean luaAsBoolean() {
|
public String id() {
|
||||||
return true;
|
return Integer.toHexString(hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return true if this value can be represented as an "int" */
|
/** Return true if this value can be represented as an "int" */
|
||||||
@@ -120,24 +120,15 @@ public class LValue {
|
|||||||
vm.push(LNil.NIL);
|
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 */
|
/** Override standard toString with lua String conversion by default */
|
||||||
public String toString() {
|
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 */
|
/** Arithmetic negative */
|
||||||
@@ -186,4 +177,105 @@ public class LValue {
|
|||||||
return LString.LTYPENAMES[luaGetType()];
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class LoadStateTest extends TestCase {
|
|||||||
long bits = Double.doubleToLongBits( v );
|
long bits = Double.doubleToLongBits( v );
|
||||||
LNumber luaNumber = LoadState.longBitsToLuaNumber( bits );
|
LNumber luaNumber = LoadState.longBitsToLuaNumber( bits );
|
||||||
|
|
||||||
assertEquals( v, luaNumber.luaAsDouble(), 0 );
|
assertEquals( v, luaNumber.toJavaDouble(), 0 );
|
||||||
|
|
||||||
if ( v != Integer.MIN_VALUE ) {
|
if ( v != Integer.MIN_VALUE ) {
|
||||||
// Special case of MIN_VALUE is probably not worth dealing with.
|
// Special case of MIN_VALUE is probably not worth dealing with.
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ public class LTableTest extends TestCase {
|
|||||||
LValue k = keys[i];
|
LValue k = keys[i];
|
||||||
|
|
||||||
if ( k instanceof LInteger ) {
|
if ( k instanceof LInteger ) {
|
||||||
final int ik = k.luaAsInt();
|
final int ik = k.toJavaInt();
|
||||||
assertTrue( ik >= 0 && ik < 10 );
|
assertTrue( ik >= 0 && ik < 10 );
|
||||||
final int mask = 1 << ik;
|
final int mask = 1 << ik;
|
||||||
assertTrue( ( intKeys & mask ) == 0 );
|
assertTrue( ( intKeys & mask ) == 0 );
|
||||||
|
|||||||
Reference in New Issue
Block a user