diff --git a/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java b/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java index 4ad42262..68d99e43 100644 --- a/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java +++ b/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java @@ -53,6 +53,12 @@ public class CoerceJavaToLua { 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() { public LValue coerce( Object javaValue ) { Number n = (Number) javaValue; @@ -66,6 +72,7 @@ public class CoerceJavaToLua { } ; COERCIONS.put( Boolean.class, boolCoercion ); COERCIONS.put( Byte.class, intCoercion ); + COERCIONS.put( Character.class, charCoercion ); COERCIONS.put( Short.class, intCoercion ); COERCIONS.put( Integer.class, intCoercion ); COERCIONS.put( Float.class, doubleCoercion ); diff --git a/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java b/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java index 7a562787..1ad9b3d5 100644 --- a/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java +++ b/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java @@ -57,6 +57,42 @@ public class CoerceLuaToJava { 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() { public Object coerce(LValue value) { return new Integer( value.toJavaInt() ); @@ -71,6 +107,18 @@ public class CoerceLuaToJava { 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() { public Object coerce(LValue value) { return new Double( value.toJavaDouble() ); @@ -80,7 +128,7 @@ public class CoerceLuaToJava { return 0; if ( value instanceof LNumber ) return 1; - if ( value instanceof LBoolean || value.isNil() ) + if ( value instanceof LBoolean ) return 2; return 4; } @@ -119,14 +167,16 @@ public class CoerceLuaToJava { }; COERCIONS.put( Boolean.TYPE, boolCoercion ); COERCIONS.put( Boolean.class, boolCoercion ); - COERCIONS.put( Byte.TYPE, intCoercion ); - COERCIONS.put( Byte.class, intCoercion ); - COERCIONS.put( Short.TYPE, intCoercion ); - COERCIONS.put( Short.class, intCoercion ); + COERCIONS.put( Byte.TYPE, byteCoercion ); + COERCIONS.put( Byte.class, byteCoercion ); + COERCIONS.put( Character.TYPE, charCoercion ); + COERCIONS.put( Character.class, charCoercion ); + COERCIONS.put( Short.TYPE, shortCoercion ); + COERCIONS.put( Short.class, shortCoercion ); COERCIONS.put( Integer.TYPE, intCoercion ); COERCIONS.put( Integer.class, intCoercion ); - COERCIONS.put( Long.TYPE, intCoercion ); - COERCIONS.put( Long.class, intCoercion ); + COERCIONS.put( Long.TYPE, longCoercion ); + COERCIONS.put( Long.class, longCoercion ); COERCIONS.put( Double.TYPE, doubleCoercion ); COERCIONS.put( Double.class, doubleCoercion ); COERCIONS.put( String.class, stringCoercion ); diff --git a/src/j2se/org/luaj/lib/j2se/LuajavaLib.java b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java index 01926d59..8a2cc7aa 100644 --- a/src/j2se/org/luaj/lib/j2se/LuajavaLib.java +++ b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java @@ -52,15 +52,17 @@ public final class LuajavaLib extends LFunction { globals.put( "luajava", luajava ); } - private static final int NEWINSTANCE = 0; - private static final int BINDCLASS = 1; - private static final int NEW = 2; - private static final int CREATEPROXY = 3; - private static final int LOADLIB = 4; + private static final int INIT = 0; + private static final int BINDCLASS = 1; + private static final int NEWINSTANCE = 2; + private static final int NEW = 3; + private static final int CREATEPROXY = 4; + private static final int LOADLIB = 5; - private static final String[] NAMES = { - "newInstance", + private static final String[] NAMES = { + "luajava", "bindClass", + "newInstance", "new", "createProxy", "loadLib" }; @@ -68,6 +70,10 @@ public final class LuajavaLib extends LFunction { private static final Map classMetatables = new HashMap(); private int id; + + public LuajavaLib() { + } + private LuajavaLib( int id ) { this.id = id; } @@ -80,6 +86,9 @@ public final class LuajavaLib extends LFunction { public boolean luaStackCall(LuaState vm) { String className; switch ( id ) { + case INIT: + install(vm._G); + break; case BINDCLASS: className = vm.tostring(2); try { @@ -91,10 +100,11 @@ public final class LuajavaLib extends LFunction { } break; case NEWINSTANCE: - className = vm.tostring(2); + case NEW: try { // 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 ); Constructor con = resolveConstructor( clazz, params ); @@ -110,6 +120,28 @@ public final class LuajavaLib extends LFunction { throw new LuaErrorException(e); } 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