Fix two bugs in LuaJava compatibility addon:

(1) LDouble to Double/double coercions were missing
(2) LuaJava.LMethod.luaStackCall adjusted the top of stack in a way that
produces the wrong results when the call occurs as the argument to another
function (such as print)

Also added two lines to the bottom of test7.lua to test luajava.bindClass.
This commit is contained in:
Ian Farmer
2007-07-04 03:23:09 +00:00
parent a3ffb9a207
commit d987d89092
4 changed files with 19 additions and 1 deletions

View File

@@ -50,6 +50,20 @@ public class CoerceLuaToJava {
return 4; return 4;
} }
}; };
Coercion doubleCoercion = new Coercion() {
public Object coerce(LValue value) {
return Double.valueOf( value.luaAsDouble() );
}
public int score(LValue value) {
if ( value instanceof LDouble )
return 0;
if ( value instanceof LNumber )
return 1;
if ( value instanceof LBoolean || value == LNil.NIL )
return 2;
return 4;
}
};
Coercion stringCoercion = new Coercion() { Coercion stringCoercion = new Coercion() {
public Object coerce(LValue value) { public Object coerce(LValue value) {
return value.luaAsString(); return value.luaAsString();
@@ -92,6 +106,8 @@ public class CoerceLuaToJava {
COERCIONS.put( Integer.class, intCoercion ); COERCIONS.put( Integer.class, intCoercion );
COERCIONS.put( Long.TYPE, intCoercion ); COERCIONS.put( Long.TYPE, intCoercion );
COERCIONS.put( Long.class, intCoercion ); COERCIONS.put( Long.class, intCoercion );
COERCIONS.put( Double.TYPE, doubleCoercion );
COERCIONS.put( Double.class, doubleCoercion );
COERCIONS.put( String.class, stringCoercion ); COERCIONS.put( String.class, stringCoercion );
COERCIONS.put( Object.class, objectCoercion ); COERCIONS.put( Object.class, objectCoercion );
} }

View File

@@ -173,7 +173,6 @@ public final class LuaJava extends LFunction {
Object result = meth.invoke( instance, args ); Object result = meth.invoke( instance, args );
call.stack[base] = CoerceJavaToLua.coerce(result); call.stack[base] = CoerceJavaToLua.coerce(result);
call.top = base + 1; call.top = base + 1;
call.adjustTop(base+nresults);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -9,3 +9,6 @@ print( obj:getS() )
obj:setS( "World" ) obj:setS( "World" )
print( obj.s ) print( obj.s )
math = luajava.bindClass("java.lang.Math")
print("Square root of 9 is", math:sqrt(9.0))

Binary file not shown.