-
Notifications
You must be signed in to change notification settings - Fork 67
[JExtract/JNI] Import enums #346
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 18 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
31b9712
import enum cases
madsodgaard 9b53e69
add support for associated values
madsodgaard 0d0af8b
add support for failable initializers
madsodgaard ffcab93
synthesize `RawRepresentable`
madsodgaard e74299e
rename tests
madsodgaard 5efd8c5
add simple support for getting associated values
madsodgaard 8985c10
add `getCase()` and `getDiscriminator()`
madsodgaard cd65619
print discriminator native
madsodgaard 4529f3e
support objects as associated values
madsodgaard 2770318
fix support for optional associated values
madsodgaard e492e45
add caching
madsodgaard 485ea4c
cleanup cache on jni unload
madsodgaard fea7909
fix multiple optionals
madsodgaard 9543f18
Merge branch 'main' into jni-enums
madsodgaard 7bd0ef6
fix tests and decl documentation
madsodgaard d75c201
fix benchmark
madsodgaard dc2a6a6
add docs
madsodgaard 89825ad
correct docs header
madsodgaard 7f40d0e
PR feedback
madsodgaard d3eec75
Merge branch 'main' into jni-enums
madsodgaard 5955c98
rename enum files back
madsodgaard a6ff6e1
fix existing tests
madsodgaard e32edf3
add source gen tests
madsodgaard 5ba7195
Merge branch 'main' into jni-enums
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
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
18 changes: 18 additions & 0 deletions
18
Samples/JExtractJNISampleApp/Sources/MySwiftLibrary/Alignment.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,18 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| public enum Alignment: String { | ||
| case horizontal | ||
| case vertical | ||
| } | ||
madsodgaard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
63 changes: 63 additions & 0 deletions
63
Samples/JExtractJNISampleApp/Sources/MySwiftLibrary/Vehicle.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,63 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| public enum Vehicle { | ||
| case bicycle | ||
| case car(String, trailer: String?) | ||
| case motorbike(String, horsePower: Int64, helmets: Int32?) | ||
| indirect case transformer(front: Vehicle, back: Vehicle) | ||
ktoso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case boat(passengers: Int32?, length: Int16?) | ||
|
|
||
| public init?(name: String) { | ||
| switch name { | ||
| case "bicycle": self = .bicycle | ||
| case "car": self = .car("Unknown", trailer: nil) | ||
| case "motorbike": self = .motorbike("Unknown", horsePower: 0, helmets: nil) | ||
| case "boat": self = .boat(passengers: nil, length: nil) | ||
| default: return nil | ||
| } | ||
| } | ||
|
|
||
| public var name: String { | ||
| switch self { | ||
| case .bicycle: "bicycle" | ||
| case .car: "car" | ||
| case .motorbike: "motorbike" | ||
| case .transformer: "transformer" | ||
| case .boat: "boat" | ||
| } | ||
| } | ||
|
|
||
| public func isFasterThan(other: Vehicle) -> Bool { | ||
| switch (self, other) { | ||
| case (.bicycle, .bicycle), (.bicycle, .car), (.bicycle, .motorbike), (.bicycle, .transformer): false | ||
| case (.car, .bicycle): true | ||
| case (.car, .motorbike), (.car, .transformer), (.car, .car): false | ||
| case (.motorbike, .bicycle), (.motorbike, .car): true | ||
| case (.motorbike, .motorbike), (.motorbike, .transformer): false | ||
| case (.transformer, .bicycle), (.transformer, .car), (.transformer, .motorbike): true | ||
| case (.transformer, .transformer): false | ||
| default: false | ||
| } | ||
| } | ||
|
|
||
| public mutating func upgrade() { | ||
| switch self { | ||
| case .bicycle: self = .car("Unknown", trailer: nil) | ||
| case .car: self = .motorbike("Unknown", horsePower: 0, helmets: nil) | ||
| case .motorbike: self = .transformer(front: .car("BMW", trailer: nil), back: self) | ||
| case .transformer, .boat: break | ||
| } | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
Samples/JExtractJNISampleApp/Sources/MySwiftLibrary/swift-java.config
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "javaPackage": "com.example.swift", | ||
| "mode": "jni", | ||
| "logLevel": ["debug"] | ||
ktoso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
60 changes: 60 additions & 0 deletions
60
Samples/JExtractJNISampleApp/src/jmh/java/com/example/swift/EnumBenchmark.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,60 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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.openjdk.jmh.annotations.*; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
| import org.swift.swiftkit.core.ClosableSwiftArena; | ||
| import org.swift.swiftkit.core.ConfinedSwiftMemorySession; | ||
| import org.swift.swiftkit.core.SwiftArena; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.OptionalInt; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @BenchmarkMode(Mode.AverageTime) | ||
| @Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) | ||
| @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| @Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" }) | ||
| public class EnumBenchmark { | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class BenchmarkState { | ||
| ClosableSwiftArena arena; | ||
| Vehicle vehicle; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void beforeAll() { | ||
| arena = SwiftArena.ofConfined(); | ||
| vehicle = Vehicle.motorbike("Yamaha", 900, OptionalInt.empty(), arena); | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void afterAll() { | ||
| arena.close(); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Vehicle.Motorbike getAssociatedValues(BenchmarkState state, Blackhole bh) { | ||
| Vehicle.Motorbike motorbike = state.vehicle.getAsMotorbike().orElseThrow(); | ||
| bh.consume(motorbike.arg0()); | ||
| bh.consume(motorbike.horsePower()); | ||
| return motorbike; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
Samples/JExtractJNISampleApp/src/test/java/com/example/swift/AlignmentEnumTest.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,41 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2024 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 org.swift.swiftkit.core.SwiftArena; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class AlignmentEnumTest { | ||
| @Test | ||
| void rawValue() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| Optional<Alignment> invalid = Alignment.init("invalid", arena); | ||
| assertFalse(invalid.isPresent()); | ||
|
|
||
| Optional<Alignment> horizontal = Alignment.init("horizontal", arena); | ||
| assertTrue(horizontal.isPresent()); | ||
| assertEquals("horizontal", horizontal.get().getRawValue()); | ||
|
|
||
| Optional<Alignment> vertical = Alignment.init("vertical", arena); | ||
| assertTrue(vertical.isPresent()); | ||
| assertEquals("vertical", vertical.get().getRawValue()); | ||
| } | ||
| } | ||
| } |
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.