From d987d8909283a544c6b86c8b51d185d58b428181 Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Wed, 4 Jul 2007 03:23:09 +0000 Subject: [PATCH] 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. --- .../java/lua/addon/luajava/CoerceLuaToJava.java | 16 ++++++++++++++++ src/addon/java/lua/addon/luajava/LuaJava.java | 1 - src/test/res/test7.lua | 3 +++ src/test/res/test7.luac | Bin 489 -> 674 bytes 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/addon/java/lua/addon/luajava/CoerceLuaToJava.java b/src/addon/java/lua/addon/luajava/CoerceLuaToJava.java index 9552b32e..b7ad9915 100644 --- a/src/addon/java/lua/addon/luajava/CoerceLuaToJava.java +++ b/src/addon/java/lua/addon/luajava/CoerceLuaToJava.java @@ -50,6 +50,20 @@ public class CoerceLuaToJava { 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() { public Object coerce(LValue value) { return value.luaAsString(); @@ -92,6 +106,8 @@ public class CoerceLuaToJava { COERCIONS.put( Integer.class, intCoercion ); COERCIONS.put( Long.TYPE, intCoercion ); COERCIONS.put( Long.class, intCoercion ); + COERCIONS.put( Double.TYPE, doubleCoercion ); + COERCIONS.put( Double.class, doubleCoercion ); COERCIONS.put( String.class, stringCoercion ); COERCIONS.put( Object.class, objectCoercion ); } diff --git a/src/addon/java/lua/addon/luajava/LuaJava.java b/src/addon/java/lua/addon/luajava/LuaJava.java index f9b42872..ba196e44 100644 --- a/src/addon/java/lua/addon/luajava/LuaJava.java +++ b/src/addon/java/lua/addon/luajava/LuaJava.java @@ -173,7 +173,6 @@ public final class LuaJava extends LFunction { Object result = meth.invoke( instance, args ); call.stack[base] = CoerceJavaToLua.coerce(result); call.top = base + 1; - call.adjustTop(base+nresults); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/test/res/test7.lua b/src/test/res/test7.lua index c109e778..2df50b50 100644 --- a/src/test/res/test7.lua +++ b/src/test/res/test7.lua @@ -9,3 +9,6 @@ print( obj:getS() ) obj:setS( "World" ) print( obj.s ) + +math = luajava.bindClass("java.lang.Math") +print("Square root of 9 is", math:sqrt(9.0)) diff --git a/src/test/res/test7.luac b/src/test/res/test7.luac index 00cff7a334aab96629368b116b4d3be9d5294b34..9c44ac41f9249bcee8a0f1b1b516f989c7494101 100644 GIT binary patch delta 229 zcmaFKyohyz8WXF*M2&t^RtE+KHU?(~M+asGnFa<%_5(}|tPG3{jt$HVtw4770cS== zMiz!S3=NDjK(%rV4Ge-4_w>fGure?(