updated luajava features.

This commit is contained in:
James Roseborough
2008-05-28 00:17:16 +00:00
parent 2c857d3cb3
commit a34591851d
4 changed files with 115 additions and 25 deletions

View File

@@ -53,6 +53,12 @@ public class CoerceJavaToLua {
return LInteger.valueOf( n.intValue() ); return LInteger.valueOf( n.intValue() );
} }
} ; } ;
Coercion charCoercion = new Coercion() {
public LValue coerce( Object javaValue ) {
Character c = (Character) javaValue;
return LInteger.valueOf( c.charValue() );
}
} ;
Coercion doubleCoercion = new Coercion() { Coercion doubleCoercion = new Coercion() {
public LValue coerce( Object javaValue ) { public LValue coerce( Object javaValue ) {
Number n = (Number) javaValue; Number n = (Number) javaValue;
@@ -66,6 +72,7 @@ public class CoerceJavaToLua {
} ; } ;
COERCIONS.put( Boolean.class, boolCoercion ); COERCIONS.put( Boolean.class, boolCoercion );
COERCIONS.put( Byte.class, intCoercion ); COERCIONS.put( Byte.class, intCoercion );
COERCIONS.put( Character.class, charCoercion );
COERCIONS.put( Short.class, intCoercion ); COERCIONS.put( Short.class, intCoercion );
COERCIONS.put( Integer.class, intCoercion ); COERCIONS.put( Integer.class, intCoercion );
COERCIONS.put( Float.class, doubleCoercion ); COERCIONS.put( Float.class, doubleCoercion );

View File

@@ -57,6 +57,42 @@ public class CoerceLuaToJava {
return 4; return 4;
} }
}; };
Coercion byteCoercion = new Coercion() {
public Object coerce(LValue value) {
return value.toJavaByte();
}
public int score(LValue value) {
if ( value instanceof LInteger )
return 1;
if ( value instanceof LNumber )
return 2;
return 4;
}
};
Coercion charCoercion = new Coercion() {
public Object coerce(LValue value) {
return value.toJavaChar();
}
public int score(LValue value) {
if ( value instanceof LInteger )
return 1;
if ( value instanceof LNumber )
return 2;
return 4;
}
};
Coercion shortCoercion = new Coercion() {
public Object coerce(LValue value) {
return value.toJavaShort();
}
public int score(LValue value) {
if ( value instanceof LInteger )
return 1;
if ( value instanceof LNumber )
return 2;
return 4;
}
};
Coercion intCoercion = new Coercion() { Coercion intCoercion = new Coercion() {
public Object coerce(LValue value) { public Object coerce(LValue value) {
return new Integer( value.toJavaInt() ); return new Integer( value.toJavaInt() );
@@ -71,6 +107,18 @@ public class CoerceLuaToJava {
return 4; return 4;
} }
}; };
Coercion longCoercion = new Coercion() {
public Object coerce(LValue value) {
return value.toJavaLong();
}
public int score(LValue value) {
if ( value instanceof LInteger )
return 1;
if ( value instanceof LNumber )
return 2;
return 4;
}
};
Coercion doubleCoercion = new Coercion() { Coercion doubleCoercion = new Coercion() {
public Object coerce(LValue value) { public Object coerce(LValue value) {
return new Double( value.toJavaDouble() ); return new Double( value.toJavaDouble() );
@@ -80,7 +128,7 @@ public class CoerceLuaToJava {
return 0; return 0;
if ( value instanceof LNumber ) if ( value instanceof LNumber )
return 1; return 1;
if ( value instanceof LBoolean || value.isNil() ) if ( value instanceof LBoolean )
return 2; return 2;
return 4; return 4;
} }
@@ -119,14 +167,16 @@ public class CoerceLuaToJava {
}; };
COERCIONS.put( Boolean.TYPE, boolCoercion ); COERCIONS.put( Boolean.TYPE, boolCoercion );
COERCIONS.put( Boolean.class, boolCoercion ); COERCIONS.put( Boolean.class, boolCoercion );
COERCIONS.put( Byte.TYPE, intCoercion ); COERCIONS.put( Byte.TYPE, byteCoercion );
COERCIONS.put( Byte.class, intCoercion ); COERCIONS.put( Byte.class, byteCoercion );
COERCIONS.put( Short.TYPE, intCoercion ); COERCIONS.put( Character.TYPE, charCoercion );
COERCIONS.put( Short.class, intCoercion ); COERCIONS.put( Character.class, charCoercion );
COERCIONS.put( Short.TYPE, shortCoercion );
COERCIONS.put( Short.class, shortCoercion );
COERCIONS.put( Integer.TYPE, intCoercion ); COERCIONS.put( Integer.TYPE, intCoercion );
COERCIONS.put( Integer.class, intCoercion ); COERCIONS.put( Integer.class, intCoercion );
COERCIONS.put( Long.TYPE, intCoercion ); COERCIONS.put( Long.TYPE, longCoercion );
COERCIONS.put( Long.class, intCoercion ); COERCIONS.put( Long.class, longCoercion );
COERCIONS.put( Double.TYPE, doubleCoercion ); COERCIONS.put( Double.TYPE, doubleCoercion );
COERCIONS.put( Double.class, doubleCoercion ); COERCIONS.put( Double.class, doubleCoercion );
COERCIONS.put( String.class, stringCoercion ); COERCIONS.put( String.class, stringCoercion );

View File

@@ -52,15 +52,17 @@ public final class LuajavaLib extends LFunction {
globals.put( "luajava", luajava ); globals.put( "luajava", luajava );
} }
private static final int NEWINSTANCE = 0; private static final int INIT = 0;
private static final int BINDCLASS = 1; private static final int BINDCLASS = 1;
private static final int NEW = 2; private static final int NEWINSTANCE = 2;
private static final int CREATEPROXY = 3; private static final int NEW = 3;
private static final int LOADLIB = 4; private static final int CREATEPROXY = 4;
private static final int LOADLIB = 5;
private static final String[] NAMES = { private static final String[] NAMES = {
"newInstance", "luajava",
"bindClass", "bindClass",
"newInstance",
"new", "new",
"createProxy", "createProxy",
"loadLib" }; "loadLib" };
@@ -68,6 +70,10 @@ public final class LuajavaLib extends LFunction {
private static final Map classMetatables = new HashMap(); private static final Map classMetatables = new HashMap();
private int id; private int id;
public LuajavaLib() {
}
private LuajavaLib( int id ) { private LuajavaLib( int id ) {
this.id = id; this.id = id;
} }
@@ -80,6 +86,9 @@ public final class LuajavaLib extends LFunction {
public boolean luaStackCall(LuaState vm) { public boolean luaStackCall(LuaState vm) {
String className; String className;
switch ( id ) { switch ( id ) {
case INIT:
install(vm._G);
break;
case BINDCLASS: case BINDCLASS:
className = vm.tostring(2); className = vm.tostring(2);
try { try {
@@ -91,10 +100,11 @@ public final class LuajavaLib extends LFunction {
} }
break; break;
case NEWINSTANCE: case NEWINSTANCE:
className = vm.tostring(2); case NEW:
try { try {
// get constructor // get constructor
Class clazz = Class.forName(className); LValue c = vm.topointer(2);
Class clazz = (id==NEWINSTANCE? Class.forName(c.toJavaString()): (Class) c.toJavaInstance());
ParamsList params = new ParamsList( vm ); ParamsList params = new ParamsList( vm );
Constructor con = resolveConstructor( clazz, params ); Constructor con = resolveConstructor( clazz, params );
@@ -110,6 +120,28 @@ public final class LuajavaLib extends LFunction {
throw new LuaErrorException(e); throw new LuaErrorException(e);
} }
break; break;
case CREATEPROXY:
break;
case LOADLIB:
try {
// get constructor
String classname = vm.tostring(2);
String methodname = vm.tostring(3);
Class clazz = Class.forName(classname);
Method method = clazz.getMethod(methodname, new Class[] { LuaState.class });
Object result = method.invoke(clazz, new Object[] { vm });
if ( result instanceof Integer ) {
int nresults = ((Integer)result).intValue();
int nremove = vm.gettop() - nresults;
for ( int i=0; i<nremove; i++ )
vm.remove(1);
} else {
vm.resettop();
}
} catch (Exception e) {
throw new LuaErrorException(e);
}
break;
default: default:
throw new LuaErrorException("not yet supported: "+this); throw new LuaErrorException("not yet supported: "+this);
} }
@@ -121,7 +153,7 @@ public final class LuajavaLib extends LFunction {
public final Class[] classes; public final Class[] classes;
public int hash; public int hash;
ParamsList( LuaState vm ) { ParamsList( LuaState vm ) {
int n = vm.gettop()-2; int n = Math.max(vm.gettop()-2,0);
values = new LValue[n]; values = new LValue[n];
classes = new Class[n]; classes = new Class[n];
for ( int i=0; i<n; i++ ) { for ( int i=0; i<n; i++ ) {
@@ -140,21 +172,22 @@ public final class LuajavaLib extends LFunction {
} }
} }
static LUserData toUserdata(final Object instance, final Class clazz) { static LUserData toUserdata(Object instance, final Class clazz) {
LTable mt = (LTable) classMetatables.get(clazz); LTable mt = (LTable) classMetatables.get(clazz);
if ( mt == null ) { if ( mt == null ) {
mt = new LTable(); mt = new LTable();
mt.put( LValue.TM_INDEX, new LFunction() { mt.put( LValue.TM_INDEX, new LFunction() {
public boolean luaStackCall(LuaState vm) { public boolean luaStackCall(LuaState vm) {
LValue table = vm.topointer(2);
LValue key = vm.topointer(3); LValue key = vm.topointer(3);
final String s = key.toJavaString(); final String s = key.toJavaString();
vm.resettop(); vm.resettop();
try { try {
Field f = clazz.getField(s); Field f = clazz.getField(s);
Object o = f.get(instance); Object o = f.get(table.toJavaInstance());
vm.pushlvalue( CoerceJavaToLua.coerce( o ) ); vm.pushlvalue( CoerceJavaToLua.coerce( o ) );
} catch (NoSuchFieldException nsfe) { } catch (NoSuchFieldException nsfe) {
vm.pushlvalue( new LMethod(instance,clazz,s) ); vm.pushlvalue( new LMethod(clazz,s) );
} catch (Exception e) { } catch (Exception e) {
throw new LuaErrorException(e); throw new LuaErrorException(e);
} }
@@ -163,13 +196,14 @@ public final class LuajavaLib extends LFunction {
}); });
mt.put( LValue.TM_NEWINDEX, new LFunction() { mt.put( LValue.TM_NEWINDEX, new LFunction() {
public boolean luaStackCall(LuaState vm) { public boolean luaStackCall(LuaState vm) {
LValue table = vm.topointer(2);
LValue key = vm.topointer(3); LValue key = vm.topointer(3);
LValue val = vm.topointer(4); LValue val = vm.topointer(4);
String s = key.toJavaString(); String s = key.toJavaString();
try { try {
Field f = clazz.getField(s); Field f = clazz.getField(s);
Object v = CoerceLuaToJava.coerceArg(val, f.getType()); Object v = CoerceLuaToJava.coerceArg(val, f.getType());
f.set(instance,v); f.set(table.toJavaInstance(),v);
vm.resettop(); vm.resettop();
} catch (Exception e) { } catch (Exception e) {
throw new LuaErrorException(e); throw new LuaErrorException(e);
@@ -183,11 +217,9 @@ public final class LuajavaLib extends LFunction {
} }
private static final class LMethod extends LFunction { private static final class LMethod extends LFunction {
private final Object instance;
private final Class clazz; private final Class clazz;
private final String s; private final String s;
private LMethod(Object instance, Class clazz, String s) { private LMethod(Class clazz, String s) {
this.instance = instance;
this.clazz = clazz; this.clazz = clazz;
this.s = s; this.s = s;
} }
@@ -197,6 +229,7 @@ public final class LuajavaLib extends LFunction {
public boolean luaStackCall(LuaState vm) { public boolean luaStackCall(LuaState vm) {
try { try {
// find the method // find the method
Object instance = vm.touserdata(2);
ParamsList params = new ParamsList( vm ); ParamsList params = new ParamsList( vm );
Method meth = resolveMethod( clazz, s, params ); Method meth = resolveMethod( clazz, s, params );

View File

@@ -1 +1 @@
version: 0.34 version: 0.35