-
Notifications
You must be signed in to change notification settings - Fork 67
[JExtract/JNI] Support optionals in JNI mode #340
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
12 commits
Select commit
Hold shift + click to select a range
7cba57b
add support for optionals as parameters
madsodgaard dc619ae
add support for basic primitive types as optional returns
madsodgaard 04b2d9e
move tests
madsodgaard cea8b92
add support for primitive types that cannot be widened
madsodgaard 56a9096
add support for jextract classes
madsodgaard 615c671
Merge branch 'main' into jni-optional
madsodgaard a6c2fb9
small cleanups
madsodgaard 528cbab
PR feedback
madsodgaard ed42947
add support for optional JavaKit wrapped parameters
madsodgaard 760476c
update supported features
madsodgaard e087327
fix tests
madsodgaard df418b0
add codegen tests
madsodgaard 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
75 changes: 75 additions & 0 deletions
75
Samples/JExtractJNISampleApp/Sources/MySwiftLibrary/Optionals.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,75 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import JavaKit | ||
|
|
||
| public func optionalBool(input: Optional<Bool>) -> Bool? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalByte(input: Optional<Int8>) -> Int8? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalChar(input: Optional<UInt16>) -> UInt16? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalShort(input: Optional<Int16>) -> Int16? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalInt(input: Optional<Int32>) -> Int32? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalLong(input: Optional<Int64>) -> Int64? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalFloat(input: Optional<Float>) -> Float? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalDouble(input: Optional<Double>) -> Double? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalString(input: Optional<String>) -> String? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalClass(input: Optional<MySwiftClass>) -> MySwiftClass? { | ||
| return input | ||
| } | ||
|
|
||
| public func optionalJavaKitLong(input: Optional<JavaLong>) -> Int64? { | ||
| if let input { | ||
| return input.longValue() | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| public func multipleOptionals( | ||
| input1: Optional<Int8>, | ||
| input2: Optional<Int16>, | ||
| input3: Optional<Int32>, | ||
| input4: Optional<Int64>, | ||
| input5: Optional<String>, | ||
| input6: Optional<MySwiftClass>, | ||
| input7: Optional<Bool> | ||
| ) -> Int64? { | ||
| return 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
116 changes: 116 additions & 0 deletions
116
Samples/JExtractJNISampleApp/src/test/java/com/example/swift/OptionalsTest.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,116 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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.ConfinedSwiftMemorySession; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.OptionalDouble; | ||
| import java.util.OptionalInt; | ||
| import java.util.OptionalLong; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class OptionalsTest { | ||
| @Test | ||
| void optionalBool() { | ||
| assertEquals(Optional.empty(), MySwiftLibrary.optionalBool(Optional.empty())); | ||
| assertEquals(Optional.of(true), MySwiftLibrary.optionalBool(Optional.of(true))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalByte() { | ||
| assertEquals(Optional.empty(), MySwiftLibrary.optionalByte(Optional.empty())); | ||
| assertEquals(Optional.of((byte) 1) , MySwiftLibrary.optionalByte(Optional.of((byte) 1))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalChar() { | ||
| assertEquals(Optional.empty(), MySwiftLibrary.optionalChar(Optional.empty())); | ||
| assertEquals(Optional.of((char) 42), MySwiftLibrary.optionalChar(Optional.of((char) 42))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalShort() { | ||
| assertEquals(Optional.empty(), MySwiftLibrary.optionalShort(Optional.empty())); | ||
| assertEquals(Optional.of((short) -250), MySwiftLibrary.optionalShort(Optional.of((short) -250))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalInt() { | ||
| assertEquals(OptionalInt.empty(), MySwiftLibrary.optionalInt(OptionalInt.empty())); | ||
| assertEquals(OptionalInt.of(999), MySwiftLibrary.optionalInt(OptionalInt.of(999))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalLong() { | ||
| assertEquals(OptionalLong.empty(), MySwiftLibrary.optionalLong(OptionalLong.empty())); | ||
| assertEquals(OptionalLong.of(999), MySwiftLibrary.optionalLong(OptionalLong.of(999))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalFloat() { | ||
| assertEquals(Optional.empty(), MySwiftLibrary.optionalFloat(Optional.empty())); | ||
| assertEquals(Optional.of(3.14f), MySwiftLibrary.optionalFloat(Optional.of(3.14f))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalDouble() { | ||
| assertEquals(OptionalDouble.empty(), MySwiftLibrary.optionalDouble(OptionalDouble.empty())); | ||
| assertEquals(OptionalDouble.of(2.718), MySwiftLibrary.optionalDouble(OptionalDouble.of(2.718))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalString() { | ||
| assertEquals(Optional.empty(), MySwiftLibrary.optionalString(Optional.empty())); | ||
| assertEquals(Optional.of("Hello Swift!"), MySwiftLibrary.optionalString(Optional.of("Hello Swift!"))); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalClass() { | ||
| try (var arena = new ConfinedSwiftMemorySession()) { | ||
| MySwiftClass c = MySwiftClass.init(arena); | ||
| assertEquals(Optional.empty(), MySwiftLibrary.optionalClass(Optional.empty(), arena)); | ||
| Optional<MySwiftClass> optionalClass = MySwiftLibrary.optionalClass(Optional.of(c), arena); | ||
| assertTrue(optionalClass.isPresent()); | ||
| assertEquals(c.getX(), optionalClass.get().getX()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void optionalJavaKitLong() { | ||
| assertEquals(OptionalLong.empty(), MySwiftLibrary.optionalJavaKitLong(Optional.empty())); | ||
| assertEquals(OptionalLong.of(99L), MySwiftLibrary.optionalJavaKitLong(Optional.of(99L))); | ||
| } | ||
|
|
||
| @Test | ||
| void multipleOptionals() { | ||
| try (var arena = new ConfinedSwiftMemorySession()) { | ||
| MySwiftClass c = MySwiftClass.init(arena); | ||
| OptionalLong result = MySwiftLibrary.multipleOptionals( | ||
| Optional.of((byte) 1), | ||
| Optional.of((short) 42), | ||
| OptionalInt.of(50), | ||
| OptionalLong.of(1000L), | ||
| Optional.of("42"), | ||
| Optional.of(c), | ||
| Optional.of(true) | ||
| ); | ||
| assertEquals(result, OptionalLong.of(1L)); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.