Skip to content

Commit

Permalink
Merge pull request #584 from marco2357/master
Browse files Browse the repository at this point in the history
Promote float varargs to double
  • Loading branch information
twall committed Jan 31, 2016
2 parents 30b7a28 + 7f54d4c commit a31715d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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
=============
Expand Down
4 changes: 4 additions & 0 deletions native/testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/com/sun/jna/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions test/com/sun/jna/VarArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit a31715d

Please sign in to comment.