Repository navigation
Expand file tree
/
Copy pathJNI.swift
More file actions
47 lines (42 loc) · 1.6 KB
/
Copy pathJNI.swift
File metadata and controls
47 lines (42 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import CSwiftJavaJNI
/// A type that represents the shared JNI environment
/// used to share any global JNI variables.
///
/// This is initialized when the `JNI_OnLoad` is triggered,
/// which happens when you call `System.loadLibrary(...)`
/// from Java.
package final class JNI {
/// The shared JNI object, initialized by `JNI_OnLoad`
///
/// This may be `nil` in the case where `SwiftJava` is not loaded as a dynamic lib
/// by the Java sources.
package fileprivate(set) static var shared: JNI?
/// The default application class loader
package let applicationClassLoader: JavaClassLoader
init(fromVM javaVM: JavaVirtualMachine) {
// Update the global JavaVM
JavaVirtualMachine.sharedJVM.withLock {
$0 = javaVM
}
let environment = try! javaVM.environment()
self.applicationClassLoader = try! JavaClass<JavaThread>(environment: environment).currentThread().getContextClassLoader()
}
}
@_cdecl("JNI_OnLoad")
func SwiftJava_JNI_OnLoad(javaVM: JavaVMPointer, reserved: UnsafeMutableRawPointer) -> jint {
JNI.shared = JNI(fromVM: JavaVirtualMachine(adoptingJVM: javaVM))
return JNI_VERSION_1_6
}