Skip to content

Commit 4931115

Browse files
committed
work on dynamic libs
1 parent 4fb6ca2 commit 4931115

File tree

11 files changed

+52
-39
lines changed

11 files changed

+52
-39
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ let package = Package(
100100
// ==== SwiftJava (i.e. calling Java directly Swift utilities)
101101
.library(
102102
name: "SwiftJava",
103+
type: .dynamic,
103104
targets: ["SwiftJava"]
104105
),
105106

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ extension FFMSwift2JavaGenerator {
195195
private static final boolean INITIALIZED_LIBS = initializeLibs();
196196
static boolean initializeLibs() {
197197
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE);
198+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
198199
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS);
199200
System.loadLibrary(LIB_NAME);
200201
return true;
@@ -345,6 +346,7 @@ extension FFMSwift2JavaGenerator {
345346
private static SymbolLookup getSymbolLookup() {
346347
if (SwiftLibraries.AUTO_LOAD_LIBS) {
347348
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE);
349+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
348350
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS);
349351
System.loadLibrary(LIB_NAME);
350352
}

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ extension JNISwift2JavaGenerator {
9999
static final String LIB_NAME = "\(swiftModuleName)";
100100
101101
static {
102+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
102103
System.loadLibrary(LIB_NAME);
103104
}
104105
"""
@@ -169,6 +170,7 @@ extension JNISwift2JavaGenerator {
169170
@SuppressWarnings("unused")
170171
private static final boolean INITIALIZED_LIBS = initializeLibs();
171172
static boolean initializeLibs() {
173+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
172174
System.loadLibrary(LIB_NAME);
173175
return true;
174176
}

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ extension JNISwift2JavaGenerator {
162162
// If the underlying translated method requires
163163
// a SwiftArena, we pass in the global arena
164164
if translatedDecl.translatedFunctionSignature.requiresSwiftArena {
165-
upcallArguments.append("JNI.shared.defaultAutoArena")
165+
upcallArguments.append("JavaSwiftArena.defaultAutoArena")
166166
}
167167

168168
let tryClause = function.originalFunctionSignature.isThrowing ? "try " : ""
@@ -201,8 +201,6 @@ extension JNISwift2JavaGenerator {
201201
private func printGlobalSwiftThunkSources(_ printer: inout CodePrinter) throws {
202202
printHeader(&printer)
203203

204-
printJNIOnLoad(&printer)
205-
206204
for decl in analysis.importedGlobalFuncs {
207205
printSwiftFunctionThunk(&printer, decl)
208206
printer.println()
@@ -214,18 +212,6 @@ extension JNISwift2JavaGenerator {
214212
}
215213
}
216214

217-
private func printJNIOnLoad(_ printer: inout CodePrinter) {
218-
printer.print(
219-
"""
220-
@_cdecl("JNI_OnLoad")
221-
func JNI_OnLoad(javaVM: JavaVMPointer, reserved: UnsafeMutableRawPointer) -> jint {
222-
SwiftJavaRuntimeSupport._JNI_OnLoad(javaVM, reserved)
223-
return JNI_VERSION_1_6
224-
}
225-
"""
226-
)
227-
}
228-
229215
private func printNominalTypeThunks(_ printer: inout CodePrinter, _ type: ImportedNominalType) throws {
230216
printHeader(&printer)
231217

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import SwiftJava
1615
import CSwiftJavaJNI
1716

1817
/// A type that represents the shared JNI environment
@@ -21,36 +20,25 @@ import CSwiftJavaJNI
2120
/// This is initialized when the `JNI_OnLoad` is triggered,
2221
/// which happens when you call `System.loadLibrary(...)`
2322
/// from Java.
24-
public final class JNI {
23+
package final class JNI {
2524
/// The shared JNI object, initialized by `JNI_OnLoad`
26-
public fileprivate(set) static var shared: JNI!
25+
package fileprivate(set) static var shared: JNI!
2726

2827
/// The default application class loader
29-
public let applicationClassLoader: JavaClassLoader
30-
31-
/// The default auto arena of SwiftKitCore
32-
public let defaultAutoArena: JavaSwiftArena
28+
package let applicationClassLoader: JavaClassLoader
3329

3430
init(fromVM javaVM: JavaVirtualMachine) {
31+
// Update the global JavaVM
32+
JavaVirtualMachine.sharedJVM.withLock {
33+
$0 = javaVM
34+
}
3535
let environment = try! javaVM.environment()
36-
3736
self.applicationClassLoader = try! JavaClass<JavaThread>(environment: environment).currentThread().getContextClassLoader()
38-
39-
// Find global arena
40-
let swiftMemoryClass = environment.interface.FindClass(environment, "org/swift/swiftkit/core/SwiftMemoryManagement")!
41-
let arenaFieldID = environment.interface.GetStaticFieldID(
42-
environment,
43-
swiftMemoryClass,
44-
"DEFAULT_SWIFT_JAVA_AUTO_ARENA",
45-
JavaSwiftArena.mangledName
46-
)
47-
let localObject = environment.interface.GetStaticObjectField(environment, swiftMemoryClass, arenaFieldID)!
48-
self.defaultAutoArena = JavaSwiftArena(javaThis: localObject, environment: environment)
49-
environment.interface.DeleteLocalRef(environment, localObject)
5037
}
5138
}
5239

53-
// Called by generated code, and not automatically by Java.
54-
public func _JNI_OnLoad(_ javaVM: JavaVMPointer, _ reserved: UnsafeMutableRawPointer) {
40+
@_cdecl("JNI_OnLoad")
41+
func SwiftJava_JNI_OnLoad(javaVM: JavaVMPointer, reserved: UnsafeMutableRawPointer) -> jint {
5542
JNI.shared = JNI(fromVM: JavaVirtualMachine(adoptingJVM: javaVM))
43+
return JNI_VERSION_1_6
5644
}

Sources/SwiftJava/JVM/JavaVirtualMachine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ extension JavaVirtualMachine {
220220
/// TODO: If the use of the lock itself ends up being slow, we could
221221
/// use an atomic here instead because our access pattern is fairly
222222
/// simple.
223-
private static let sharedJVM: LockedState<JavaVirtualMachine?> = .init(initialState: nil)
223+
static let sharedJVM: LockedState<JavaVirtualMachine?> = .init(initialState: nil)
224224

225225
/// Access the shared Java Virtual Machine instance.
226226
///

Sources/SwiftJavaRuntimeSupport/generated/JavaSwiftArena.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ import SwiftJava
1616

1717
@JavaInterface("org.swift.swiftkit.core.SwiftArena")
1818
public struct JavaSwiftArena {}
19+
20+
extension JavaSwiftArena {
21+
/// A cache for the default auto arena found in SwiftKitCore
22+
public static internal(set) var defaultAutoArena: JavaSwiftArena = {
23+
let swiftMemoryClass = try! JavaClass<SwiftJavaRuntimeSupport.JavaSwiftMemoryManagement>()
24+
return swiftMemoryClass.defaultSwiftJavaAutoArena
25+
}()
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftJava
16+
17+
@JavaClass("org.swift.swiftkit.core.SwiftMemoryManagement")
18+
public class JavaSwiftMemoryManagement: JavaObject {}
19+
20+
extension JavaClass<JavaSwiftMemoryManagement> {
21+
@JavaStaticField("DEFAULT_SWIFT_JAVA_AUTO_ARENA", isFinal: true)
22+
var defaultSwiftJavaAutoArena: JavaSwiftArena!
23+
}

SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class SwiftLibraries {
2828
public static final String LIB_NAME_SWIFT_CORE = "swiftCore";
2929
public static final String LIB_NAME_SWIFT_CONCURRENCY = "swift_Concurrency";
3030
public static final String LIB_NAME_SWIFT_RUNTIME_FUNCTIONS = "SwiftRuntimeFunctions";
31+
public static final String LIB_NAME_SWIFT_JAVA = "SwiftJava";
3132

3233
/**
3334
* Allows for configuration if jextracted types should automatically attempt to load swiftCore and the library type is from.
@@ -44,6 +45,7 @@ public final class SwiftLibraries {
4445

4546
public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) {
4647
System.loadLibrary(LIB_NAME_SWIFT_CORE);
48+
System.loadLibrary(LIB_NAME_SWIFT_JAVA);
4749
if (loadSwiftRuntimeFunctions) {
4850
System.loadLibrary(LIB_NAME_SWIFT_RUNTIME_FUNCTIONS);
4951
}

Tests/JExtractSwiftTests/JNI/JNIClassTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct JNIClassTests {
6666
@SuppressWarnings("unused")
6767
private static final boolean INITIALIZED_LIBS = initializeLibs();
6868
static boolean initializeLibs() {
69+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
6970
System.loadLibrary(LIB_NAME);
7071
return true;
7172
}

0 commit comments

Comments
 (0)