From d1f179a1704d3be3ed288ae1be94e826d48b3044 Mon Sep 17 00:00:00 2001 From: Taco Date: Mon, 25 Jan 2016 21:45:07 +0100 Subject: [PATCH 1/2] Promote float varargs to double --- native/testlib.c | 3 +++ src/com/sun/jna/Function.java | 6 ++++++ test/com/sun/jna/VarArgsTest.java | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/native/testlib.c b/native/testlib.c index 7080d1d76d..5cdb681b89 100644 --- a/native/testlib.c +++ b/native/testlib.c @@ -872,6 +872,9 @@ addInt32VarArgs(const char *fmt, ...) { case 'c': sum += (int) va_arg(ap, int); break; + case 'f': + 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..dd67266274 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], From 7f54d4c040dd54bdb3f2f54a238ea13b2f8f9b76 Mon Sep 17 00:00:00 2001 From: Taco Date: Tue, 26 Jan 2016 12:25:23 +0100 Subject: [PATCH 2/2] updated "Promote float varargs to double" bug fix --- CHANGES.md | 1 + native/testlib.c | 3 ++- test/com/sun/jna/VarArgsTest.java | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) 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 5cdb681b89..def7eb9ac8 100644 --- a/native/testlib.c +++ b/native/testlib.c @@ -872,7 +872,8 @@ addInt32VarArgs(const char *fmt, ...) { case 'c': sum += (int) va_arg(ap, int); break; - case 'f': + case 'f': // float (promoted to ‘double’ when passed through ‘...’) + case 'F': // double sum += va_arg(ap, double); break; default: diff --git a/test/com/sun/jna/VarArgsTest.java b/test/com/sun/jna/VarArgsTest.java index dd67266274..2654831605 100644 --- a/test/com/sun/jna/VarArgsTest.java +++ b/test/com/sun/jna/VarArgsTest.java @@ -70,7 +70,7 @@ 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))); + lib.addInt32VarArgs("FF", Double.valueOf(arg1), Double.valueOf(arg2))); } public void testStringVarArgs() { Object[] args = new Object[] { "Test" };