Skip to content

[JavaKit] Add JNIEnvPointer and JNINativeInterface_ for Android #311

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Sources/JavaKit/JavaEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

import JavaRuntime

#if canImport(Android)
typealias JNINativeInterface_ = JNINativeInterface
#endif

extension UnsafeMutablePointer<JNIEnv?> {
var interface: JNINativeInterface_ { self.pointee!.pointee }
}
25 changes: 20 additions & 5 deletions Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import Foundation
#endif

public typealias JavaVMPointer = UnsafeMutablePointer<JavaVM?>
#if canImport(Android)
typealias JNIEnvPointer = UnsafeMutablePointer<JNIEnv?>
#else
typealias JNIEnvPointer = UnsafeMutableRawPointer
#endif

public final class JavaVirtualMachine: @unchecked Sendable {
/// The JNI version that we depend on.
Expand Down Expand Up @@ -61,7 +66,7 @@ public final class JavaVirtualMachine: @unchecked Sendable {
) throws {
self.classpath = classpath
var jvm: JavaVMPointer? = nil
var environment: UnsafeMutableRawPointer? = nil
var environment: JNIEnvPointer? = nil
var vmArgs = JavaVMInitArgs()
vmArgs.version = JavaVirtualMachine.jniVersion
vmArgs.ignoreUnrecognized = jboolean(ignoreUnrecognized ? JNI_TRUE : JNI_FALSE)
Expand Down Expand Up @@ -161,12 +166,18 @@ extension JavaVirtualMachine {
return environment.assumingMemoryBound(to: JNIEnv?.self)
}

#if canImport(Android)
var jniEnv = environment?.assumingMemoryBound(to: JNIEnv?.self)
#else
var jniEnv = environment
#endif

// Attach the current thread to the JVM.
let attachResult: jint
if asDaemon {
attachResult = jvm.pointee!.pointee.AttachCurrentThreadAsDaemon(jvm, &environment, nil)
attachResult = jvm.pointee!.pointee.AttachCurrentThreadAsDaemon(jvm, &jniEnv, nil)
} else {
attachResult = jvm.pointee!.pointee.AttachCurrentThread(jvm, &environment, nil)
attachResult = jvm.pointee!.pointee.AttachCurrentThread(jvm, &jniEnv, nil)
}

// If we failed to attach, report that.
Expand All @@ -175,9 +186,13 @@ extension JavaVirtualMachine {
throw attachError
}

JavaVirtualMachine.destroyTLS.set(environment!)
JavaVirtualMachine.destroyTLS.set(jniEnv!)

return environment!.assumingMemoryBound(to: JNIEnv?.self)
#if canImport(Android)
return jniEnv!
#else
return jniEnv!.assumingMemoryBound(to: JNIEnv?.self)
#endif
}

/// Detach the current thread from the Java Virtual Machine. All Java
Expand Down
Loading