Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote float varargs to double #584

Merged
merged 2 commits into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============
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