-
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 1 commit
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
File renamed without changes.
File renamed without changes.
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 |
|---|---|---|
|
|
@@ -56,8 +56,6 @@ extension JNISwift2JavaGenerator { | |
| self.expectedOutputSwiftFiles.remove(moduleFilename) | ||
| } | ||
|
|
||
| try self.writeJNICacheSource(&printer) | ||
|
|
||
| for (_, ty) in self.analysis.importedTypes.sorted(by: { (lhs, rhs) in lhs.key < rhs.key }) { | ||
| let fileNameBase = "\(ty.swiftNominal.qualifiedName)+SwiftJava" | ||
| let filename = "\(fileNameBase).swift" | ||
|
|
@@ -82,38 +80,13 @@ extension JNISwift2JavaGenerator { | |
| } | ||
| } | ||
|
|
||
| private func writeJNICacheSource(_ printer: inout CodePrinter) throws { | ||
| printer.print("import JavaKit") | ||
| printer.println() | ||
| printer.printBraceBlock("enum JNICaches") { printer in | ||
| let enumCases = self.analysis.importedTypes.values.filter { $0.swiftNominal.kind == .enum }.flatMap(\.cases) | ||
| for enumCase in enumCases { | ||
| printer.print("static var \(JNICaching.cacheName(for: enumCase)): _JNICache!") | ||
| } | ||
| printer.println() | ||
| printer.printBraceBlock("static func cleanup()") { printer in | ||
| for enumCase in enumCases { | ||
| printer.print("JNICaches.\(JNICaching.cacheName(for: enumCase)) = nil") | ||
| } | ||
| private func printJNICache(_ printer: inout CodePrinter, _ type: ImportedNominalType) { | ||
| printer.printBraceBlock("enum \(JNICaching.cacheName(for: type))") { printer in | ||
| for enumCase in type.cases { | ||
| guard let translatedCase = translatedEnumCase(for: enumCase) else { continue } | ||
| printer.print("static let \(JNICaching.cacheMemberName(for: enumCase)) = \(renderEnumCaseCacheInit(translatedCase))") | ||
| } | ||
| } | ||
|
|
||
| printer.println() | ||
| printer.print(#"@_cdecl("JNI_OnUnload")"#) | ||
| printer.printBraceBlock("func JNI_OnUnload(javaVM: UnsafeMutablePointer<JavaVM?>!, reserved: UnsafeMutableRawPointer!)") { printer in | ||
| printer.print("JNICaches.cleanup()") | ||
| } | ||
|
|
||
| let fileName = "\(self.swiftModuleName)+JNICaches.swift" | ||
|
|
||
| if let outputFile = try printer.writeContents( | ||
| outputDirectory: self.swiftOutputDirectory, | ||
| javaPackagePath: nil, | ||
| filename: fileName | ||
| ) { | ||
| print("[swift-java] Generated: \(fileName.bold) (at \(outputFile))") | ||
| self.expectedOutputSwiftFiles.remove(fileName) | ||
| } | ||
| } | ||
|
|
||
| private func printGlobalSwiftThunkSources(_ printer: inout CodePrinter) throws { | ||
|
|
@@ -133,6 +106,9 @@ extension JNISwift2JavaGenerator { | |
| private func printNominalTypeThunks(_ printer: inout CodePrinter, _ type: ImportedNominalType) throws { | ||
| printHeader(&printer) | ||
|
|
||
| printJNICache(&printer, type) | ||
|
Collaborator
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. 👍 |
||
| printer.println() | ||
|
|
||
| for initializer in type.initializers { | ||
| printSwiftFunctionThunk(&printer, initializer) | ||
| printer.println() | ||
|
|
@@ -192,30 +168,18 @@ extension JNISwift2JavaGenerator { | |
| printSwiftFunctionThunk(&printer, enumCase.caseFunction) | ||
| printer.println() | ||
|
|
||
| // Print enum case native init | ||
| printEnumNativeInit(&printer, translatedCase) | ||
|
|
||
| // Print getAsCase method | ||
| if !translatedCase.translatedValues.isEmpty { | ||
| printEnumGetAsCaseThunk(&printer, translatedCase) | ||
| } | ||
| } | ||
|
|
||
| private func printEnumNativeInit(_ printer: inout CodePrinter, _ enumCase: TranslatedEnumCase) { | ||
| printCDecl( | ||
| &printer, | ||
| javaMethodName: "$nativeInit", | ||
| parentName: "\(enumCase.original.enumType.nominalTypeDecl.name)$\(enumCase.name)$$JNI", | ||
| parameters: [], | ||
| resultType: .void | ||
| ) { printer in | ||
| // Setup caching | ||
| private func renderEnumCaseCacheInit(_ enumCase: TranslatedEnumCase) -> String { | ||
| let nativeParametersClassName = "\(javaPackagePath)/\(enumCase.enumName)$\(enumCase.name)$$NativeParameters" | ||
| let methodSignature = MethodSignature(resultType: .void, parameterTypes: enumCase.parameterConversions.map(\.native.javaType)) | ||
| let methods = #"[.init(name: "<init>", signature: "\#(methodSignature.mangledName)")]"# | ||
|
|
||
| printer.print(#"JNICaches.\#(JNICaching.cacheName(for: enumCase.original)) = _JNICache(environment: environment, className: "\#(nativeParametersClassName)", methods: \#(methods))"#) | ||
| } | ||
| return #"_JNIMethodIDCache(environment: try! JavaVirtualMachine.shared().environment(), className: "\#(nativeParametersClassName)", methods: \#(methods))"# | ||
| } | ||
|
|
||
| private func printEnumGetAsCaseThunk( | ||
|
|
@@ -237,9 +201,9 @@ extension JNISwift2JavaGenerator { | |
| guard case .\(enumCase.original.name)(\(caseNamesWithLet.joined(separator: ", "))) = \(selfPointer).pointee else { | ||
| fatalError("Expected enum case '\(enumCase.original.name)', but was '\\(\(selfPointer).pointee)'!") | ||
| } | ||
| let cache$ = JNICaches.\(JNICaching.cacheName(for: enumCase.original))! | ||
| let cache$ = \(JNICaching.cacheName(for: enumCase.original.enumType)).\(JNICaching.cacheMemberName(for: enumCase.original)) | ||
| let class$ = cache$.javaClass | ||
| let method$ = _JNICache.Method(name: "<init>", signature: "\(methodSignature.mangledName)") | ||
| let method$ = _JNIMethodIDCache.Method(name: "<init>", signature: "\(methodSignature.mangledName)") | ||
| let constructorID$ = cache$[method$] | ||
| """ | ||
| ) | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -72,7 +72,6 @@ package class JNISwift2JavaGenerator: Swift2JavaGenerator { | |
| return String(filePathPart.replacing(".swift", with: "+SwiftJava.swift")) | ||
| }) | ||
| self.expectedOutputSwiftFiles.insert("\(translator.swiftModuleName)Module+SwiftJava.swift") | ||
| self.expectedOutputSwiftFiles.insert("\(translator.swiftModuleName)+JNICaches.swift") | ||
|
||
|
|
||
| // FIXME: Can we avoid this? | ||
| self.expectedOutputSwiftFiles.insert("Data+SwiftJava.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
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.
Uh oh!
There was an error while loading. Please reload this page.