Skip to content

Commit a6c2083

Browse files
Merge pull request #816 from ncruces/boolean-fixes
Direct Mapping for boolean arrays
2 parents a5e77ba + 33d2774 commit a6c2083

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Features
1111
* [#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).
1212
* [#784](https://github.com/java-native-access/jna/issues/784): Added Solaris Kstat library - [@dbwiddis](https://github.com/dbwiddis).
1313
* [#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).
14+
* [#816](https://github.com/java-native-access/jna/pull/816): Support `boolean[]` in direct mapping - [@ncruces](https://github.com/ncruces).
1415

1516
Bug Fixes
1617
---------

native/dispatch.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ dispatch(JNIEnv *env, void* func, jint flags, jobjectArray args,
485485
}
486486
else if ((*env)->IsInstanceOf(env, arg, classBoolean)) {
487487
c_args[i].i = (*env)->GetBooleanField(env, arg, FID_Boolean_value);
488-
arg_types[i] = &ffi_type_sint32;
488+
arg_types[i] = &ffi_type_uint32;
489489
arg_values[i] = &c_args[i].i;
490490
}
491491
else if ((*env)->IsInstanceOf(env, arg, classByte)) {
@@ -1849,6 +1849,7 @@ dispatch_direct(ffi_cif* cif, void* volatile resp, void** argp, void *cdata) {
18491849
objects[i] = *(void **)args[i]; \
18501850
release[i] = (void *)(*env)->Release##Type##ArrayElements; \
18511851
elems[i] = *(void **)args[i] = (*env)->Get##Type##ArrayElements(env, objects[i], NULL); } while(0)
1852+
case CVT_ARRAY_BOOLEAN: ARRAY(Boolean); break;
18521853
case CVT_ARRAY_BYTE: ARRAY(Byte); break;
18531854
case CVT_ARRAY_SHORT: ARRAY(Short); break;
18541855
case CVT_ARRAY_CHAR: ARRAY(Char); break;
@@ -1968,6 +1969,7 @@ dispatch_direct(ffi_cif* cif, void* volatile resp, void** argp, void *cdata) {
19681969
free(*(void **)args[i]);
19691970
break;
19701971
case CVT_BUFFER:
1972+
case CVT_ARRAY_BOOLEAN:
19711973
case CVT_ARRAY_BYTE:
19721974
case CVT_ARRAY_SHORT:
19731975
case CVT_ARRAY_CHAR:

test/com/sun/jna/ArgumentsMarshalTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ public TestPointerType() { }
109109
int fillFloatBuffer(float[] buf, int len, float value);
110110
int fillDoubleBuffer(double[] buf, int len, double value);
111111

112+
// boolean[] maps to jboolean* (always 8-bit), boolean mapping is 32-bit by default; use byte
113+
int fillInt8Buffer(boolean[] buf, int len, byte value);
114+
115+
// char[] maps to jchar* (always 16-bit), char maps to wchar_t (can be 32-bit); use short
116+
int fillInt16Buffer(char[] buf, int len, short value);
117+
112118
// Nonexistent functions
113119
boolean returnBooleanArgument(Object arg);
114120

@@ -507,6 +513,20 @@ public void testReadStructureByReferenceArrayArgumentMemory() {
507513
}
508514
}
509515

516+
public void testBooleanArrayArgument() {
517+
boolean[] buf = new boolean[1024];
518+
assertEquals("Wrong return value", buf.length,
519+
lib.fillInt8Buffer(buf, buf.length, (byte)1));
520+
for (int i=0;i < buf.length;i++) {
521+
assertTrue("Bad value at index " + i, buf[i]);
522+
}
523+
assertEquals("Wrong return value", buf.length,
524+
lib.fillInt8Buffer(buf, buf.length, (byte)0));
525+
for (int i=0;i < buf.length;i++) {
526+
assertFalse("Bad value at index " + i, buf[i]);
527+
}
528+
}
529+
510530
public void testByteArrayArgument() {
511531
byte[] buf = new byte[1024];
512532
final byte MAGIC = (byte)0xED;
@@ -517,6 +537,16 @@ public void testByteArrayArgument() {
517537
}
518538
}
519539

540+
public void testCharArrayArgument() {
541+
char[] buf = new char[1024];
542+
final char MAGIC = '\uFEFF';
543+
assertEquals("Wrong return value", buf.length,
544+
lib.fillInt16Buffer(buf, buf.length, (short)MAGIC));
545+
for (int i=0;i < buf.length;i++) {
546+
assertEquals("Bad value at index " + i, MAGIC, buf[i]);
547+
}
548+
}
549+
520550
public void testShortArrayArgument() {
521551
short[] buf = new short[1024];
522552
final short MAGIC = (short)0xABED;

test/com/sun/jna/DirectArgumentsMarshalTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public void modifyStructureByReferenceArray(CheckFieldAlignment.ByReference[] p,
107107
public native int fillFloatBuffer(float[] buf, int len, float value);
108108
@Override
109109
public native int fillDoubleBuffer(double[] buf, int len, double value);
110+
@Override
111+
public native int fillInt8Buffer(boolean[] buf, int len, byte value);
112+
@Override
113+
public native int fillInt16Buffer(char[] buf, int len, short value);
110114

111115
// dummy to avoid causing Native.register to fail
112116
@Override

0 commit comments

Comments
 (0)