From 6429f96dc7039ade5669b9bcd1a1b6540817b6b8 Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Tue, 4 Nov 2025 18:55:47 +0900 Subject: [PATCH 01/12] Prepare 2nd blog post for summer of code summary posts (2025) These posts were approved by their respective contributor co-authors over here in https://github.com/swiftlang/swift-org-website/pull/1191 also link to the android sdk blog post, since the screenshot there showcases this work Apply suggestions from code review --- _data/authors.yml | 6 + ...ighlight-2-swift-java-jextract-jni-mode.md | 130 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 _posts/2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md diff --git a/_data/authors.yml b/_data/authors.yml index 4bd0c9f1a..71d393216 100644 --- a/_data/authors.yml +++ b/_data/authors.yml @@ -560,6 +560,12 @@ mitchellallison: github: mitchellallison about: "Mitchell Allison works on Distributed Systems in Swift at Apple." +mads: + name: Mads Odgaard + email: mads@madsodgaard.com + github: madsodgaard + about: Mads is a Tech Lead at Frameo. During Google Summer of Code 2025, he worked on bringing JNI support to the jextract tool which is part of the Swift Java interoperability project. + heckj: name: Joe Heck email: j_heck@apple.com diff --git a/_posts/2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md b/_posts/2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md new file mode 100644 index 000000000..496caf34d --- /dev/null +++ b/_posts/2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md @@ -0,0 +1,130 @@ +--- +layout: new-layouts/post +published: true +date: 2025-02-NN 10:00:00 +title: 'Swift GSoC 2025 highlight: JNI mode for SwiftJava interoperability jextract tool +author: [ktoso, mads] +category: "Community" +--- + +Another year of successful Swift participation in [Google Summer of Code](https://summerofcode.withgoogle.com) 2025 came to an end recently, and this year we'd like to shine some light on the projects and work acomplished during the summer! + +Summer of Code is an annual program, organized by Google, which provides hands-on experience for newcomers contributing +to open source projects. Participants usually are students, but do not have to be. + +In this series of four posts, we'll highlight each of the Summer of Code contributors and their projects. + +- [Bringing Swiftly support to VS Code](2025-11-NN-swift-gsoc-2025-highlight-1-vscode-swiftly.md) +- JNI mode for swift-java’s source jextract tool (this post) +- [Improve the display of documentation during code completion in SourceKit-LSP](2025-11-NN-swift-gsoc-2025-highlight-3-vscode-swift-lsp-documentation.md) +- [Improved Console Output for Swift Testing](2025-11-NN-swift-gsoc-2025-highlight-4-swift-testing-output.md) + +--- + +## JNI mode for SwiftJava interoperability jextract tool + +My name is Mads and I am excited to share with you what I have been working on for Swift/Java interoperability over the summer with my mentor Konrad for Google Summer of Code 2025. + +# Overview + +> You can also view Mads' presentation from the Serverside.swift conference about his work on this project: [Expanding Swift/Java Interoperability](https://www.youtube.com/watch?v=tOH6V1IvTAc). You may also have noticed it in action in the recent Swift.org blog post: [Announcing the Swift SDK for Android](https://www.swift.org/blog/nightly-swift-sdk-for-android/)! + +The [swift-java](https://github.com/swiftlang/swift-java) interoperability library provides the `swift-java jextract` tool, which automatically generates Java sources that are used to call Swift code from Java. Previously, this tool only worked using the [Foreign Function and Memory API (FFM)](https://docs.oracle.com/en/java/javase/21/core/foreign-function-and-memory-api.html), which requires JDK 22+, making it unavailable on platforms such as Android. The goal of this project was to extend the jextract tool, such that it is able to generate Java sources using JNI instead of FFM and thereby allowing more platforms to utilize Swift/Java interoperability. + +I am very glad to report that we have succeeded in that goal, supporting even more features than initially planned! Our initial goal was to achieve feature parity with the FFM mode, but the new JNI mode also supports additional Swift language features such as enums and protocols! + +With the outcome of this project, you can now run the following command to automatically generate Java wrappers for your Swift library using JNI, therefore opening up the possibility of using it on platforms such as Android. + +```bash +swift-java jextract --swift-module MySwiftLibrary \ + --mode jni \ + --input-swift Sources/MySwiftLibrary \ + --output-java out/java \ + --output-swift out/swift +``` + +# How does it work? + +Each Swift class/struct is extracted as a single Java `class`. Functions and variables are generated as Java methods, that internally calls down to a native method that is implemented in Swift using `@_cdecl`. Take a look at the following example: + +```swift +public class MySwiftClass { + public let x: Int64 + public init(x: Int64) { + self.x = x + } + + public func printMe() { + print(“\(self.x)”); + } +} +``` + +It is roughly generated to the equivalent Java `class`: + +```java +public final class MySwiftClass implements JNISwiftInstance { + public static MySwiftClass init(long x, long y, SwiftArena swiftArena$) { + return MySwiftClass.wrapMemoryAddressUnsafe(MySwiftClass.$init(x, y), swiftArena$); + } + + public long getX() { + return MySwiftClass.$getX(this.$memoryAddress()); + } + + public void printMe() { + MySwiftClass.$printMe(this.$memoryAddress()); + } + + private static native long $init(long x, long y); + private static native long $getX(long self); + private static native void $printMe(long self); +} +``` +We also generate additional Swift thunks that actually implement the `native` methods and call the underlying Swift methods. + +You can learn more about how the memory allocation and management works [in the full version of this post of this post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/81858)! + +An interesting aspect of an interoperability library such as `swift-java` is the memory management between the two sides, in this case the JVM and Swift. The FFM mode uses the FFM APIs around `MemorySegment` to allocate and manage native memory. We are not so lucky in JNI. In older Java versions there are different ways of allocating memory, such as `Unsafe` or `ByteBuffer.allocateDirect()`. We could have decided to use these and allocate memory on the Java side, like FFM, but instead we decided to move the responsibility to Swift, which allocates the memory instead. This had some nice upsides, as we did not have to mess the the witness tables like FFM does. + +> For more info on memory in FFM, I strongly recommend watching Konrad’s talk [try\! Swift Tokyo 2025 \- Foreign Function and Memory APIs and Swift/Java interoperability](https://www.youtube.com/watch?v=vgtzhTOhEbs) + +The most obvious place we need to allocate memory is when we initialize a wrapped Swift `class`. Take a look at the following generated code for a Swift initializer: +```java +public static MySwiftClass init(SwiftArena swiftArena$) { + return MySwiftClass.wrapMemoryAddressUnsafe(MySwiftClass.$init(), swiftArena$); +} +private static native long $init(); +``` +Here we see that we are calling a native method `$init` which returns a `long`. This value is a pointer to the Swift instance in the memory space of Swift. It is passed to `wrapMemoryAddressUnsafe`, which is basically just storing the pointer in a local field and registering the wrapper to the `SwiftArena`. + +`SwiftArena` is a type that is used to ensure we eventually deallocate the memory when the Java wrapper is no longer needed. There exists two implements of this: + +1. `SwiftArena.ofConfined()`: returns a confined arena which is used with *try-with-resource*, to deallocate all instances at the end of some scope. +2. `SwiftArena.ofAuto()`: returns an arena that deallocates instances once the garbage-collector has decided to do so. + +This concept also exists in the FFM mode, and I recommend watching Konrad’s talk to learn more about them! + +If we take a look at the native implementation of `$init` in Swift, we see how we allocate and initialize the memory: +```swift +// Generated code, not something you would write + +@_cdecl("Java_com_example_swift_MySwiftClass__00024init__JJ") +func Java_com_example_swift_MySwiftClass__00024init__JJ(environment: UnsafeMutablePointer!, thisClass: jclass, x: jlong, y: jlong) -> jlong { + let result$ = UnsafeMutablePointer.allocate(capacity: 1) + result$.initialize(to: MySwiftClass.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!))) + let resultBits$ = Int64(Int(bitPattern: result$)) + return resultBits$.getJNIValue(in: environment!) +} +``` +We are basically allocating memory for a single instance of `MySwiftClass`, initializing it to a new instance and returning the memory address of the pointer. It is the same approach for `struct` as well\! + +# My experience with GSoC + +Google Summer of Code was an awesome experience for me\! I got to work with my favourite language on a library that is very relevant\! A **HUGE** thanks to my mentor @ktoso, who provided invaluable guidance, was always available for questions and allowed me to experiment and take ownership of the work\! + +I would definitely recommend GSoC to anyone interested, it is a great way to engage with the open-source community, develop your skills and work with some talented people\! My number one advice would be to never be afraid of asking seemingly “stupid” questions, they might not be that stupid afterall. + +--- + +If you'd like to learn more about this project, please [check out the full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/81858) as it contains lots of more additional examples and in-depth discussion about memory management and trade-offs this project had to resolve! From 4101646714a10fe6b13ddeaa42a9f6fffb520b09 Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Tue, 4 Nov 2025 20:32:58 +0900 Subject: [PATCH 02/12] adjust my bio (ktoso) --- _data/authors.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_data/authors.yml b/_data/authors.yml index 71d393216..1d94a6e48 100644 --- a/_data/authors.yml +++ b/_data/authors.yml @@ -202,7 +202,7 @@ ktoso: email: ktoso@apple.com github: ktoso gravatar: 03cb20b97f6a14701c24c4e088b6af87 - about: "Konrad Malawski is a member of a team developing foundational server-side Swift libraries at Apple, with focus on distributed systems and concurrency." + about: "Konrad is part of the Swift language team and Swift on Server Workgroup, where he works on language and runtime features with particular interest in supporting server and distributed computing use-cases, and expanding the Swift ecosystem." compnerd: name: Saleem Abdulrasool From 7a6a66d1eaba5e2b4faba69e44ecc79d11d4fcd5 Mon Sep 17 00:00:00 2001 From: Dave Lester <18080+davelester@users.noreply.github.com> Date: Wed, 5 Nov 2025 23:36:08 -0800 Subject: [PATCH 03/12] Rename 2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md to 2025-11-07-gsoc-2025-showcase-swift-java.md Revised filename to be consistent with previous GSoC post. --- ...ct-jni-mode.md => 2025-11-07-gsoc-2025-showcase-swift-java.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename _posts/{2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md => 2025-11-07-gsoc-2025-showcase-swift-java.md} (100%) diff --git a/_posts/2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md similarity index 100% rename from _posts/2025-11-NN-swift-gsoc-2025-highlight-2-swift-java-jextract-jni-mode.md rename to _posts/2025-11-07-gsoc-2025-showcase-swift-java.md From 8e01f2908bc4305149cda8fceda5bfd8e1099274 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 6 Nov 2025 18:32:10 +0900 Subject: [PATCH 04/12] Apply suggestions from code review Co-authored-by: Dave Lester <18080+davelester@users.noreply.github.com> --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index 496caf34d..2d2a42a6d 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -1,9 +1,9 @@ --- layout: new-layouts/post published: true -date: 2025-02-NN 10:00:00 -title: 'Swift GSoC 2025 highlight: JNI mode for SwiftJava interoperability jextract tool -author: [ktoso, mads] +date: 2025-11-07 10:00:00 +title: "Swift GSoC 2025 highlight: JNI mode for SwiftJava interoperability jextract tool" +author: [mads, ktoso] category: "Community" --- @@ -14,11 +14,12 @@ to open source projects. Participants usually are students, but do not have to b In this series of four posts, we'll highlight each of the Summer of Code contributors and their projects. -- [Bringing Swiftly support to VS Code](2025-11-NN-swift-gsoc-2025-highlight-1-vscode-swiftly.md) +- [Bringing Swiftly support to VS Code](/blog/gsoc-2025-showcase-swiftly-support-in-vscode/) - JNI mode for swift-java’s source jextract tool (this post) -- [Improve the display of documentation during code completion in SourceKit-LSP](2025-11-NN-swift-gsoc-2025-highlight-3-vscode-swift-lsp-documentation.md) -- [Improved Console Output for Swift Testing](2025-11-NN-swift-gsoc-2025-highlight-4-swift-testing-output.md) +- Improved display of documentation during code completion in SourceKit-LSP _(coming soon)_ +- Improved Console Output for Swift Testing _(coming soon)_ +The second GSoC 2025 project we’re featuring on the Swift blog improved Swift/Java interoperability, contributed by Mads Odgaard. To learn more, you can read the [full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/8185). --- ## JNI mode for SwiftJava interoperability jextract tool From 0c17617f565d5b8103466ec30671c3656fe48420 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 6 Nov 2025 19:31:04 +0900 Subject: [PATCH 05/12] update whitespace to not cause header formatting Added a section about JNI mode for Swift/Java interoperability. --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index 2d2a42a6d..4ae597914 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -20,6 +20,7 @@ In this series of four posts, we'll highlight each of the Summer of Code contrib - Improved Console Output for Swift Testing _(coming soon)_ The second GSoC 2025 project we’re featuring on the Swift blog improved Swift/Java interoperability, contributed by Mads Odgaard. To learn more, you can read the [full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/8185). + --- ## JNI mode for SwiftJava interoperability jextract tool From 87ade08f9b251fdcd9deef37ae4d8912cb885526 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 6 Nov 2025 19:34:48 +0900 Subject: [PATCH 06/12] Apply suggestions from code review Co-authored-by: Dave Lester <18080+davelester@users.noreply.github.com> --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index 4ae597914..3c726919c 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -89,7 +89,7 @@ You can learn more about how the memory allocation and management works [in the An interesting aspect of an interoperability library such as `swift-java` is the memory management between the two sides, in this case the JVM and Swift. The FFM mode uses the FFM APIs around `MemorySegment` to allocate and manage native memory. We are not so lucky in JNI. In older Java versions there are different ways of allocating memory, such as `Unsafe` or `ByteBuffer.allocateDirect()`. We could have decided to use these and allocate memory on the Java side, like FFM, but instead we decided to move the responsibility to Swift, which allocates the memory instead. This had some nice upsides, as we did not have to mess the the witness tables like FFM does. -> For more info on memory in FFM, I strongly recommend watching Konrad’s talk [try\! Swift Tokyo 2025 \- Foreign Function and Memory APIs and Swift/Java interoperability](https://www.youtube.com/watch?v=vgtzhTOhEbs) +> For more info on memory in FFM, I strongly recommend watching Konrad’s talk [try! Swift Tokyo 2025 \- Foreign Function and Memory APIs and Swift/Java interoperability](https://www.youtube.com/watch?v=vgtzhTOhEbs) The most obvious place we need to allocate memory is when we initialize a wrapped Swift `class`. Take a look at the following generated code for a Swift initializer: ```java @@ -119,14 +119,10 @@ func Java_com_example_swift_MySwiftClass__00024init__JJ(environment: UnsafeMutab return resultBits$.getJNIValue(in: environment!) } ``` -We are basically allocating memory for a single instance of `MySwiftClass`, initializing it to a new instance and returning the memory address of the pointer. It is the same approach for `struct` as well\! +We are basically allocating memory for a single instance of `MySwiftClass`, initializing it to a new instance and returning the memory address of the pointer. It is the same approach for `struct` as well! # My experience with GSoC -Google Summer of Code was an awesome experience for me\! I got to work with my favourite language on a library that is very relevant\! A **HUGE** thanks to my mentor @ktoso, who provided invaluable guidance, was always available for questions and allowed me to experiment and take ownership of the work\! +Google Summer of Code was an awesome experience for me! I got to work with my favorite language on a library that is very relevant! A HUGE thanks to my mentor Konrad Malawski, who provided invaluable guidance, was always available for questions and allowed me to experiment and take ownership of the work! -I would definitely recommend GSoC to anyone interested, it is a great way to engage with the open-source community, develop your skills and work with some talented people\! My number one advice would be to never be afraid of asking seemingly “stupid” questions, they might not be that stupid afterall. - ---- - -If you'd like to learn more about this project, please [check out the full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/81858) as it contains lots of more additional examples and in-depth discussion about memory management and trade-offs this project had to resolve! +I would definitely recommend GSoC to anyone interested, it is a great way to engage with the open-source community, develop your skills and work with some talented people! My number one advice would be to never be afraid of asking seemingly “stupid” questions, they might not be that stupid afterall. From 839e734c1560d821b91432be2559289ce14a3949 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Fri, 7 Nov 2025 14:32:33 +0900 Subject: [PATCH 07/12] Apply suggestions from code review Co-authored-by: Dave Lester <18080+davelester@users.noreply.github.com> --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index 3c726919c..917b30c0c 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -7,19 +7,14 @@ author: [mads, ktoso] category: "Community" --- -Another year of successful Swift participation in [Google Summer of Code](https://summerofcode.withgoogle.com) 2025 came to an end recently, and this year we'd like to shine some light on the projects and work acomplished during the summer! - -Summer of Code is an annual program, organized by Google, which provides hands-on experience for newcomers contributing -to open source projects. Participants usually are students, but do not have to be. - -In this series of four posts, we'll highlight each of the Summer of Code contributors and their projects. +This is the second post our series showcasing the Swift community’s participation in [Google Summer of Code](https://summerofcode.withgoogle.com) 2025. Learn more about the projects and work accomplished: - [Bringing Swiftly support to VS Code](/blog/gsoc-2025-showcase-swiftly-support-in-vscode/) - JNI mode for swift-java’s source jextract tool (this post) - Improved display of documentation during code completion in SourceKit-LSP _(coming soon)_ - Improved Console Output for Swift Testing _(coming soon)_ -The second GSoC 2025 project we’re featuring on the Swift blog improved Swift/Java interoperability, contributed by Mads Odgaard. To learn more, you can read the [full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/8185). +Each GSoC contributor has shared a writeup about their project and experience in the program on the Swift forums. Today’s featured project improved interoperability between Swift and Java, and was contributed by Mads Odgaard. To learn more, you can read the [full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/8185). --- From 2bd641d53a6de4d912af1624862e8de67ead9d3d Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Fri, 7 Nov 2025 14:44:25 +0900 Subject: [PATCH 08/12] reword sentence to avoid use of 'stupid questions' --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index 917b30c0c..f185aa680 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -120,4 +120,4 @@ We are basically allocating memory for a single instance of `MySwiftClass`, init Google Summer of Code was an awesome experience for me! I got to work with my favorite language on a library that is very relevant! A HUGE thanks to my mentor Konrad Malawski, who provided invaluable guidance, was always available for questions and allowed me to experiment and take ownership of the work! -I would definitely recommend GSoC to anyone interested, it is a great way to engage with the open-source community, develop your skills and work with some talented people! My number one advice would be to never be afraid of asking seemingly “stupid” questions, they might not be that stupid afterall. +I would definitely recommend GSoC to anyone interested, it is a great way to engage with the open-source community, develop your skills and work with some talented people! My number one advice would be to never be afraid of asking what might seem to be embarrassing questions, they might actually turn out to be great questions and lead to useful discussions and solutions. From e3642924ef349aa2b479aeeb1638b60fcc26683e Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Fri, 7 Nov 2025 14:50:18 +0900 Subject: [PATCH 09/12] first instance of mentor name to include surname --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index f185aa680..697e9d4f3 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -20,7 +20,7 @@ Each GSoC contributor has shared a writeup about their project and experience in ## JNI mode for SwiftJava interoperability jextract tool -My name is Mads and I am excited to share with you what I have been working on for Swift/Java interoperability over the summer with my mentor Konrad for Google Summer of Code 2025. +My name is Mads and I am excited to share with you what I have been working on for Swift/Java interoperability over the summer with my mentor Konrad Malawski for Google Summer of Code 2025. # Overview From 815c6c5bf485e941f882a44c2db5039fcf2d544f Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Fri, 7 Nov 2025 16:07:20 +0900 Subject: [PATCH 10/12] Apply suggestions from code review Co-authored-by: Dave Lester <18080+davelester@users.noreply.github.com> --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index 697e9d4f3..a61611045 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -2,19 +2,21 @@ layout: new-layouts/post published: true date: 2025-11-07 10:00:00 -title: "Swift GSoC 2025 highlight: JNI mode for SwiftJava interoperability jextract tool" +title: "GSoC 2025 Showcase: Extending Swift-Java Interoperability" author: [mads, ktoso] category: "Community" --- -This is the second post our series showcasing the Swift community’s participation in [Google Summer of Code](https://summerofcode.withgoogle.com) 2025. Learn more about the projects and work accomplished: +This is the second post in our series showcasing the Swift community’s participation in [Google Summer of Code](https://summerofcode.withgoogle.com) 2025. Learn more about the projects and work accomplished: - [Bringing Swiftly support to VS Code](/blog/gsoc-2025-showcase-swiftly-support-in-vscode/) -- JNI mode for swift-java’s source jextract tool (this post) +- Extending Swift-Java Interoperability (this post) - Improved display of documentation during code completion in SourceKit-LSP _(coming soon)_ - Improved Console Output for Swift Testing _(coming soon)_ -Each GSoC contributor has shared a writeup about their project and experience in the program on the Swift forums. Today’s featured project improved interoperability between Swift and Java, and was contributed by Mads Odgaard. To learn more, you can read the [full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/8185). +Each GSoC contributor has shared a writeup about their project and experience in the program on the Swift forums. Today’s featured project improved interoperability between Swift and Java, contributed by Mads Odgaard. Mads recently presented on this project at the Server-Side Swift conference: [Expanding Swift/Java Interoperability](https://www.youtube.com/watch?v=tOH6V1IvTAc), and you may have noticed it in action in the recent Swift.org blog post: [Announcing the Swift SDK for Android](https://www.swift.org/blog/nightly-swift-sdk-for-android/)! + +To learn more, you can read the [full post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/8185). --- @@ -24,8 +26,6 @@ My name is Mads and I am excited to share with you what I have been working on f # Overview -> You can also view Mads' presentation from the Serverside.swift conference about his work on this project: [Expanding Swift/Java Interoperability](https://www.youtube.com/watch?v=tOH6V1IvTAc). You may also have noticed it in action in the recent Swift.org blog post: [Announcing the Swift SDK for Android](https://www.swift.org/blog/nightly-swift-sdk-for-android/)! - The [swift-java](https://github.com/swiftlang/swift-java) interoperability library provides the `swift-java jextract` tool, which automatically generates Java sources that are used to call Swift code from Java. Previously, this tool only worked using the [Foreign Function and Memory API (FFM)](https://docs.oracle.com/en/java/javase/21/core/foreign-function-and-memory-api.html), which requires JDK 22+, making it unavailable on platforms such as Android. The goal of this project was to extend the jextract tool, such that it is able to generate Java sources using JNI instead of FFM and thereby allowing more platforms to utilize Swift/Java interoperability. I am very glad to report that we have succeeded in that goal, supporting even more features than initially planned! Our initial goal was to achieve feature parity with the FFM mode, but the new JNI mode also supports additional Swift language features such as enums and protocols! @@ -80,7 +80,7 @@ public final class MySwiftClass implements JNISwiftInstance { ``` We also generate additional Swift thunks that actually implement the `native` methods and call the underlying Swift methods. -You can learn more about how the memory allocation and management works [in the full version of this post of this post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/81858)! +You can learn more about how the memory allocation and management works [in the full version of this post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/81858)! An interesting aspect of an interoperability library such as `swift-java` is the memory management between the two sides, in this case the JVM and Swift. The FFM mode uses the FFM APIs around `MemorySegment` to allocate and manage native memory. We are not so lucky in JNI. In older Java versions there are different ways of allocating memory, such as `Unsafe` or `ByteBuffer.allocateDirect()`. We could have decided to use these and allocate memory on the Java side, like FFM, but instead we decided to move the responsibility to Swift, which allocates the memory instead. This had some nice upsides, as we did not have to mess the the witness tables like FFM does. From a3212806f3fa3b51f7e4d7193e7e8407e1bf54b4 Mon Sep 17 00:00:00 2001 From: Dave Lester <18080+davelester@users.noreply.github.com> Date: Fri, 7 Nov 2025 11:49:12 -0800 Subject: [PATCH 11/12] Update _posts/2025-11-07-gsoc-2025-showcase-swift-java.md --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index a61611045..718cd3376 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -100,7 +100,8 @@ Here we see that we are calling a native method `$init` which returns a `long`. 1. `SwiftArena.ofConfined()`: returns a confined arena which is used with *try-with-resource*, to deallocate all instances at the end of some scope. 2. `SwiftArena.ofAuto()`: returns an arena that deallocates instances once the garbage-collector has decided to do so. -This concept also exists in the FFM mode, and I recommend watching Konrad’s talk to learn more about them! +This concept also exists in the FFM mode, and I recommend watching [Konrad’s talk at +try! Swift Tokyo 2025](https://www.youtube.com/watch?v=vgtzhTOhEbs) to learn more! If we take a look at the native implementation of `$init` in Swift, we see how we allocate and initialize the memory: ```swift From be7709da51db61a20bad068718b6bba1e6c35af4 Mon Sep 17 00:00:00 2001 From: Dave Lester <18080+davelester@users.noreply.github.com> Date: Fri, 7 Nov 2025 11:49:29 -0800 Subject: [PATCH 12/12] Update _posts/2025-11-07-gsoc-2025-showcase-swift-java.md --- _posts/2025-11-07-gsoc-2025-showcase-swift-java.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md index 718cd3376..5daf589be 100644 --- a/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md +++ b/_posts/2025-11-07-gsoc-2025-showcase-swift-java.md @@ -82,7 +82,7 @@ We also generate additional Swift thunks that actually implement the `native` me You can learn more about how the memory allocation and management works [in the full version of this post on the Swift forums](https://forums.swift.org/t/gsoc-2025-new-jni-mode-added-to-swift-java-jextract-tool/81858)! -An interesting aspect of an interoperability library such as `swift-java` is the memory management between the two sides, in this case the JVM and Swift. The FFM mode uses the FFM APIs around `MemorySegment` to allocate and manage native memory. We are not so lucky in JNI. In older Java versions there are different ways of allocating memory, such as `Unsafe` or `ByteBuffer.allocateDirect()`. We could have decided to use these and allocate memory on the Java side, like FFM, but instead we decided to move the responsibility to Swift, which allocates the memory instead. This had some nice upsides, as we did not have to mess the the witness tables like FFM does. +An interesting aspect of an interoperability library such as `swift-java` is the memory management between the two sides, in this case the JVM and Swift. The FFM mode uses the FFM APIs around `MemorySegment` to allocate and manage native memory. We are not so lucky in JNI. In older Java versions there are different ways of allocating memory, such as `Unsafe` or `ByteBuffer.allocateDirect()`. We could have decided to use these and allocate memory on the Java side, like FFM, but instead we decided to move the responsibility to Swift, which allocates the memory instead. This had some nice upsides, as we did not have to handle the complexity of witness tables like FFM mode does. > For more info on memory in FFM, I strongly recommend watching Konrad’s talk [try! Swift Tokyo 2025 \- Foreign Function and Memory APIs and Swift/Java interoperability](https://www.youtube.com/watch?v=vgtzhTOhEbs)