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

Direct Mapping for boolean arrays #816

Merged
merged 2 commits into from
May 27, 2017
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 @@ -11,6 +11,7 @@ Features
* [#783](https://github.com/java-native-access/jna/pull/783): Add Ole32 functions: `OleBuildVersion`, `OleInitialize`, `OleUninitialize`, `OleFlushClipboard`, `OleRun`, add VARIANT conversion functions to OleAuto, add default locale, LCID and LANG to WinNT - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#784](https://github.com/java-native-access/jna/issues/784): Added Solaris Kstat library - [@dbwiddis](https://github.com/dbwiddis).
* [#805](https://github.com/java-native-access/jna/issues/805): Provide a way to pass JNIEnv pointer to native method and support OPTION_ALLOW_OBJECTs for direct mapping - [@ncruces](https://github.com/ncruces).
* [#816](https://github.com/java-native-access/jna/pull/816): Support `boolean[]` in direct mapping - [@ncruces](https://github.com/ncruces).

Bug Fixes
---------
Expand Down
4 changes: 3 additions & 1 deletion native/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ dispatch(JNIEnv *env, void* func, jint flags, jobjectArray args,
}
else if ((*env)->IsInstanceOf(env, arg, classBoolean)) {
c_args[i].i = (*env)->GetBooleanField(env, arg, FID_Boolean_value);
arg_types[i] = &ffi_type_sint32;
arg_types[i] = &ffi_type_uint32;
arg_values[i] = &c_args[i].i;
}
else if ((*env)->IsInstanceOf(env, arg, classByte)) {
Expand Down Expand Up @@ -1849,6 +1849,7 @@ dispatch_direct(ffi_cif* cif, void* volatile resp, void** argp, void *cdata) {
objects[i] = *(void **)args[i]; \
release[i] = (void *)(*env)->Release##Type##ArrayElements; \
elems[i] = *(void **)args[i] = (*env)->Get##Type##ArrayElements(env, objects[i], NULL); } while(0)
case CVT_ARRAY_BOOLEAN: ARRAY(Boolean); break;
case CVT_ARRAY_BYTE: ARRAY(Byte); break;
case CVT_ARRAY_SHORT: ARRAY(Short); break;
case CVT_ARRAY_CHAR: ARRAY(Char); break;
Expand Down Expand Up @@ -1968,6 +1969,7 @@ dispatch_direct(ffi_cif* cif, void* volatile resp, void** argp, void *cdata) {
free(*(void **)args[i]);
break;
case CVT_BUFFER:
case CVT_ARRAY_BOOLEAN:
case CVT_ARRAY_BYTE:
case CVT_ARRAY_SHORT:
case CVT_ARRAY_CHAR:
Expand Down
30 changes: 30 additions & 0 deletions test/com/sun/jna/ArgumentsMarshalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public TestPointerType() { }
int fillFloatBuffer(float[] buf, int len, float value);
int fillDoubleBuffer(double[] buf, int len, double value);

// boolean[] maps to jboolean* (always 8-bit), boolean mapping is 32-bit by default; use byte
int fillInt8Buffer(boolean[] buf, int len, byte value);

// char[] maps to jchar* (always 16-bit), char maps to wchar_t (can be 32-bit); use short
int fillInt16Buffer(char[] buf, int len, short value);

// Nonexistent functions
boolean returnBooleanArgument(Object arg);

Expand Down Expand Up @@ -507,6 +513,20 @@ public void testReadStructureByReferenceArrayArgumentMemory() {
}
}

public void testBooleanArrayArgument() {
boolean[] buf = new boolean[1024];
assertEquals("Wrong return value", buf.length,
lib.fillInt8Buffer(buf, buf.length, (byte)1));
for (int i=0;i < buf.length;i++) {
assertTrue("Bad value at index " + i, buf[i]);
}
assertEquals("Wrong return value", buf.length,
lib.fillInt8Buffer(buf, buf.length, (byte)0));
for (int i=0;i < buf.length;i++) {
assertFalse("Bad value at index " + i, buf[i]);
}
}

public void testByteArrayArgument() {
byte[] buf = new byte[1024];
final byte MAGIC = (byte)0xED;
Expand All @@ -517,6 +537,16 @@ public void testByteArrayArgument() {
}
}

public void testCharArrayArgument() {
char[] buf = new char[1024];
final char MAGIC = '\uFEFF';
assertEquals("Wrong return value", buf.length,
lib.fillInt16Buffer(buf, buf.length, (short)MAGIC));
for (int i=0;i < buf.length;i++) {
assertEquals("Bad value at index " + i, MAGIC, buf[i]);
}
}

public void testShortArrayArgument() {
short[] buf = new short[1024];
final short MAGIC = (short)0xABED;
Expand Down
4 changes: 4 additions & 0 deletions test/com/sun/jna/DirectArgumentsMarshalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public void modifyStructureByReferenceArray(CheckFieldAlignment.ByReference[] p,
public native int fillFloatBuffer(float[] buf, int len, float value);
@Override
public native int fillDoubleBuffer(double[] buf, int len, double value);
@Override
public native int fillInt8Buffer(boolean[] buf, int len, byte value);
@Override
public native int fillInt16Buffer(char[] buf, int len, short value);

// dummy to avoid causing Native.register to fail
@Override
Expand Down