Skip to content

Commit 441c31a

Browse files
committed
wip on actually converting the optional values swift -> java optional
1 parent 2df09cf commit 441c31a

File tree

6 files changed

+113
-5
lines changed

6 files changed

+113
-5
lines changed

Samples/SwiftKitSampleApp/src/test/java/com/example/swift/generated/MySwiftClassTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.jupiter.api.condition.DisabledOnOs;
2121
import org.junit.jupiter.api.condition.OS;
2222

23+
import java.util.OptionalLong;
24+
2325
import static org.junit.jupiter.api.Assertions.assertEquals;
2426
import static org.junit.jupiter.api.Assertions.assertNotNull;
2527

@@ -56,4 +58,11 @@ void test_MySwiftClass_property_len() {
5658
assertEquals(12, got);
5759
}
5860

61+
@Test
62+
void test_MySwiftClass_optionalInt() {
63+
MySwiftClass o = new MySwiftClass(12, 42);
64+
OptionalLong got = o.getOptionalInt(); // FIXME: we need to get the Swift optional as memory region, map it to the Java Optional
65+
assertEquals(12, got);
66+
}
67+
5968
}

Sources/ExampleSwiftLibrary/MySwiftLibrary.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public class MySwiftClass {
7878
p("make int -> 12")
7979
return 12
8080
}
81+
82+
public func getOptionalInt() -> Int? {
83+
return 12
84+
}
8185
}
8286

8387
@_silgen_name("swift_getTypeByMangledNameInEnvironment")

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ extension Swift2JavaTranslator {
456456
printer.print(
457457
"""
458458
static {
459-
System.loadLibrary("swiftCore");
459+
System.loadLibrary(SwiftKit.STDLIB_DYLIB_NAME);
460+
System.loadLibrary(SwiftKit.SWIFTKIT_DYLIB_NAME);
460461
System.loadLibrary(LIB_NAME);
461462
}
462463
"""

SwiftKit/src/main/java/org/swift/swiftkit/SwiftKit.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@
2727

2828
public class SwiftKit {
2929

30-
private static final String STDLIB_DYLIB_NAME = "swiftCore";
31-
private static final String STDLIB_MACOS_DYLIB_PATH = "/usr/lib/swift/libswiftCore.dylib";
30+
public static final String STDLIB_DYLIB_NAME = "swiftCore";
31+
public static final String SWIFTKIT_DYLIB_NAME = "SwiftKitSwift";
3232

33-
private static final Arena LIBRARY_ARENA = Arena.ofAuto();
33+
static final String STDLIB_MACOS_DYLIB_PATH = "/usr/lib/swift/libswiftCore.dylib";
34+
35+
static final Arena LIBRARY_ARENA = Arena.ofAuto();
3436
static final boolean TRACE_DOWNCALLS = Boolean.getBoolean("jextract.trace.downcalls");
3537

3638
static {
3739
System.loadLibrary(STDLIB_DYLIB_NAME);
38-
System.loadLibrary("SwiftKitSwift");
40+
System.loadLibrary(SWIFTKIT_DYLIB_NAME);
3941
}
4042

4143
static final SymbolLookup SYMBOL_LOOKUP = getSymbolLookup();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
package org.swift.swiftkit.stdlib;
16+
17+
import org.swift.swiftkit.*;
18+
import org.swift.swiftkit.util.TypeToken;
19+
20+
import java.lang.foreign.*;
21+
22+
public class SwiftOptional<Wrapped extends SwiftInstance> implements SwiftValue {
23+
24+
// Pointer to the referred to class instance's "self".
25+
private final MemorySegment selfMemorySegment;
26+
27+
static final String LIB_NAME = SwiftKit.STDLIB_DYLIB_NAME;
28+
29+
public SwiftOptional(MemorySegment selfMemorySegment) {
30+
this.selfMemorySegment = selfMemorySegment;
31+
}
32+
33+
public final MemorySegment $memorySegment() {
34+
return this.selfMemorySegment;
35+
}
36+
37+
public static final AddressLayout SWIFT_POINTER = ValueLayout.ADDRESS;
38+
39+
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
40+
SWIFT_POINTER
41+
).withName("Swift.Optional<T>");
42+
43+
@Override
44+
public GroupLayout $layout() {
45+
return $LAYOUT;
46+
}
47+
48+
@Override
49+
public SwiftAnyType $swiftType() {
50+
var ty = new TypeToken<Wrapped>() {}.getType();
51+
if (ty == Integer.class || ty == Long.class) {
52+
return SwiftKit.getTypeByMangledNameInEnvironment("SiSg").get();
53+
} else {
54+
throw new RuntimeException("Other Optional type mappings not implemented yet");
55+
}
56+
}
57+
}

Tests/JExtractSwiftTests/OptionalImportTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,39 @@ final class OptionalImportTests {
113113
]
114114
)
115115
}
116+
117+
@Test("Import: public func globalGetFloatOptional() -> Float?")
118+
func globalGetFloatOptional() throws {
119+
let st = Swift2JavaTranslator(
120+
javaPackage: "com.example.swift",
121+
swiftModuleName: "__FakeModule"
122+
)
123+
st.log.logLevel = .warning
124+
125+
assertOutput(
126+
st,
127+
input: class_interfaceFile,
128+
expectedChunks: [
129+
"""
130+
/**
131+
* Downcall to Swift:
132+
* {@snippet lang=swift :
133+
* public func globalGetFloatOptional() -> Float?
134+
* }
135+
*/
136+
public static java.util.OptionalDouble globalGetFloatOptional() {
137+
var mh$ = globalGetFloatOptional.HANDLE;
138+
try {
139+
if (TRACE_DOWNCALLS) {
140+
traceDowncall();
141+
}
142+
return (java.util.OptionalDouble) mh$.invokeExact();
143+
} catch (Throwable ex$) {
144+
throw new AssertionError("should not reach here", ex$);
145+
}
146+
}
147+
"""
148+
]
149+
)
150+
}
116151
}

0 commit comments

Comments
 (0)