Skip to content

Commit 9543f18

Browse files
committed
Merge branch 'main' into jni-enums
2 parents fea7909 + d5f9c90 commit 9543f18

File tree

23 files changed

+474
-56
lines changed

23 files changed

+474
-56
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func findJavaHome() -> String {
3232
}
3333

3434

35-
if ProcessInfo.processInfo.environment["SPI_PROCESSING"] == "1" {
36-
// just ignore that we're missing a JAVA_HOME when building in Swift Package Index
35+
if ProcessInfo.processInfo.environment["SPI_PROCESSING"] == "1" && ProcessInfo.processInfo.environment["SPI_BUILD"] == nil {
36+
// Just ignore that we're missing a JAVA_HOME when building in Swift Package Index during general processing where no Java is needed. However, do _not_ suppress the error during SPI's compatibility build stage where Java is required.
3737
return ""
3838
}
3939
fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.")

Samples/JExtractJNISampleApp/src/main/java/com/example/swift/HelloJava2SwiftJNI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
// Import javakit/swiftkit support libraries
2020

21+
import org.swift.swiftkit.core.SwiftArena;
2122
import org.swift.swiftkit.core.SwiftLibraries;
22-
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
2323

2424
public class HelloJava2SwiftJNI {
2525

@@ -41,7 +41,7 @@ static void examples() {
4141

4242
MySwiftClass.method();
4343

44-
try (var arena = new ConfinedSwiftMemorySession()) {
44+
try (var arena = SwiftArena.ofConfined()) {
4545
MySwiftClass myClass = MySwiftClass.init(10, 5, arena);
4646
MySwiftClass myClass2 = MySwiftClass.init(arena);
4747

Samples/JExtractJNISampleApp/src/test/java/com/example/swift/MySwiftClassTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.example.swift;
1616

1717
import org.junit.jupiter.api.Test;
18-
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
18+
import org.swift.swiftkit.core.SwiftArena;
1919

2020
import java.util.Optional;
2121
import java.util.OptionalInt;
@@ -26,39 +26,39 @@
2626
public class MySwiftClassTest {
2727
@Test
2828
void init_noParameters() {
29-
try (var arena = new ConfinedSwiftMemorySession()) {
29+
try (var arena = SwiftArena.ofConfined()) {
3030
MySwiftClass c = MySwiftClass.init(arena);
3131
assertNotNull(c);
3232
}
3333
}
3434

3535
@Test
3636
void init_withParameters() {
37-
try (var arena = new ConfinedSwiftMemorySession()) {
37+
try (var arena = SwiftArena.ofConfined()) {
3838
MySwiftClass c = MySwiftClass.init(1337, 42, arena);
3939
assertNotNull(c);
4040
}
4141
}
4242

4343
@Test
4444
void sum() {
45-
try (var arena = new ConfinedSwiftMemorySession()) {
45+
try (var arena = SwiftArena.ofConfined()) {
4646
MySwiftClass c = MySwiftClass.init(20, 10, arena);
4747
assertEquals(30, c.sum());
4848
}
4949
}
5050

5151
@Test
5252
void xMultiplied() {
53-
try (var arena = new ConfinedSwiftMemorySession()) {
53+
try (var arena = SwiftArena.ofConfined()) {
5454
MySwiftClass c = MySwiftClass.init(20, 10, arena);
5555
assertEquals(200, c.xMultiplied(10));
5656
}
5757
}
5858

5959
@Test
6060
void throwingFunction() {
61-
try (var arena = new ConfinedSwiftMemorySession()) {
61+
try (var arena = SwiftArena.ofConfined()) {
6262
MySwiftClass c = MySwiftClass.init(20, 10, arena);
6363
Exception exception = assertThrows(Exception.class, () -> c.throwingFunction());
6464

@@ -68,15 +68,15 @@ void throwingFunction() {
6868

6969
@Test
7070
void constant() {
71-
try (var arena = new ConfinedSwiftMemorySession()) {
71+
try (var arena = SwiftArena.ofConfined()) {
7272
MySwiftClass c = MySwiftClass.init(20, 10, arena);
7373
assertEquals(100, c.getConstant());
7474
}
7575
}
7676

7777
@Test
7878
void mutable() {
79-
try (var arena = new ConfinedSwiftMemorySession()) {
79+
try (var arena = SwiftArena.ofConfined()) {
8080
MySwiftClass c = MySwiftClass.init(20, 10, arena);
8181
assertEquals(0, c.getMutable());
8282
c.setMutable(42);
@@ -86,15 +86,15 @@ void mutable() {
8686

8787
@Test
8888
void product() {
89-
try (var arena = new ConfinedSwiftMemorySession()) {
89+
try (var arena = SwiftArena.ofConfined()) {
9090
MySwiftClass c = MySwiftClass.init(20, 10, arena);
9191
assertEquals(200, c.getProduct());
9292
}
9393
}
9494

9595
@Test
9696
void throwingVariable() {
97-
try (var arena = new ConfinedSwiftMemorySession()) {
97+
try (var arena = SwiftArena.ofConfined()) {
9898
MySwiftClass c = MySwiftClass.init(20, 10, arena);
9999

100100
Exception exception = assertThrows(Exception.class, () -> c.getThrowingVariable());
@@ -105,7 +105,7 @@ void throwingVariable() {
105105

106106
@Test
107107
void mutableDividedByTwo() {
108-
try (var arena = new ConfinedSwiftMemorySession()) {
108+
try (var arena = SwiftArena.ofConfined()) {
109109
MySwiftClass c = MySwiftClass.init(20, 10, arena);
110110
assertEquals(0, c.getMutableDividedByTwo());
111111
c.setMutable(20);
@@ -117,15 +117,15 @@ void mutableDividedByTwo() {
117117

118118
@Test
119119
void isWarm() {
120-
try (var arena = new ConfinedSwiftMemorySession()) {
120+
try (var arena = SwiftArena.ofConfined()) {
121121
MySwiftClass c = MySwiftClass.init(20, 10, arena);
122122
assertFalse(c.isWarm());
123123
}
124124
}
125125

126126
@Test
127127
void sumWithX() {
128-
try (var arena = new ConfinedSwiftMemorySession()) {
128+
try (var arena = SwiftArena.ofConfined()) {
129129
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
130130
MySwiftClass c2 = MySwiftClass.init(50, 10, arena);
131131
assertEquals(70, c1.sumX(c2));
@@ -134,7 +134,7 @@ void sumWithX() {
134134

135135
@Test
136136
void copy() {
137-
try (var arena = new ConfinedSwiftMemorySession()) {
137+
try (var arena = SwiftArena.ofConfined()) {
138138
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
139139
MySwiftClass c2 = c1.copy(arena);
140140

@@ -146,7 +146,7 @@ void copy() {
146146

147147
@Test
148148
void addXWithJavaLong() {
149-
try (var arena = new ConfinedSwiftMemorySession()) {
149+
try (var arena = SwiftArena.ofConfined()) {
150150
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
151151
Long javaLong = 50L;
152152
assertEquals(70, c1.addXWithJavaLong(javaLong));

Samples/JExtractJNISampleApp/src/test/java/com/example/swift/MySwiftStructTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
import org.junit.jupiter.api.Test;
1818
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
19+
import org.swift.swiftkit.core.SwiftArena;
1920

2021
import static org.junit.jupiter.api.Assertions.*;
2122

2223
public class MySwiftStructTest {
2324
@Test
2425
void init() {
25-
try (var arena = new ConfinedSwiftMemorySession()) {
26+
try (var arena = SwiftArena.ofConfined()) {
2627
MySwiftStruct s = MySwiftStruct.init(1337, 42, arena);
2728
assertEquals(1337, s.getCapacity());
2829
assertEquals(42, s.getLen());
@@ -31,7 +32,7 @@ void init() {
3132

3233
@Test
3334
void getAndSetLen() {
34-
try (var arena = new ConfinedSwiftMemorySession()) {
35+
try (var arena = SwiftArena.ofConfined()) {
3536
MySwiftStruct s = MySwiftStruct.init(1337, 42, arena);
3637
s.setLen(100);
3738
assertEquals(100, s.getLen());
@@ -40,7 +41,7 @@ void getAndSetLen() {
4041

4142
@Test
4243
void increaseCap() {
43-
try (var arena = new ConfinedSwiftMemorySession()) {
44+
try (var arena = SwiftArena.ofConfined()) {
4445
MySwiftStruct s = MySwiftStruct.init(1337, 42, arena);
4546
long newCap = s.increaseCap(10);
4647
assertEquals(1347, newCap);

Samples/JExtractJNISampleApp/src/test/java/com/example/swift/OptionalsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.example.swift;
1616

1717
import org.junit.jupiter.api.Test;
18-
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
18+
import org.swift.swiftkit.core.SwiftArena;
1919

2020
import java.util.Optional;
2121
import java.util.OptionalDouble;
@@ -82,7 +82,7 @@ void optionalString() {
8282

8383
@Test
8484
void optionalClass() {
85-
try (var arena = new ConfinedSwiftMemorySession()) {
85+
try (var arena = SwiftArena.ofConfined()) {
8686
MySwiftClass c = MySwiftClass.init(arena);
8787
assertEquals(Optional.empty(), MySwiftLibrary.optionalClass(Optional.empty(), arena));
8888
Optional<MySwiftClass> optionalClass = MySwiftLibrary.optionalClass(Optional.of(c), arena);
@@ -99,7 +99,7 @@ void optionalJavaKitLong() {
9999

100100
@Test
101101
void multipleOptionals() {
102-
try (var arena = new ConfinedSwiftMemorySession()) {
102+
try (var arena = SwiftArena.ofConfined()) {
103103
MySwiftClass c = MySwiftClass.init(arena);
104104
OptionalLong result = MySwiftLibrary.multipleOptionals(
105105
Optional.of((byte) 1),

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,22 +362,38 @@ extension JNISwift2JavaGenerator {
362362
modifiers.append("static")
363363
}
364364

365-
let translatedSignature = translatedDecl.translatedFunctionSignature
366365
let resultType = translatedSignature.resultType.javaType
367-
var parameters = translatedDecl.translatedFunctionSignature.parameters.map({ $0.parameter.renderParameter() })
368-
if translatedSignature.requiresSwiftArena {
369-
parameters.append("SwiftArena swiftArena$")
370-
}
371-
let throwsClause = translatedDecl.isThrowing ? " throws Exception" : ""
366+
var parameters = translatedDecl.translatedFunctionSignature.parameters.map { $0.parameter.renderParameter() }
367+
let throwsClause = decl.isThrowing ? " throws Exception" : ""
372368

373369
var annotationsStr = translatedSignature.annotations.map({ $0.render() }).joined(separator: "\n")
374370
if !annotationsStr.isEmpty { annotationsStr += "\n" }
375371

376-
let modifiersStr = modifiers.joined(separator: " ")
377372
let parametersStr = parameters.joined(separator: ", ")
378373

374+
// Print default global arena variation
375+
if config.effectiveMemoryManagementMode.requiresGlobalArena && translatedSignature.requiresSwiftArena {
376+
printDeclDocumentation(&printer, decl)
377+
printer.printBraceBlock(
378+
"\(annotationsStr)\(modifiers.joined(separator: " ")) \(resultType) \(translatedDecl.name)(\(parametersStr))\(throwsClause)"
379+
) { printer in
380+
let globalArenaName = "SwiftMemoryManagement.GLOBAL_SWIFT_JAVA_ARENA"
381+
let arguments = translatedDecl.translatedFunctionSignature.parameters.map(\.parameter.name) + [globalArenaName]
382+
let call = "\(translatedDecl.name)(\(arguments.joined(separator: ", ")))"
383+
if translatedDecl.translatedFunctionSignature.resultType.javaType.isVoid {
384+
printer.print("\(call);")
385+
} else {
386+
printer.print("return \(call);")
387+
}
388+
}
389+
printer.println()
390+
}
391+
392+
if translatedSignature.requiresSwiftArena {
393+
parameters.append("SwiftArena swiftArena$")
394+
}
379395
printer.printBraceBlock(
380-
"\(annotationsStr)\(modifiersStr) \(resultType) \(translatedDecl.name)(\(parametersStr))\(throwsClause)"
396+
"\(annotationsStr)\(modifiers.joined(separator: " ")) \(resultType) \(translatedDecl.name)(\(parameters.joined(separator: ", ")))\(throwsClause)"
381397
) { printer in
382398
prefix(&printer)
383399
printDowncall(&printer, translatedDecl)

Sources/JExtractSwiftLib/Swift2JavaVisitor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
5+
// Copyright (c) 2024-2025 Apple Inc. and the Swift.org project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -92,7 +92,7 @@ final class Swift2JavaVisitor {
9292
}
9393

9494
func visit(extensionDecl node: ExtensionDeclSyntax, in parent: ImportedNominalType?) {
95-
guard parent != nil else {
95+
guard parent == nil else {
9696
// 'extension' in a nominal type is invalid. Ignore
9797
return
9898
}

Sources/JavaKitConfigurationShared/Configuration.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public struct Configuration: Codable {
5151
minimumInputAccessLevelMode ?? .default
5252
}
5353

54+
public var memoryManagementMode: JExtractMemoryManagementMode?
55+
public var effectiveMemoryManagementMode: JExtractMemoryManagementMode {
56+
memoryManagementMode ?? .default
57+
}
58+
5459
// ==== java 2 swift ---------------------------------------------------------
5560

5661
/// The Java class path that should be passed along to the swift-java tool.

Sources/JavaKitConfigurationShared/GenerationMode.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,25 @@ extension JExtractMinimumAccessLevelMode {
7676
.public
7777
}
7878
}
79+
80+
81+
/// Configures how memory should be managed by the user
82+
public enum JExtractMemoryManagementMode: String, Codable {
83+
/// Force users to provide an explicit `SwiftArena` to all calls that require them.
84+
case explicit
85+
86+
/// Provide both explicit `SwiftArena` support
87+
/// and a default global automatic `SwiftArena` that will deallocate memory when the GC decides to.
88+
case allowGlobalAutomatic
89+
90+
public static var `default`: Self {
91+
.explicit
92+
}
93+
94+
public var requiresGlobalArena: Bool {
95+
switch self {
96+
case .explicit: false
97+
case .allowGlobalAutomatic: true
98+
}
99+
}
100+
}

Sources/SwiftJavaDocumentation/Documentation.docc/SwiftJavaCommandLineTool.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ OPTIONS:
5757
The name of the Swift module into which the resulting Swift types will be generated.
5858
--depends-on <depends-on>
5959
A swift-java configuration file for a given Swift module name on which this module depends,
60-
e.g., JavaKitJar=Sources/JavaKitJar/Java2Swift.config. There should be one of these options
60+
e.g., JavaKitJar=Sources/JavaKitJar/swift-java.config. There should be one of these options
6161
for each Swift module that this module depends on (transitively) that contains wrapped Java sources.
6262
--swift-native-implementation <swift-native-implementation>
6363
The names of Java classes whose declared native methods will be implemented in Swift.

0 commit comments

Comments
 (0)