handle null java arrays

This commit is contained in:
Adam D. Ruppe 2021-03-17 21:50:15 -04:00
parent cce50f56a0
commit 3252df7531
1 changed files with 39 additions and 21 deletions

60
jni.d
View File

@ -1240,6 +1240,8 @@ private void exceptionCheck(JNIEnv* env) {
} }
E[] translateJavaArray(E)(JNIEnv* env, jarray jarr) { E[] translateJavaArray(E)(JNIEnv* env, jarray jarr) {
if(jarr is null)
return null;
auto len = (*env).GetArrayLength(env, jarr); auto len = (*env).GetArrayLength(env, jarr);
static if(is(E == int)) { static if(is(E == int)) {
auto eles = (*env).GetIntArrayElements(env, jarr, null); auto eles = (*env).GetIntArrayElements(env, jarr, null);
@ -2080,35 +2082,49 @@ private struct JavaParamsToD(Spec...) {
} else static if(is(T : IJavaObject)) { } else static if(is(T : IJavaObject)) {
arg = fromExistingJavaObject!T(jarg); arg = fromExistingJavaObject!T(jarg);
} else static if(is(T == bool[])) { } else static if(is(T == bool[])) {
auto len = (*env).GetArrayLength(env, jarg); if(jarg !is null) {
auto ptr = (*env).GetBooleanArrayElements(env, jarg, null); auto len = (*env).GetArrayLength(env, jarg);
arg = ptr is null ? null : ptr[0 .. len]; auto ptr = (*env).GetBooleanArrayElements(env, jarg, null);
arg = ptr is null ? null : ptr[0 .. len];
}
} else static if(is(T == byte[])) { } else static if(is(T == byte[])) {
auto len = (*env).GetArrayLength(env, jarg); if(jarg !is null) {
auto ptr = (*env).GetByteArrayElements(env, jarg, null); auto len = (*env).GetArrayLength(env, jarg);
arg = ptr is null ? null : ptr[0 .. len]; auto ptr = (*env).GetByteArrayElements(env, jarg, null);
arg = ptr is null ? null : ptr[0 .. len];
}
} else static if(is(T == wchar[])) { } else static if(is(T == wchar[])) {
// handled above // handled above
} else static if(is(T == short[])) { } else static if(is(T == short[])) {
auto len = (*env).GetArrayLength(env, jarg); if(jarg !is null) {
auto ptr = (*env).GetShortArrayElements(env, jarg, null); auto len = (*env).GetArrayLength(env, jarg);
arg = ptr is null ? null : ptr[0 .. len]; auto ptr = (*env).GetShortArrayElements(env, jarg, null);
arg = ptr is null ? null : ptr[0 .. len];
}
} else static if(is(T == int[])) { } else static if(is(T == int[])) {
auto len = (*env).GetArrayLength(env, jarg); if(jarg !is null) {
auto ptr = (*env).GetIntArrayElements(env, jarg, null); auto len = (*env).GetArrayLength(env, jarg);
arg = ptr is null ? null : ptr[0 .. len]; auto ptr = (*env).GetIntArrayElements(env, jarg, null);
arg = ptr is null ? null : ptr[0 .. len];
}
} else static if(is(T == long[])) { } else static if(is(T == long[])) {
auto len = (*env).GetArrayLength(env, jarg); if(jarg !is null) {
auto ptr = (*env).GetLongArrayElements(env, jarg, null); auto len = (*env).GetArrayLength(env, jarg);
arg = ptr is null ? null : ptr[0 .. len]; auto ptr = (*env).GetLongArrayElements(env, jarg, null);
arg = ptr is null ? null : ptr[0 .. len];
}
} else static if(is(T == float[])) { } else static if(is(T == float[])) {
auto len = (*env).GetArrayLength(env, jarg); if(jarg !is null) {
auto ptr = (*env).GetFloatArrayElements(env, jarg, null); auto len = (*env).GetArrayLength(env, jarg);
arg = ptr is null ? null : ptr[0 .. len]; auto ptr = (*env).GetFloatArrayElements(env, jarg, null);
arg = ptr is null ? null : ptr[0 .. len];
}
} else static if(is(T == double[])) { } else static if(is(T == double[])) {
auto len = (*env).GetArrayLength(env, jarg); if(jarg !is null) {
auto ptr = (*env).GetDoubleArrayElements(env, jarg, null); auto len = (*env).GetArrayLength(env, jarg);
arg = ptr is null ? null : ptr[0 .. len]; auto ptr = (*env).GetDoubleArrayElements(env, jarg, null);
arg = ptr is null ? null : ptr[0 .. len];
}
} }
else static assert(0, "Unimplemented/unsupported type " ~ T.stringof); else static assert(0, "Unimplemented/unsupported type " ~ T.stringof);
@ -2205,6 +2221,8 @@ T as(T, R)(R obj) {
static T fromExistingJavaObject(T)(jobject o) if(is(T : IJavaObject) && !is(T == interface)) { static T fromExistingJavaObject(T)(jobject o) if(is(T : IJavaObject) && !is(T == interface)) {
if(o is null)
return null;
import core.memory; import core.memory;
auto ptr = GC.malloc(__traits(classInstanceSize, T)); auto ptr = GC.malloc(__traits(classInstanceSize, T));
ptr[0 .. __traits(classInstanceSize, T)] = typeid(T).initializer[]; ptr[0 .. __traits(classInstanceSize, T)] = typeid(T).initializer[];