-
Notifications
You must be signed in to change notification settings - Fork 100
jextract/jni: Support non-JavaBoxable types in Dictionary #754
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
sidepelican
wants to merge
27
commits into
swiftlang:main
Choose a base branch
from
sidepelican:collection_boxable
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
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6e02a68
initial implementation
sidepelican 090e4fa
Add support generic types
sidepelican 98ce301
Skip to print method cache for Core classes
sidepelican 72b4808
Remove JavaBoxable extension generations for collection types
sidepelican d31aa69
Update examples
sidepelican c744d2a
Fix optional boxing
sidepelican 381586e
Skip optional supporting
sidepelican 9d2ff60
Nested array support
sidepelican ae54814
Remove array support
sidepelican 09cedb6
slim test code
sidepelican 583b25b
Merge remote-tracking branch 'origin/main' into collection_boxable
sidepelican 8f8b1b4
Add javaBoxClass support
sidepelican e784100
Merge remote-tracking branch 'origin/main' into collection_boxable
sidepelican 37e19f9
Use default arena explicitly
sidepelican 5cdfc4f
print JavaBoxable extension for all types
sidepelican e2790d2
Use bridge type strategy
sidepelican 153fd1d
Simplified Implementation
sidepelican dccc874
Restore removal of JavaValue from JavaBoxable
sidepelican 81b1a21
rename bridge types
sidepelican 163193d
Fix tests
sidepelican f33c2bf
Add Array and Optional support
sidepelican 407886d
Add intArrayDictionary test
sidepelican 85d8658
formatting
sidepelican 6c421e2
Add documentation comment and error details
sidepelican 9b61ec0
Fix build error in LinkageTest
sidepelican c5cdc0a
Fix generic clause join operation
sidepelican 02ea80e
Pickup generic where clause
sidepelican 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
97 changes: 97 additions & 0 deletions
97
Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/CollectionBoxable.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,97 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if canImport(FoundationEssentials) | ||
| import FoundationEssentials | ||
| #else | ||
| import Foundation | ||
| #endif | ||
|
|
||
| public func makeIntToFishDictionary() -> [Int: Fish] { | ||
| [ | ||
| 1: Fish(name: "salmon"), | ||
| 2: Fish(name: "clownfish"), | ||
| ] | ||
| } | ||
|
|
||
| public func intToFishDictionary(dict: [Int: Fish]) -> [Int: Fish] { | ||
| dict | ||
| } | ||
|
|
||
| public func makeFishSet() -> Set<Fish> { | ||
| [ | ||
| Fish(name: "salmon"), | ||
| Fish(name: "clownfish"), | ||
| ] | ||
| } | ||
|
|
||
| public func fishSet(set: Set<Fish>) -> Set<Fish> { | ||
| set | ||
| } | ||
|
|
||
| public func makeMyIDToFish() -> [MyID<Int>: Fish] { | ||
| [ | ||
| .init(0): Fish(name: "salmon"), | ||
| .init(1): Fish(name: "clownfish"), | ||
| ] | ||
| } | ||
|
|
||
| public func makeSpecializedGenericTypeSet() -> Set<FishBox> { | ||
| [.init(count: 2), .init(count: 3)] | ||
| } | ||
|
|
||
| public func makeSetInDictionary() -> [String: Set<Int32>] { | ||
| [ | ||
| "even": [0, 2, 4], | ||
| "odd": [1, 3, 5], | ||
| ] | ||
| } | ||
|
|
||
| public func makeIntArrayDictionary() -> [String: [Int32]] { | ||
| [ | ||
| "even": [0, 2, 4], | ||
| "odd": [1, 3, 5], | ||
| ] | ||
| } | ||
|
|
||
| public func intArrayDictionary(dict: [String: [Int32]]) -> [String: [Int32]] { | ||
| dict | ||
| } | ||
|
|
||
| public func makeFishArrayDictionary() -> [String: [Fish]] { | ||
| [ | ||
| "reef": [ | ||
| Fish(name: "clownfish"), | ||
| Fish(name: "blue tang"), | ||
| ], | ||
| "river": [ | ||
| Fish(name: "salmon") | ||
| ], | ||
| ] | ||
| } | ||
|
|
||
| public func fishArrayDictionary(dict: [String: [Fish]]) -> [String: [Fish]] { | ||
| dict | ||
| } | ||
|
|
||
| public func makeOptionalFishDictionary() -> [String: Fish?] { | ||
| [ | ||
| "reef": Fish(name: "clownfish"), | ||
| "empty": nil, | ||
| ] | ||
| } | ||
|
|
||
| public func optionalFishDictionary(dict: [String: Fish?]) -> [String: Fish?] { | ||
| dict | ||
| } | ||
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
153 changes: 153 additions & 0 deletions
153
...s/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/CollectionBoxableTest.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,153 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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.SwiftArena; | ||
| import org.swift.swiftkit.core.collections.SwiftDictionaryMap; | ||
| import org.swift.swiftkit.core.collections.SwiftSet; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class CollectionBoxableTest { | ||
| @Test | ||
| void intToFishDictionaryRoundtrip() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftDictionaryMap<Long, Fish> original = MySwiftLibrary.makeIntToFishDictionary(arena); | ||
| assertEquals(2, original.size()); | ||
| assertEquals("salmon", original.get(1L).getName()); | ||
| assertEquals("clownfish", original.get(2L).getName()); | ||
|
|
||
| SwiftDictionaryMap<Long, Fish> roundtripped = MySwiftLibrary.intToFishDictionary(original, arena); | ||
| assertEquals(2, roundtripped.size()); | ||
| assertEquals("salmon", roundtripped.get(1L).getName()); | ||
| assertEquals("clownfish", roundtripped.get(2L).getName()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void fishSetRoundtrip() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftSet<Fish> original = MySwiftLibrary.makeFishSet(arena); | ||
| assertEquals(2, original.size()); | ||
| assertTrue(original.contains(Fish.init("salmon", arena))); | ||
| assertTrue(original.contains(Fish.init("clownfish", arena))); | ||
|
|
||
| SwiftSet<Fish> roundtripped = MySwiftLibrary.fishSet(original, arena); | ||
| assertEquals(2, roundtripped.size()); | ||
| assertTrue(roundtripped.contains(Fish.init("salmon", arena))); | ||
| assertTrue(roundtripped.contains(Fish.init("clownfish", arena))); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void makeMyIDToFish() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftDictionaryMap<MyID<Long>, Fish> dict = MySwiftLibrary.makeMyIDToFish(arena); | ||
| assertEquals(2, dict.size()); | ||
|
|
||
| MyID<Long> salmonId = MyIDs.makeIntID(0, arena); | ||
| MyID<Long> clownfishId = MyIDs.makeIntID(1, arena); | ||
| MyID<Long> unknownId = MyIDs.makeIntID(-100, arena); | ||
|
|
||
| assertTrue(dict.containsKey(salmonId)); | ||
| assertTrue(dict.containsKey(clownfishId)); | ||
| assertFalse(dict.containsKey(unknownId)); | ||
| assertEquals("salmon", dict.get(salmonId).getName()); | ||
| assertEquals("clownfish", dict.get(clownfishId).getName()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void makeSpecializedGenericTypeSet() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftSet<Box<Fish>> set = MySwiftLibrary.makeSpecializedGenericTypeSet(arena); | ||
|
|
||
| assertEquals( | ||
| set.stream() | ||
| .map(Box::getCount) | ||
| .collect(Collectors.toSet()), | ||
| Set.of(2L, 3L) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void makeSetInDictionary() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftDictionaryMap<String, SwiftSet<Integer>> dict = MySwiftLibrary.makeSetInDictionary(arena); | ||
| assertEquals(Set.of(0, 2, 4), dict.get("even").toJavaSet()); | ||
| assertNull(dict.get("unknown")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void intArrayDictionaryRoundtrip() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftDictionaryMap<String, Integer[]> original = MySwiftLibrary.makeIntArrayDictionary(arena); | ||
| assertArrayEquals(new Integer[] {0, 2, 4}, original.get("even")); | ||
|
|
||
| SwiftDictionaryMap<String, Integer[]> roundtripped = MySwiftLibrary.intArrayDictionary(original, arena); | ||
| assertArrayEquals(new Integer[] {0, 2, 4}, roundtripped.get("even")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void fishArrayDictionaryRoundtrip() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftDictionaryMap<String, Fish[]> original = MySwiftLibrary.makeFishArrayDictionary(arena); | ||
| assertArrayEquals(new String[] {"clownfish", "blue tang"}, fishNames(original.get("reef"))); | ||
| assertArrayEquals(new String[] {"salmon"}, fishNames(original.get("river"))); | ||
|
|
||
| SwiftDictionaryMap<String, Fish[]> roundtripped = MySwiftLibrary.fishArrayDictionary(original, arena); | ||
| assertArrayEquals(new String[] {"clownfish", "blue tang"}, fishNames(roundtripped.get("reef"))); | ||
| assertArrayEquals(new String[] {"salmon"}, fishNames(roundtripped.get("river"))); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void optionalFishDictionaryRoundtrip() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| SwiftDictionaryMap<String, Optional<Fish>> original = MySwiftLibrary.makeOptionalFishDictionary(arena); | ||
| assertDoesNotThrow(() -> { | ||
| var value = original.get("reef").orElseThrow(); | ||
| assertEquals("clownfish", value.getName()); | ||
| }); | ||
| assertDoesNotThrow(() -> { | ||
| assertTrue(original.get("empty").isEmpty()); | ||
| }); | ||
|
|
||
| SwiftDictionaryMap<String, Optional<Fish>> roundtripped = MySwiftLibrary.optionalFishDictionary(original, arena); | ||
| assertDoesNotThrow(() -> { | ||
| var value = roundtripped.get("reef").orElseThrow(); | ||
| assertEquals("clownfish", value.getName()); | ||
| }); | ||
| assertDoesNotThrow(() -> { | ||
| assertTrue(roundtripped.get("empty").isEmpty()); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private static String[] fishNames(Fish[] fish) { | ||
| return Arrays.stream(fish) | ||
| .map(Fish::getName) | ||
| .toArray(String[]::new); | ||
| } | ||
| } |
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.
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.
Fantastic, thank you :)