-
Notifications
You must be signed in to change notification settings - Fork 122
jextract: support typed unsafepointer/unsafebufferpointer in FFM and JNI generators #904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tatenda-k
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
Tatenda-k:jextract-pointer-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
Samples/SwiftJavaExtractFFMSampleApp/Sources/MySwiftLibrary/UnsafeBufferPointer.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift.org project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| private let int32BufferStorage: [Int32] = [10, 20, 30, 40] | ||
| private let emptyInt32BufferStorage: [Int32] = [] | ||
| private var mutableInt32BufferStorage: [Int32] = [5, 10, 15] | ||
|
|
||
| public func makeInt32Buffer() -> UnsafeBufferPointer<Int32> { | ||
| int32BufferStorage.withUnsafeBufferPointer { $0 } | ||
| } | ||
|
|
||
| public func makeEmptyInt32Buffer() -> UnsafeBufferPointer<Int32> { | ||
| emptyInt32BufferStorage.withUnsafeBufferPointer { $0 } | ||
| } | ||
|
|
||
| public func sumInt32Buffer(data: UnsafeBufferPointer<Int32>) -> Int64 { | ||
| data.reduce(0) { $0 + Int64($1) } | ||
| } | ||
|
|
||
| public func makeMutableInt32Buffer() -> UnsafeMutableBufferPointer<Int32> { | ||
| mutableInt32BufferStorage.withUnsafeMutableBufferPointer { $0 } | ||
| } | ||
|
|
||
| public func sumMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) -> Int64 { | ||
| data.reduce(0) { $0 + Int64($1) } | ||
| } | ||
|
|
||
| public func mutableInt32BufferElement(data: UnsafeMutableBufferPointer<Int32>, index: Int32) -> Int32 { | ||
| data[Int(index)] | ||
| } | ||
|
|
||
| public func incrementMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) { | ||
| for index in data.indices { | ||
| data[index] += 1 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/UnsafeBufferPointerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift.org project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| package com.example.swift; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.foreign.Arena; | ||
| import java.lang.foreign.MemorySegment; | ||
| import java.lang.foreign.ValueLayout; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class UnsafeBufferPointerTest { | ||
| @Test | ||
| void nullMemorySegment_isEmptyTypedBuffer() { | ||
| assertEquals(0, MySwiftLibrary.sumInt32Buffer(MemorySegment.NULL)); | ||
| } | ||
|
|
||
| @Test | ||
| void sumInt32Buffer() { | ||
| try (Arena arena = Arena.ofConfined()) { | ||
| MemorySegment input = arena.allocateFrom(ValueLayout.JAVA_INT, new int[] {10, 20, 30, 40}); | ||
|
|
||
| assertEquals(100, MySwiftLibrary.sumInt32Buffer(input)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void sumInt32Buffer_empty() { | ||
| try (Arena arena = Arena.ofConfined()) { | ||
| MemorySegment input = arena.allocate(0, ValueLayout.JAVA_INT.byteAlignment()); | ||
|
|
||
| assertEquals(0, MySwiftLibrary.sumInt32Buffer(input)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void returnInt32Buffer() { | ||
| MemorySegment result = MySwiftLibrary.makeInt32Buffer(); | ||
|
|
||
| assertEquals(16, result.byteSize()); | ||
| assertEquals(10, result.get(ValueLayout.JAVA_INT, 0)); | ||
| assertEquals(40, result.get(ValueLayout.JAVA_INT, 12)); | ||
| } | ||
|
|
||
| @Test | ||
| void returnEmptyInt32Buffer() { | ||
| assertEquals(0, MySwiftLibrary.makeEmptyInt32Buffer().byteSize()); | ||
| } | ||
|
|
||
| @Test | ||
| void returnMutableInt32Buffer() { | ||
| MemorySegment result = MySwiftLibrary.makeMutableInt32Buffer(); | ||
|
|
||
| assertEquals(12, result.byteSize()); | ||
| assertEquals(5, result.get(ValueLayout.JAVA_INT, 0)); | ||
| assertEquals(15, result.get(ValueLayout.JAVA_INT, 8)); | ||
| } | ||
|
|
||
| @Test | ||
| void mutableInt32Buffer_isInitialized() { | ||
| MemorySegment buffer = MySwiftLibrary.makeMutableInt32Buffer(); | ||
|
|
||
| assertEquals(5, buffer.get(ValueLayout.JAVA_INT, 0)); | ||
| assertEquals(10, buffer.get(ValueLayout.JAVA_INT, 4)); | ||
| assertEquals(15, buffer.get(ValueLayout.JAVA_INT, 8)); | ||
| } | ||
|
|
||
| @Test | ||
| void incrementMutableInt32Buffer_modifiesElements() { | ||
| try (Arena arena = Arena.ofConfined()) { | ||
| MemorySegment buffer = arena.allocateFrom(ValueLayout.JAVA_INT, new int[] {5, 10, 15}); | ||
|
|
||
| MySwiftLibrary.incrementMutableInt32Buffer(buffer); | ||
|
|
||
| assertEquals(6, buffer.get(ValueLayout.JAVA_INT, 0)); | ||
| assertEquals(11, buffer.get(ValueLayout.JAVA_INT, 4)); | ||
| assertEquals(16, buffer.get(ValueLayout.JAVA_INT, 8)); | ||
| } | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
...ftJavaExtractFFMSampleApp/src/test/java/com/example/swift/UnsafeRawBufferPointerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift.org project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| package com.example.swift; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.foreign.Arena; | ||
| import java.lang.foreign.MemorySegment; | ||
| import java.lang.foreign.ValueLayout; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class UnsafeRawBufferPointerTest { | ||
| @Test | ||
| void sumOfBytes() { | ||
| try (Arena arena = Arena.ofConfined()) { | ||
| MemorySegment input = arena.allocateFrom(ValueLayout.JAVA_BYTE, new byte[] {1, 2, 3, 4, 5}); | ||
|
|
||
| assertEquals(15, MySwiftLibrary.sumOfBytes(input)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void sumOfBytes_empty() { | ||
| try (Arena arena = Arena.ofConfined()) { | ||
| MemorySegment input = arena.allocate(0, 1); | ||
|
|
||
| assertEquals(0, MySwiftLibrary.sumOfBytes(input)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void bufferCount() { | ||
| try (Arena arena = Arena.ofConfined()) { | ||
| MemorySegment input = arena.allocateFrom(ValueLayout.JAVA_BYTE, new byte[] {10, 20, 30, 40}); | ||
|
|
||
| assertEquals(4, MySwiftLibrary.bufferCount(input)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void bufferCount_empty() { | ||
| try (Arena arena = Arena.ofConfined()) { | ||
| MemorySegment input = arena.allocate(0, 1); | ||
|
|
||
| assertEquals(0, MySwiftLibrary.bufferCount(input)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void returnRawBuffer() { | ||
| MemorySegment result = MySwiftLibrary.makeRawBuffer(); | ||
|
|
||
| assertEquals(4, result.byteSize()); | ||
| assertEquals(10, result.get(ValueLayout.JAVA_BYTE, 0)); | ||
| assertEquals(40, result.get(ValueLayout.JAVA_BYTE, 3)); | ||
| } | ||
|
|
||
| @Test | ||
| void returnEmptyRawBuffer() { | ||
| assertEquals(0, MySwiftLibrary.makeEmptyRawBuffer().byteSize()); | ||
| } | ||
|
|
||
| @Test | ||
| void returnMutableRawBuffer() { | ||
| MemorySegment result = MySwiftLibrary.makeMutableRawBuffer(); | ||
|
|
||
| assertEquals(3, result.byteSize()); | ||
| assertEquals(5, result.get(ValueLayout.JAVA_BYTE, 0)); | ||
| assertEquals(15, result.get(ValueLayout.JAVA_BYTE, 2)); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/UnsafeBufferPointer.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift.org project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftJava | ||
|
|
||
| private let int32BufferStorage: [Int32] = [10, 20, 30, 40] | ||
| private let emptyInt32BufferStorage: [Int32] = [] | ||
| private var mutableInt32BufferStorage: [Int32] = [5, 10, 15] | ||
|
|
||
| public func makeInt32Buffer() -> UnsafeBufferPointer<Int32> { | ||
| int32BufferStorage.withUnsafeBufferPointer { $0 } | ||
| } | ||
|
|
||
| public func makeEmptyInt32Buffer() -> UnsafeBufferPointer<Int32> { | ||
| emptyInt32BufferStorage.withUnsafeBufferPointer { $0 } | ||
| } | ||
|
|
||
| public func sumInt32Buffer(data: UnsafeBufferPointer<Int32>) -> Int64 { | ||
| data.reduce(0) { $0 + Int64($1) } | ||
| } | ||
|
|
||
| public func makeMutableInt32Buffer() -> UnsafeMutableBufferPointer<Int32> { | ||
| mutableInt32BufferStorage.withUnsafeMutableBufferPointer { $0 } | ||
| } | ||
|
|
||
| public func sumMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) -> Int64 { | ||
| data.reduce(0) { $0 + Int64($1) } | ||
| } | ||
|
|
||
| public func mutableInt32BufferElement(data: UnsafeMutableBufferPointer<Int32>, index: Int32) -> Int32 { | ||
| data[Int(index)] | ||
| } | ||
|
|
||
| public func incrementMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) { | ||
| for index in data.indices { | ||
| data[index] += 1 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/UnsafeBufferPointerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift.org project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| package com.example.swift; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.swift.swiftkit.core.SwiftUnsafeBufferPointer; | ||
| import org.swift.swiftkit.core.SwiftUnsafeMutableBufferPointer; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class UnsafeBufferPointerTest { | ||
| @Test | ||
| void returnInt32Buffer() { | ||
| SwiftUnsafeBufferPointer buffer = MySwiftLibrary.makeInt32Buffer(); | ||
|
|
||
| assertNotEquals(0, buffer.getBaseAddress()); | ||
| assertEquals(4, buffer.getCount()); | ||
| assertEquals(100, MySwiftLibrary.sumInt32Buffer(buffer)); | ||
| } | ||
|
|
||
| @Test | ||
| void returnEmptyInt32Buffer() { | ||
| SwiftUnsafeBufferPointer buffer = MySwiftLibrary.makeEmptyInt32Buffer(); | ||
| assertEquals(0, buffer.getBaseAddress()); | ||
| assertEquals(0, buffer.getCount()); | ||
| assertEquals(0, MySwiftLibrary.sumInt32Buffer(buffer)); | ||
| } | ||
|
|
||
| @Test | ||
| void returnMutableInt32Buffer() { | ||
| SwiftUnsafeMutableBufferPointer buffer = MySwiftLibrary.makeMutableInt32Buffer(); | ||
|
|
||
| assertNotEquals(0, buffer.getBaseAddress()); | ||
| assertEquals(3, buffer.getCount()); | ||
| assertEquals(30, MySwiftLibrary.sumMutableInt32Buffer(buffer)); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void mutableInt32Buffer_isInitialized() { | ||
| SwiftUnsafeMutableBufferPointer buffer = MySwiftLibrary.makeMutableInt32Buffer(); | ||
|
|
||
| assertEquals(5, MySwiftLibrary.mutableInt32BufferElement(buffer, 0)); | ||
| assertEquals(10, MySwiftLibrary.mutableInt32BufferElement(buffer, 1)); | ||
| assertEquals(15, MySwiftLibrary.mutableInt32BufferElement(buffer, 2)); | ||
| } | ||
|
|
||
| @Test | ||
| void incrementMutableInt32Buffer_modifiesElements() { | ||
| SwiftUnsafeMutableBufferPointer buffer = MySwiftLibrary.makeMutableInt32Buffer(); | ||
|
|
||
| MySwiftLibrary.incrementMutableInt32Buffer(buffer); | ||
|
|
||
| assertEquals(6, MySwiftLibrary.mutableInt32BufferElement(buffer, 0)); | ||
| assertEquals(11, MySwiftLibrary.mutableInt32BufferElement(buffer, 1)); | ||
| assertEquals(16, MySwiftLibrary.mutableInt32BufferElement(buffer, 2)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would break existing snippets