diff --git a/CHANGES.md b/CHANGES.md index 8871e13e82..c107223b8d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -40,6 +40,7 @@ Bug Fixes * [#536](https://github.com/java-native-access/jna/pull/536): Fixed bug in determining the Library and options associated with types defined outside of a Library - [@twall](https://github.com/twall). * [#531](https://github.com/java-native-access/jna/pull/531): Ensure direct-mapped callbacks use the right calling convention - [@twall](https://github.com/twall). * [#566](https://github.com/java-native-access/jna/pull/566): Fix return type of Native#loadLibrary to match unconstrained generic [@lgoldstein](https://github.com/lgoldstein) +* [#584](https://github.com/java-native-access/jna/pull/584): Promote float varargs to double - [@marco2357](https://github.com/marco2357). Release 4.2.1 ============= diff --git a/native/testlib.c b/native/testlib.c index 7080d1d76d..def7eb9ac8 100644 --- a/native/testlib.c +++ b/native/testlib.c @@ -872,6 +872,10 @@ addInt32VarArgs(const char *fmt, ...) { case 'c': sum += (int) va_arg(ap, int); break; + case 'f': // float (promoted to ‘double’ when passed through ‘...’) + case 'F': // double + sum += va_arg(ap, double); + break; default: break; } diff --git a/src/com/sun/jna/Function.java b/src/com/sun/jna/Function.java index de4e9baab8..4d131697d4 100644 --- a/src/com/sun/jna/Function.java +++ b/src/com/sun/jna/Function.java @@ -726,6 +726,12 @@ static Object[] concatenateVarArgs(Object[] inArgs) { Class argType = lastArg != null ? lastArg.getClass() : null; if (argType != null && argType.isArray()) { Object[] varArgs = (Object[])lastArg; + // Promote float varargs to double (https://github.com/java-native-access/jna/issues/463). + for (int i=0; i < varArgs.length; i++) { + if (varArgs[i] instanceof Float) { + varArgs[i] = (double)(Float)varArgs[i]; + } + } Object[] fullArgs = new Object[inArgs.length+varArgs.length]; System.arraycopy(inArgs, 0, fullArgs, 0, inArgs.length-1); System.arraycopy(varArgs, 0, fullArgs, inArgs.length-1, varArgs.length); diff --git a/test/com/sun/jna/VarArgsTest.java b/test/com/sun/jna/VarArgsTest.java index 15b42ff8d3..2654831605 100644 --- a/test/com/sun/jna/VarArgsTest.java +++ b/test/com/sun/jna/VarArgsTest.java @@ -60,6 +60,18 @@ public void testLongVarArgs() { assertEquals("VarArgs not added correctly", arg1 + arg2, lib.addInt32VarArgs("ll", Long.valueOf(arg1), Long.valueOf(arg2))); } + public void testFloatVarArgs() { + float arg1 = 1; + float arg2 = 2; + assertEquals("VarArgs not added correctly", (int)arg1 + (int)arg2, + lib.addInt32VarArgs("ff", Float.valueOf(arg1), Float.valueOf(arg2))); + } + public void testDoubleVarArgs() { + double arg1 = 1; + double arg2 = 2; + assertEquals("VarArgs not added correctly", (int)arg1 + (int)arg2, + lib.addInt32VarArgs("FF", Double.valueOf(arg1), Double.valueOf(arg2))); + } public void testStringVarArgs() { Object[] args = new Object[] { "Test" }; assertEquals("Did not return correct string", args[0],