-
Notifications
You must be signed in to change notification settings - Fork 100
jextract: Move Foundation support into a common package with code generation #687
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
20
commits into
swiftlang:main
Choose a base branch
from
sidepelican:swiftkit_foundation_codegen
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
20 commits
Select commit
Hold shift + click to select a range
99d5227
Generate foundation types in SwiftKitCore
sidepelican cbd55ff
Add Date and Date wrapper by hand
sidepelican ce6ec4c
implement without macro for jbytearray returning functions
sidepelican cc176f8
Remove generate script
sidepelican 60070cf
Merge remote-tracking branch 'origin/main' into swiftkit_foundation
sidepelican ae82915
Fix ffm java generated code
sidepelican c42156f
native implementation and fix test import
sidepelican afae76e
Fix test cases
sidepelican e09a5d1
Merge remote-tracking branch 'origin/main' into swiftkit_foundation
sidepelican 9a6f3ed
Fix benchmark code imports
sidepelican d954765
swift format
sidepelican f377d1c
simple collapling
sidepelican ab3e202
Add JavaType.optional utility function
sidepelican ead284b
Merge branch 'swiftkit_foundation' into swiftkit_foundation_codegen
sidepelican c4eaf38
Make --single-type available in JNI
sidepelican a040322
Reuse the generation script
sidepelican 8ee8976
Update generator logic for internal module
sidepelican baecaf2
add public accessor
sidepelican ee1ce47
fix stripping import target module name
sidepelican ac26e21
Update .licenseignore
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 struct Data: DataProtocol { | ||
| public init(bytes: UnsafeRawPointer, count: Int) | ||
| public init(_ bytes: [UInt8]) | ||
| public var count: Int { get } | ||
| public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) -> Void) | ||
| } |
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,21 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 protocol DataProtocol {} |
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,27 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 struct Date { | ||
| /// The interval between the date object and 00:00:00 UTC on 1 January 1970. | ||
| public var timeIntervalSince1970: Double { get } | ||
|
|
||
| /// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds. | ||
| public init(timeIntervalSince1970: Double) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,7 @@ extension FFMSwift2JavaGenerator { | |
| // === All types | ||
| // We have to write all types to their corresponding output file that matches the file they were declared in, | ||
| // because otherwise SwiftPM plugins will not pick up files apropriately -- we expect 1 output +SwiftJava.swift file for every input. | ||
|
|
||
| let filteredTypes: [String: ImportedNominalType] | ||
| if let singleType = config.singleType { | ||
| filteredTypes = self.analysis.importedTypes.filter { $0.key == singleType } | ||
|
|
@@ -92,14 +93,13 @@ extension FFMSwift2JavaGenerator { | |
| let filename = "\(inputFileName)".replacing(".swift", with: "+SwiftJava.swift") | ||
|
|
||
| // Print file header before all type thunks | ||
| printer.print( | ||
| """ | ||
| // Generated by swift-java | ||
|
|
||
| import SwiftRuntimeFunctions | ||
| printer.print("// Generated by swift-java") | ||
| printer.println() | ||
| if !group.value.allSatisfy({ $0.value.swiftNominal.moduleName == "SwiftRuntimeFunctions" }) { | ||
| printer.print("import SwiftRuntimeFunctions") | ||
| } | ||
| printer.println() | ||
|
|
||
| """ | ||
| ) | ||
| self.lookupContext.symbolTable.printImportedModules(&printer) | ||
|
|
||
| for ty in importedTypesForThisFile { | ||
|
|
@@ -263,16 +263,15 @@ struct SwiftThunkTranslator { | |
|
|
||
| /// Render special thunks for known types like Foundation.Data | ||
| func renderSpecificTypeThunks(_ nominal: ImportedNominalType) -> [DeclSyntax] { | ||
| guard let knownType = nominal.swiftNominal.knownTypeKind else { | ||
| return [] | ||
| } | ||
|
|
||
| switch knownType { | ||
| case .foundationData, .essentialsData: | ||
| return renderFoundationDataThunks(nominal) | ||
| default: | ||
| return [] | ||
| if nominal.swiftNominal.moduleName == "SwiftRuntimeFunctions" { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Foundation types are now imported using the swift-java package name. This is the concern I raised in #684 (comment) |
||
| switch nominal.swiftNominal.qualifiedName { | ||
| case "Data": | ||
| return renderFoundationDataThunks(nominal) | ||
| default: | ||
| break | ||
| } | ||
| } | ||
| return [] | ||
| } | ||
|
|
||
| /// Render Swift thunks for Foundation.Data helper methods | ||
|
|
||
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.
A workaround to ensure the generated code doesn't import its own package