Skip to content

Commit c3bf377

Browse files
committed
snapshot
1 parent 2700a2e commit c3bf377

File tree

22 files changed

+544
-267
lines changed

22 files changed

+544
-267
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ JEXTRACT_BUILD_DIR="$(BUILD_DIR)/jextract"
9797
define make_swiftinterface
9898
$(eval $@_MODULE = $(1))
9999
$(eval $@_FILENAME = $(2))
100-
eval ${SWIFTC} \
100+
# eval ${SWIFTC} \
101+
eval /Library/Developer/Toolchains/swift-PR-76905-1539.xctoolchain/usr/bin/swiftc \
101102
-emit-module-interface-path ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftinterface \
102103
-emit-module-path ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftmodule \
103104
-enable-library-evolution \

Samples/SwiftKitSampleApp/src/main/java/com/example/swift/HelloJava2Swift.java

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

1717
// Import swift-extract generated sources
18-
import com.example.swift.generated.ExampleSwiftLibrary;
1918
import com.example.swift.generated.MySwiftClass;
2019

2120
// Import javakit/swiftkit support libraries
2221
import org.swift.swiftkit.SwiftKit;
23-
22+
import org.swift.swiftkit.SwiftValueWitnessTable;
2423

2524
import java.lang.foreign.*;
26-
import java.util.List;
27-
2825

2926
public class HelloJava2Swift {
3027

@@ -36,16 +33,35 @@ public static void main(String[] args) {
3633
}
3734

3835
static void examples() {
39-
ExampleSwiftLibrary.helloWorld();
40-
41-
ExampleSwiftLibrary.globalTakeInt(1337);
36+
// ExampleSwiftLibrary.helloWorld();
37+
//
38+
// ExampleSwiftLibrary.globalTakeInt(1337);
39+
//
40+
// MySwiftClass obj = new MySwiftClass(2222, 7777);
41+
//
42+
// SwiftKit.retain(obj.$memorySegment());
43+
// System.out.println("[java] obj ref count = " + SwiftKit.retainCount(obj.$memorySegment()));
44+
//
45+
// obj.voidMethod();
46+
// obj.takeIntMethod(42);
4247

43-
MySwiftClass obj = new MySwiftClass(2222, 7777);
48+
// try (var arena = SwiftArena.ofConfined()) {
49+
var instance = new MySwiftClass(
50+
// arena,
51+
1111, 2222);
4452

45-
SwiftKit.retain(obj.$memorySegment());
46-
System.out.println("[java] obj ref count = " + SwiftKit.retainCount(obj.$memorySegment()));
53+
System.out.println("MySwiftClass.TYPE_MANGLED_NAME = " + MySwiftClass.TYPE_MANGLED_NAME);
54+
var swiftType = SwiftKit.getTypeByMangledNameInEnvironment(MySwiftClass.TYPE_MANGLED_NAME);
55+
System.out.println("swiftType = " + swiftType);
56+
// MemorySegment typeMetadata = SwiftValueWitnessTable.fullTypeMetadata(swiftType.$memorySegment());
57+
// System.out.println("typeMetadata = " + typeMetadata);
58+
//
59+
//
60+
// System.out.printf("size of type = %d%n", SwiftValueWitnessTable.sizeOfSwiftType(swiftType.$memorySegment()));
61+
// System.out.printf("stride of type = %d%n", SwiftValueWitnessTable.strideOfSwiftType(swiftType.$memorySegment()));
62+
// System.out.printf("alignment of type = %d%n", SwiftValueWitnessTable.alignmentOfSwiftType(swiftType.$memorySegment()));
63+
// System.out.printf("layout of type = %s%n", SwiftValueWitnessTable.layoutOfSwiftType(swiftType.$memorySegment()).toString());
4764

48-
obj.voidMethod();
49-
obj.takeIntMethod(42);
65+
// } // instance should be deallocated
5066
}
5167
}

Samples/SwiftKitSampleApp/src/main/java/com/example/swift/ManualMySwiftClass.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public final class ManualMySwiftClass extends Manual_MySwiftClass implements Man
2727
// 000000000003f4a8 S type metadata for JavaKitExample.MySwiftSlice
2828
// strip the _$s
2929
// drop the N
30-
public static final String TYPE_METADATA_NAME = "14JavaKitExample12MySwiftClassC";
30+
public static final String TYPE_MANGLED_NAME = "14JavaKitExample12MySwiftClassC";
3131

3232
private final MemorySegment self;
3333

@@ -167,13 +167,13 @@ static MemorySegment __allocating_init_len_cap(long len, long cap) {
167167
if (ManualJavaKitExample.TRACE_DOWNCALLS) {
168168
ManualJavaKitExample.traceDowncall("MySwiftClass.__allocating_init(len:cap:)", len, cap);
169169
}
170-
ManualJavaKitExample.trace("type name = " + TYPE_METADATA_NAME);
170+
ManualJavaKitExample.trace("type name = " + TYPE_MANGLED_NAME);
171171

172172
// FIXME: problems with _getTypeByName because of the String memory repr
173-
// final MemorySegment type = SwiftKit.getTypeByMangledNameInEnvironment(TYPE_METADATA_NAME);
173+
// final MemorySegment type = SwiftKit.getTypeByMangledNameInEnvironment(TYPE_MANGLED_NAME);
174174
// we must get a method we can call like this into SwiftKit:
175175

176-
MemorySegment type = ManualJavaKitExample.swiftkit_getTypeByStringByteArray(TYPE_METADATA_NAME);
176+
MemorySegment type = ManualJavaKitExample.swiftkit_getTypeByStringByteArray(TYPE_MANGLED_NAME);
177177
ManualJavaKitExample.trace("type = " + type);
178178

179179
var address = (MemorySegment) mh$.invokeExact(len, cap, type);

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

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

1717
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Disabled;
1819
import org.junit.jupiter.api.Test;
1920
import org.junit.jupiter.api.condition.DisabledOnOs;
2021
import org.junit.jupiter.api.condition.OS;
@@ -48,9 +49,10 @@ void test_MySwiftClass_makeIntMethod() {
4849
}
4950

5051
@Test
52+
@Disabled // TODO: Need var mangled names in interfaces
5153
void test_MySwiftClass_property_len() {
5254
MySwiftClass o = new MySwiftClass(12, 42);
53-
var got = o.makeIntMethod();
55+
var got = o.getLen();
5456
assertEquals(12, got);
5557
}
5658

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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;
16+
17+
import com.example.swift.generated.MySwiftClass;
18+
import org.junit.jupiter.api.BeforeAll;
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.junit.jupiter.api.Assertions.*;
22+
import static org.swift.swiftkit.SwiftKit.*;
23+
import static org.swift.swiftkit.SwiftKit.retainCount;
24+
25+
public class SwiftArenaTest {
26+
27+
@BeforeAll
28+
static void beforeAll() {
29+
System.out.printf("java.library.path = %s\n", System.getProperty("java.library.path"));
30+
31+
System.loadLibrary("swiftCore");
32+
System.loadLibrary("ExampleSwiftLibrary");
33+
34+
System.setProperty("jextract.trace.downcalls", "true");
35+
}
36+
37+
@Test
38+
void arena_releaseClassOnClose_class_ok() {
39+
MySwiftClass unsafelyEscaped = null;
40+
41+
try (var arena = SwiftArena.ofConfined()) {
42+
var obj = new MySwiftClass(arena,1, 2);
43+
unsafelyEscaped = obj; // also known as "don't do this" (outliving the arena)
44+
45+
retain(obj.$memorySegment());
46+
assertEquals(2, retainCount(obj.$memorySegment()));
47+
48+
release(obj.$memorySegment());
49+
assertEquals(1, retainCount(obj.$memorySegment()));
50+
}
51+
52+
// TODO: should we zero out the $memorySegment perhaps?
53+
}
54+
55+
@Test
56+
void arena_releaseClassOnClose_class_leaked() {
57+
String memorySegmentDescription = "<none>";
58+
59+
try {
60+
try (var arena = SwiftArena.ofConfined()) {
61+
var obj = new MySwiftClass(arena,1, 2);
62+
memorySegmentDescription = obj.$memorySegment().toString();
63+
64+
// Pretend that we "leaked" the class, something still holds a reference to it while we try to destroy it
65+
retain(obj.$memorySegment());
66+
assertEquals(2, retainCount(obj.$memorySegment()));
67+
}
68+
69+
fail("Expected exception to be thrown while the arena is closed!");
70+
} catch (Exception ex) {
71+
// The message should point out which objects "leaked":
72+
assertTrue(ex.getMessage().contains(memorySegmentDescription));
73+
}
74+
75+
}
76+
}

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/SwiftArenaTests.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

Sources/ExampleSwiftLibrary/MySwiftLibrary.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ public class MySwiftClass {
4848
p("\(MySwiftClass.self).cap = \(self.cap)")
4949
let addr = unsafeBitCast(self, to: UInt64.self)
5050
p("initializer done, self = 0x\(String(addr, radix: 16, uppercase: true))")
51+
52+
p(" ty = \(MySwiftClass.self) @ \(ObjectIdentifier(MySwiftClass.self))")
53+
54+
let s = "19ExampleSwiftLibrary02MyB5ClassC"
55+
if let ty = _typeByName(s) {
56+
p("any ty (\(s) = \(ty) @ \(ObjectIdentifier(ty))")
57+
assert(ObjectIdentifier(ty) == ObjectIdentifier(MySwiftClass.self))
58+
} else {
59+
p("any ty (\(s) = @ nil")
60+
}
61+
62+
let s2 = "$s19ExampleSwiftLibrary02MyB5ClassC"
63+
if let ty = _typeByName(s2) {
64+
p("any ty (\(s2) = \(ty) @ \(ObjectIdentifier(ty))")
65+
} else {
66+
p("any ty (\(s2) = @ nil")
67+
}
68+
p("USE THE swift_getTypeByMangledNameInEnvironment -----")
69+
if let ty = _getTypeByMangledNameInEnvironment(s, UInt(s.count), genericEnvironment: nil, genericArguments: nil) {
70+
p("any ty (\(s) = \(ty) @ \(ObjectIdentifier(ty))")
71+
assert(ObjectIdentifier(ty) == ObjectIdentifier(MySwiftClass.self))
72+
} else {
73+
p("any ty (\(s) = @ nil")
74+
}
5175
}
5276

5377
deinit {
@@ -76,6 +100,15 @@ public class MySwiftClass {
76100
}
77101
}
78102

103+
@_silgen_name("swift_getTypeByMangledNameInEnvironment")
104+
public func _getTypeByMangledNameInEnvironment(
105+
_ name: UnsafePointer<UInt8>,
106+
_ nameLength: UInt,
107+
genericEnvironment: UnsafeRawPointer?,
108+
genericArguments: UnsafeRawPointer?)
109+
-> Any.Type?
110+
111+
79112
// ==== Internal helpers
80113

81114
private func p(_ msg: String, file: String = #fileID, line: UInt = #line, function: String = #function) {

0 commit comments

Comments
 (0)