Skip to content

Commit c531f01

Browse files
committed
PR feedback
1 parent 3c12d2d commit c531f01

37 files changed

+244
-278
lines changed

BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,6 @@ repositories {
3131
mavenCentral()
3232
}
3333

34-
testing {
35-
suites {
36-
// val test by getting(JvmTestSuite::class) {
37-
// useJUnitJupiter("5.10.3")
38-
// }
39-
}
40-
}
41-
42-
/// Enable access to preview APIs, e.g. java.lang.foreign.* (Panama)
43-
tasks.withType(JavaCompile::class).forEach {
44-
it.options.compilerArgs.add("--enable-preview")
45-
it.options.compilerArgs.add("-Xlint:preview")
46-
}
47-
48-
4934
fun getSwiftRuntimeLibraryPaths(): List<String> {
5035
val process = ProcessBuilder("swiftc", "-print-target-info")
5136
.redirectError(ProcessBuilder.Redirect.INHERIT)

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

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

1919
// Import javakit/swiftkit support libraries
2020

21-
import org.swift.swiftkitffm.SwiftKit;
21+
import org.swift.swiftkit.core.SwiftLibraries;
2222

2323
public class HelloJava2SwiftJNI {
2424

2525
public static void main(String[] args) {
26-
System.out.print("Property: java.library.path = " + SwiftKit.getJavaLibraryPath());
26+
System.out.print("Property: java.library.path = " + SwiftLibraries.getJavaLibraryPath());
2727

2828
examples();
2929
}
@@ -35,10 +35,8 @@ static void examples() {
3535
MySwiftLibrary.globalTakeIntInt(1337, 42);
3636

3737
long cnt = MySwiftLibrary.globalWriteString("String from Java");
38-
SwiftKit.trace("count = " + cnt);
3938

4039
long i = MySwiftLibrary.globalMakeInt();
41-
SwiftKit.trace("globalMakeInt() = " + i);
4240

4341
MySwiftClass.method();
4442

Samples/SwiftAndJavaJarSampleLib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ java {
4343
}
4444

4545
dependencies {
46+
implementation(project(':SwiftKitCore'))
4647
implementation(project(':SwiftKitFFM'))
4748

4849
testImplementation(platform("org.junit:junit-bom:5.10.0"))

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
// Import swift-extract generated sources
1818

1919
// Import javakit/swiftkit support libraries
20-
import org.swift.swiftkitffm.SwiftArena;
21-
import org.swift.swiftkitffm.SwiftFFM;
20+
import org.swift.swiftkit.ffm.AllocatingSwiftArena;
21+
import org.swift.swiftkit.ffm.SwiftRuntime;
22+
import org.swift.swiftkit.core.SwiftLibraries;
2223

2324
public class HelloJava2Swift {
2425

2526
public static void main(String[] args) {
2627
boolean traceDowncalls = Boolean.getBoolean("jextract.trace.downcalls");
2728
System.out.println("Property: jextract.trace.downcalls = " + traceDowncalls);
2829

29-
System.out.print("Property: java.library.path = " + SwiftFFM.getJavaLibraryPath());
30+
System.out.print("Property: java.library.path = " + SwiftLibraries.getJavaLibraryPath());
3031

3132
examples();
3233
}
@@ -37,12 +38,12 @@ static void examples() {
3738
MySwiftLibrary.globalTakeInt(1337);
3839

3940
// Example of using an arena; MyClass.deinit is run at end of scope
40-
try (var arena = SwiftArena.ofConfined()) {
41+
try (var arena = AllocatingSwiftArena.ofConfined()) {
4142
MySwiftClass obj = MySwiftClass.init(2222, 7777, arena);
4243

4344
// just checking retains/releases work
44-
SwiftFFM.retain(obj);
45-
SwiftFFM.release(obj);
45+
SwiftRuntime.retain(obj);
46+
SwiftRuntime.release(obj);
4647

4748
obj.voidMethod();
4849
obj.takeIntMethod(42);

Samples/SwiftAndJavaJarSampleLib/src/test/java/org/swift/swiftkitffm/SwiftArenaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.junit.jupiter.api.condition.DisabledIf;
2020
import org.swift.swiftkitffm.util.PlatformUtils;
2121

22-
import static org.junit.jupiter.api.Assertions.*;
2322
import static org.swift.swiftkitffm.SwiftFFM.retainCount;
2423

2524
public class SwiftArenaTest {

SwiftKitCore/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
id("build-logic.java-application-conventions")
1717
}
1818

19-
group = "org.swift.swiftkitcore"
19+
group = "org.swift.swiftkit"
2020
version = "1.0-SNAPSHOT"
2121

2222
repositories {
@@ -25,15 +25,15 @@ repositories {
2525

2626
java {
2727
toolchain {
28-
languageVersion.set(JavaLanguageVersion.of(24))
28+
// JDK 19 is the last JDK to support Java 7
29+
languageVersion.set(JavaLanguageVersion.of(19))
2930
}
3031
// Support Android 6+ (Java 7)
3132
sourceCompatibility = JavaVersion.VERSION_1_7
3233
}
3334

3435
dependencies {
35-
// testImplementation(platform("org.junit:junit-bom:5.10.0"))
36-
// testImplementation("org.junit.jupiter:junit-jupiter")
36+
testImplementation 'junit:junit:4.13.2'
3737
}
3838

3939
tasks.test {

SwiftKitCore/src/main/java/org/swift/swiftkitcore/ClosableSwiftArena.java renamed to SwiftKitCore/src/main/java/org/swift/swiftkit/core/ClosableSwiftArena.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
package org.swift.swiftkitcore;
15+
package org.swift.swiftkit.core;
1616

1717
/**
1818
* Auto-closable version of {@link SwiftArena}.

SwiftKitCore/src/main/java/org/swift/swiftkitcore/ConfinedSwiftMemorySession.java renamed to SwiftKitCore/src/main/java/org/swift/swiftkit/core/ConfinedSwiftMemorySession.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
package org.swift.swiftkitcore;
15+
package org.swift.swiftkit.core;
1616

17-
import java.lang.foreign.Arena;
18-
import java.lang.foreign.MemorySegment;
1917
import java.util.LinkedList;
2018
import java.util.List;
2119
import java.util.concurrent.atomic.AtomicInteger;
@@ -58,7 +56,7 @@ public void close() {
5856
public void register(SwiftInstance instance) {
5957
checkValid();
6058

61-
SwiftInstanceCleanup cleanup = instance.makeCleanupAction();
59+
SwiftInstanceCleanup cleanup = instance.createCleanupAction();
6260
this.resources.add(cleanup);
6361
}
6462

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.core;
16+
17+
import java.util.concurrent.atomic.AtomicBoolean;
18+
19+
public abstract class JNISwiftInstance extends SwiftInstance {
20+
/**
21+
* The designated constructor of any imported Swift types.
22+
*
23+
* @param pointer a pointer to the memory containing the value
24+
* @param arena the arena this object belongs to. When the arena goes out of scope, this value is destroyed.
25+
*/
26+
protected JNISwiftInstance(long pointer, SwiftArena arena) {
27+
super(pointer, arena);
28+
}
29+
30+
/**
31+
* Creates a function that will be called when the value should be destroyed.
32+
* This will be code-generated to call a native method to do deinitialization and deallocation.
33+
* <p>
34+
* The reason for this "indirection" is that we cannot have static methods on abstract classes,
35+
* and we can't define the destroy method as a member method, because we assume that the wrapper
36+
* has been released, when we destroy.
37+
* <p>
38+
* <b>Warning:</b> The function must not capture {@code this}.
39+
*
40+
* @return a function that is called when the value should be destroyed.
41+
*/
42+
abstract Runnable $createDestroyFunction();
43+
44+
@Override
45+
public SwiftInstanceCleanup createCleanupAction() {
46+
final AtomicBoolean statusDestroyedFlag = $statusDestroyedFlag();
47+
Runnable markAsDestroyed = new Runnable() {
48+
@Override
49+
public void run() {
50+
statusDestroyedFlag.set(true);
51+
}
52+
};
53+
54+
return new JNISwiftInstanceCleanup(this.$createDestroyFunction(), markAsDestroyed);
55+
}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.core;
16+
17+
class JNISwiftInstanceCleanup implements SwiftInstanceCleanup {
18+
private final Runnable destroyFunction;
19+
private final Runnable markAsDestroyed;
20+
21+
public JNISwiftInstanceCleanup(Runnable destroyFunction, Runnable markAsDestroyed) {
22+
this.destroyFunction = destroyFunction;
23+
this.markAsDestroyed = markAsDestroyed;
24+
}
25+
26+
@Override
27+
public void run() {
28+
markAsDestroyed.run();
29+
destroyFunction.run();
30+
}
31+
}

0 commit comments

Comments
 (0)