-
Notifications
You must be signed in to change notification settings - Fork 70
jextract/ffm: Support [UInt8] parameters #477
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2e1c8d8
samples: move more tests from sample app to test files
ktoso 7ebccdd
add another test that uses MemorySegment to pass an array copy to Swi…
ktoso b7c8b2e
jextract/ffm: support [UInt8] parameters, although it causes copying
ktoso 91ff2c5
add source gen test
ktoso 9fcc003
jextract/ffm: make sure uint arrays are marked @unsigned
ktoso 772a6ae
Merge branch 'main' into wip-support-array-uint8-ffm
ktoso e44364a
undo the _ -> $ change, we'll do it separately
ktoso 67154d5
snapshot, new version of swift side for returning array
ktoso 6716f4e
almost getting the upcall passed
ktoso da42a87
Done implementing returning byte[] in ffm mode
ktoso 6df1b41
cleanups
ktoso a4fd03f
final boilerplate additions for ffm array returns
ktoso 0a94a34
Update Sources/JExtractSwiftLib/FFM/ConversionStep.swift
ktoso 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
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| .sdkmanrc | ||
|
|
||
| .DS_Store | ||
| .metals | ||
| .build | ||
| .idea | ||
| .vscode | ||
|
|
||
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
67 changes: 67 additions & 0 deletions
67
Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/FFMArraysTest.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,67 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 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.*; | ||
| import org.swift.swiftkit.ffm.*; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.lang.foreign.ValueLayout; | ||
| import java.util.Arrays; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class FFMArraysTest { | ||
|
|
||
| @Test | ||
| void test_sumAllByteArrayElements_throughMemorySegment() { | ||
| byte[] bytes = new byte[124]; | ||
| Arrays.fill(bytes, (byte) 1); | ||
|
|
||
| try (var arena = AllocatingSwiftArena.ofConfined()) { | ||
| // NOTE: We cannot use MemorySegment.ofArray because that creates a HEAP backed segment and therefore cannot pass into native: | ||
| // java.lang.IllegalArgumentException: Heap segment not allowed: MemorySegment{ kind: heap, heapBase: [B@5b6ec132, address: 0x0, byteSize: 124 } | ||
| // MemorySegment bytesSegment = MemorySegment.ofArray(bytes); // NO COPY (!) | ||
| // MySwiftLibrary.sumAllByteArrayElements(bytesSegment, bytes.length); | ||
|
|
||
| var bytesCopy = arena.allocateFrom(ValueLayout.JAVA_BYTE, bytes); | ||
| var swiftSideSum = MySwiftLibrary.sumAllByteArrayElements(bytesCopy, bytes.length); | ||
|
|
||
| int javaSideSum = IntStream.range(0, bytes.length).map(i -> bytes[i]).sum(); | ||
| assertEquals(javaSideSum, swiftSideSum); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void test_sumAllByteArrayElements_arrayCopy() { | ||
| byte[] bytes = new byte[124]; | ||
| Arrays.fill(bytes, (byte) 1); | ||
|
|
||
| var swiftSideSum = MySwiftLibrary.sumAllByteArrayElements(bytes); | ||
|
|
||
| int javaSideSum = IntStream.range(0, bytes.length).map(i -> bytes[i]).sum(); | ||
| assertEquals(javaSideSum, swiftSideSum); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getArray() { | ||
| AtomicLong bufferSize = new AtomicLong(); | ||
| byte[] javaBytes = MySwiftLibrary.getArray(); // automatically converted [UInt8] to byte[] | ||
|
|
||
| assertArrayEquals(new byte[]{1, 2, 3}, javaBytes); | ||
| } | ||
| } |
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
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
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
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
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
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
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.
We have naming inconsistency; these should be $ and not _ but seems we got this inconsistency already from the Data work -- lets fix it separately, since this made the PR kinda big