Skip to content

Commit 9ca6e30

Browse files
authored
Merge branch 'main' into wip-test-with-nightly
2 parents 896ff4c + c3ecd54 commit 9ca6e30

36 files changed

+726
-279
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.DS_Store
55
.build
66
.idea
7+
.vscode
78
Packages
89
xcuserdata/
910
DerivedData/
@@ -16,6 +17,7 @@ bin/
1617
BuildLogic/out/
1718
.index-build
1819
.build-vscode
20+
**/.vscode
1921

2022
# Ignore gradle build artifacts
2123
.gradle
@@ -45,3 +47,4 @@ Package.resolved
4547
*/**/*.swiftdeps
4648
*/**/*.swiftdeps~
4749
*/**/.docc-build/
50+

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/FFM/ConversionStep.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ enum ConversionStep: Equatable {
123123
// of splatting out text.
124124
let renderedArgumentList = renderedArguments.joined(separator: ", ")
125125
return "\(raw: type.description)(\(raw: renderedArgumentList))"
126-
126+
127127
case .tuplify(let elements):
128128
let renderedElements: [String] = elements.enumerated().map { (index, element) in
129129
element.asExprSyntax(placeholder: "\(placeholder)_\(index)", bodyItems: &bodyItems)!.description

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ extension FFMSwift2JavaGenerator {
407407
"arena$"
408408
}
409409

410+
// FIXME: use trailing$ convention
410411
let varName = outParameter.name.isEmpty ? "_result" : "_result_" + outParameter.name
411412

412413
printer.print(
@@ -463,7 +464,7 @@ extension FFMSwift2JavaGenerator.JavaConversionStep {
463464
switch self {
464465
case .placeholder, .explodedName, .constant, .readMemorySegment:
465466
return false
466-
case .constructSwiftValue:
467+
case .constructSwiftValue, .wrapMemoryAddressUnsafe:
467468
return true
468469

469470
case .call(let inner, _, _), .cast(let inner, _), .construct(let inner, _),
@@ -482,7 +483,11 @@ extension FFMSwift2JavaGenerator.JavaConversionStep {
482483
return false
483484
case .readMemorySegment:
484485
return true
485-
case .cast(let inner, _), .construct(let inner, _), .constructSwiftValue(let inner, _), .swiftValueSelfSegment(let inner):
486+
case .cast(let inner, _),
487+
.construct(let inner, _),
488+
.constructSwiftValue(let inner, _),
489+
.swiftValueSelfSegment(let inner),
490+
.wrapMemoryAddressUnsafe(let inner, _):
486491
return inner.requiresSwiftArena
487492
case .call(let inner, _, let withArena):
488493
return withArena || inner.requiresTemporaryArena
@@ -522,6 +527,10 @@ extension FFMSwift2JavaGenerator.JavaConversionStep {
522527
let inner = inner.render(&printer, placeholder)
523528
return "new \(javaType.className!)(\(inner), swiftArena$)"
524529

530+
case .wrapMemoryAddressUnsafe(let inner, let javaType):
531+
let inner = inner.render(&printer, placeholder)
532+
return "\(javaType.className!).wrapMemoryAddressUnsafe(\(inner), swiftArena$)"
533+
525534
case .construct(let inner, let javaType):
526535
let inner = inner.render(&printer, placeholder)
527536
return "new \(javaType)(\(inner))"

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ extension FFMSwift2JavaGenerator {
684684
outParameters: [
685685
JavaParameter(name: "", type: javaType)
686686
],
687-
conversion: .constructSwiftValue(.placeholder, javaType)
687+
conversion: .wrapMemoryAddressUnsafe(.placeholder, javaType)
688688
)
689689

690690
case .tuple:
@@ -732,6 +732,9 @@ extension FFMSwift2JavaGenerator {
732732
// Call 'new \(Type)(\(placeholder), swiftArena$)'.
733733
indirect case constructSwiftValue(JavaConversionStep, JavaType)
734734

735+
/// Call the `MyType.wrapMemoryAddressUnsafe` in order to wrap a memory address using the Java binding type
736+
indirect case wrapMemoryAddressUnsafe(JavaConversionStep, JavaType)
737+
735738
// Construct the type using the placeholder as arguments.
736739
indirect case construct(JavaConversionStep, JavaType)
737740

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,22 @@ extension FFMSwift2JavaGenerator {
216216

217217
printer.print(
218218
"""
219-
public \(decl.swiftNominal.name)(MemorySegment segment, AllocatingSwiftArena arena) {
219+
private \(decl.swiftNominal.name)(MemorySegment segment, AllocatingSwiftArena arena) {
220220
super(segment, arena);
221221
}
222+
223+
/**
224+
* Assume that the passed {@code MemorySegment} represents a memory address of a {@link \(decl.swiftNominal.name)}.
225+
* <p/>
226+
* Warnings:
227+
* <ul>
228+
* <li>No checks are performed about the compatibility of the pointed at memory and the actual \(decl.swiftNominal.name) types.</li>
229+
* <li>This operation does not copy, or retain, the pointed at pointer, so its lifetime must be ensured manually to be valid when wrapping.</li>
230+
* </ul>
231+
*/
232+
public static \(decl.swiftNominal.name) wrapMemoryAddressUnsafe(MemorySegment selfPointer, AllocatingSwiftArena swiftArena) {
233+
return new \(decl.swiftNominal.name)(selfPointer, swiftArena);
234+
}
222235
"""
223236
)
224237

0 commit comments

Comments
 (0)