|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +extension JNISwift2JavaGenerator { |
| 16 | + func writeSwiftThunkSources() throws { |
| 17 | + var printer = CodePrinter() |
| 18 | + try writeSwiftThunkSources(&printer) |
| 19 | + } |
| 20 | + |
| 21 | + package func writeSwiftExpectedEmptySources() throws { |
| 22 | + for expectedFileName in self.expectedOutputSwiftFiles { |
| 23 | + logger.trace("Write empty file: \(expectedFileName) ...") |
| 24 | + |
| 25 | + var printer = CodePrinter() |
| 26 | + printer.print("// Empty file generated on purpose") |
| 27 | + _ = try printer.writeContents( |
| 28 | + outputDirectory: self.swiftOutputDirectory, |
| 29 | + javaPackagePath: nil, |
| 30 | + filename: expectedFileName) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + package func writeSwiftThunkSources(_ printer: inout CodePrinter) throws { |
| 35 | + let moduleFilenameBase = "\(self.swiftModuleName)Module+SwiftJava" |
| 36 | + let moduleFilename = "\(moduleFilenameBase).swift" |
| 37 | + |
| 38 | + do { |
| 39 | + logger.trace("Printing swift module class: \(moduleFilename)") |
| 40 | + |
| 41 | + try printGlobalSwiftThunkSources(&printer) |
| 42 | + |
| 43 | + if let outputFile = try printer.writeContents( |
| 44 | + outputDirectory: self.swiftOutputDirectory, |
| 45 | + javaPackagePath: nil, |
| 46 | + filename: moduleFilename |
| 47 | + ) { |
| 48 | + print("[swift-java] Generated: \(moduleFilenameBase.bold).swift (at \(outputFile))") |
| 49 | + self.expectedOutputSwiftFiles.remove(moduleFilename) |
| 50 | + } |
| 51 | + |
| 52 | + for (_, ty) in self.analysis.importedTypes.sorted(by: { (lhs, rhs) in lhs.key < rhs.key }) { |
| 53 | + let fileNameBase = "\(ty.swiftNominal.qualifiedName)+SwiftJava" |
| 54 | + let filename = "\(fileNameBase).swift" |
| 55 | + logger.info("Printing contents: \(filename)") |
| 56 | + |
| 57 | + do { |
| 58 | + try printNominalTypeThunks(&printer, ty) |
| 59 | + |
| 60 | + if let outputFile = try printer.writeContents( |
| 61 | + outputDirectory: self.swiftOutputDirectory, |
| 62 | + javaPackagePath: nil, |
| 63 | + filename: filename) { |
| 64 | + print("[swift-java] Generated: \(fileNameBase.bold).swift (at \(outputFile))") |
| 65 | + self.expectedOutputSwiftFiles.remove(filename) |
| 66 | + } |
| 67 | + } catch { |
| 68 | + logger.warning("Failed to write to Swift thunks: \(filename)") |
| 69 | + } |
| 70 | + } |
| 71 | + } catch { |
| 72 | + logger.warning("Failed to write to Swift thunks: \(moduleFilename)") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private func printGlobalSwiftThunkSources(_ printer: inout CodePrinter) throws { |
| 77 | + printHeader(&printer) |
| 78 | + |
| 79 | + for decl in analysis.importedGlobalFuncs { |
| 80 | + printSwiftFunctionThunk(&printer, decl) |
| 81 | + printer.println() |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private func printNominalTypeThunks(_ printer: inout CodePrinter, _ type: ImportedNominalType) throws { |
| 86 | + printHeader(&printer) |
| 87 | + |
| 88 | + for decl in type.methods { |
| 89 | + printSwiftFunctionThunk(&printer, decl, sorroundingType: type) |
| 90 | + printer.println() |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private func printSwiftFunctionThunk( |
| 95 | + _ printer: inout CodePrinter, |
| 96 | + _ decl: ImportedFunc, |
| 97 | + sorroundingType: ImportedNominalType? = nil |
| 98 | + ) { |
| 99 | + let parentName = sorroundingType?.swiftNominal.qualifiedName ?? swiftModuleName |
| 100 | + |
| 101 | + let cName = |
| 102 | + "Java_" + self.javaPackage.replacingOccurrences(of: ".", with: "_") + "_\(parentName)_" |
| 103 | + + decl.name |
| 104 | + let thunkName = thunkNameRegistry.functionThunkName(decl: decl) |
| 105 | + let translatedParameters = decl.functionSignature.parameters.enumerated().map { idx, param in |
| 106 | + (param.parameterName ?? "arg\(idx)", param.type.javaType) |
| 107 | + } |
| 108 | + |
| 109 | + let thunkParameters = |
| 110 | + [ |
| 111 | + "environment: UnsafeMutablePointer<JNIEnv?>!", |
| 112 | + "thisClass: jclass", |
| 113 | + ] + translatedParameters.map { "\($0.0): \($0.1.jniTypeName)" } |
| 114 | + let swiftReturnType = decl.functionSignature.result.type |
| 115 | + let thunkReturnType = |
| 116 | + !swiftReturnType.isVoid ? " -> \(swiftReturnType.javaType.jniTypeName)" : "" |
| 117 | + |
| 118 | + printer.printBraceBlock( |
| 119 | + """ |
| 120 | + @_cdecl("\(cName)") |
| 121 | + func \(thunkName)(\(thunkParameters.joined(separator: ", ")))\(thunkReturnType) |
| 122 | + """ |
| 123 | + ) { printer in |
| 124 | + let downcallParameters = zip(decl.functionSignature.parameters, translatedParameters).map { |
| 125 | + originalParam, translatedParam in |
| 126 | + let label = originalParam.argumentLabel.map { "\($0): " } ?? "" |
| 127 | + return "\(label)\(originalParam.type)(fromJNI: \(translatedParam.0), in: environment!)" |
| 128 | + } |
| 129 | + let tryClause: String = decl.isThrowing ? "try " : "" |
| 130 | + let functionDowncall = |
| 131 | + "\(tryClause)\(parentName).\(decl.name)(\(downcallParameters.joined(separator: ", ")))" |
| 132 | + |
| 133 | + let innerBody = |
| 134 | + if swiftReturnType.isVoid { |
| 135 | + functionDowncall |
| 136 | + } else { |
| 137 | + """ |
| 138 | + let result = \(functionDowncall) |
| 139 | + return result.getJNIValue(in: environment) |
| 140 | + """ |
| 141 | + } |
| 142 | + |
| 143 | + if decl.isThrowing { |
| 144 | + let dummyReturn = |
| 145 | + !swiftReturnType.isVoid ? "return \(swiftReturnType).jniPlaceholderValue" : "" |
| 146 | + printer.print( |
| 147 | + """ |
| 148 | + do { |
| 149 | + \(innerBody) |
| 150 | + } catch { |
| 151 | + environment.throwAsException(error) |
| 152 | + \(dummyReturn) |
| 153 | + } |
| 154 | + """ |
| 155 | + ) |
| 156 | + } else { |
| 157 | + printer.print(innerBody) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private func printHeader(_ printer: inout CodePrinter) { |
| 163 | + printer.print( |
| 164 | + """ |
| 165 | + // Generated by swift-java |
| 166 | +
|
| 167 | + import JavaKit |
| 168 | +
|
| 169 | + """ |
| 170 | + ) |
| 171 | + } |
| 172 | +} |
0 commit comments