Skip to content

Commit 2a92246

Browse files
[NFC] BridgeJS: Use consistent codegen helper for @_extern decl
1 parent 8bbf832 commit 2a92246

File tree

5 files changed

+418
-71
lines changed

5 files changed

+418
-71
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,16 @@ public struct ClosureCodegen {
4646

4747
// Generate extern declaration using CallJSEmission
4848
let externDecl = builder.renderImportDecl()
49-
50-
let makeClosureExternDecl: DeclSyntax = """
51-
#if arch(wasm32)
52-
@_extern(wasm, module: "bjs", name: "make_swift_closure_\(raw: signature.moduleName)_\(raw: signature.mangleName)")
53-
fileprivate func make_swift_closure_\(raw: signature.moduleName)_\(raw: signature.mangleName)(_ boxPtr: UnsafeMutableRawPointer, _ file: UnsafePointer<UInt8>, _ line: UInt32) -> Int32
54-
#else
55-
fileprivate func make_swift_closure_\(raw: signature.moduleName)_\(raw: signature.mangleName)(_ boxPtr: UnsafeMutableRawPointer, _ file: UnsafePointer<UInt8>, _ line: UInt32) -> Int32 {
56-
fatalError("Only available on WebAssembly")
57-
}
58-
#endif
59-
"""
49+
let externABIName = "make_swift_closure_\(signature.moduleName)_\(signature.mangleName)"
50+
let externDeclPrinter = CodeFragmentPrinter()
51+
SwiftCodePattern.buildExternFunctionDecl(
52+
printer: externDeclPrinter,
53+
moduleName: "bjs",
54+
abiName: externABIName,
55+
functionName: externABIName,
56+
signature: "(_ boxPtr: UnsafeMutableRawPointer, _ file: UnsafePointer<UInt8>, _ line: UInt32) -> Int32"
57+
)
58+
let makeClosureExternDecl: DeclSyntax = "\(raw: externDeclPrinter.lines.joined(separator: "\n"))"
6059

6160
let helperEnumDeclPrinter = CodeFragmentPrinter()
6261
helperEnumDeclPrinter.write("private enum \(helperName) {")
@@ -98,7 +97,7 @@ public struct ClosureCodegen {
9897
extension JSTypedClosure where Signature == \(raw: swiftClosureType) {
9998
init(fileID: StaticString = #fileID, line: UInt32 = #line, _ body: @escaping \(raw: swiftClosureType)) {
10099
self.init(
101-
makeClosure: make_swift_closure_\(raw: signature.moduleName)_\(raw: signature.mangleName),
100+
makeClosure: \(raw: externABIName),
102101
body: body,
103102
fileID: fileID,
104103
line: line

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -701,26 +701,12 @@ public class ExportSwift {
701701
)
702702

703703
let externDeclPrinter = CodeFragmentPrinter()
704-
SwiftCodePattern.buildWasmConditionalCompilationDecls(
704+
SwiftCodePattern.buildExternFunctionDecl(
705705
printer: externDeclPrinter,
706-
wasmDecl: { printer in
707-
SwiftCodePattern.buildExternFunctionDecl(
708-
printer: printer,
709-
moduleName: moduleName,
710-
abiName: externFunctionName,
711-
functionName: wrapFunctionName,
712-
signature: funcSignature
713-
)
714-
},
715-
elseDecl: { printer in
716-
printer.write(
717-
multilineString: """
718-
fileprivate func \(wrapFunctionName)\(funcSignature) {
719-
fatalError("Only available on WebAssembly")
720-
}
721-
"""
722-
)
723-
}
706+
moduleName: moduleName,
707+
abiName: externFunctionName,
708+
functionName: wrapFunctionName,
709+
signature: funcSignature
724710
)
725711
let externDecl: DeclSyntax = "\(raw: externDeclPrinter.lines.joined(separator: "\n"))"
726712
return [extensionDecl, externDecl]
@@ -1297,26 +1283,12 @@ struct StructCodegen {
12971283
functionName: String,
12981284
signature: String
12991285
) {
1300-
SwiftCodePattern.buildWasmConditionalCompilationDecls(
1286+
SwiftCodePattern.buildExternFunctionDecl(
13011287
printer: printer,
1302-
wasmDecl: { printer in
1303-
SwiftCodePattern.buildExternFunctionDecl(
1304-
printer: printer,
1305-
moduleName: "bjs",
1306-
abiName: externName,
1307-
functionName: functionName,
1308-
signature: signature
1309-
)
1310-
},
1311-
elseDecl: { printer in
1312-
printer.write(
1313-
multilineString: """
1314-
fileprivate func \(functionName)\(signature) {
1315-
fatalError("Only available on WebAssembly")
1316-
}
1317-
"""
1318-
)
1319-
}
1288+
moduleName: "bjs",
1289+
abiName: externName,
1290+
functionName: functionName,
1291+
signature: signature
13201292
)
13211293
}
13221294

Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,26 +228,12 @@ public struct ImportTS {
228228
)
229229

230230
let printer = CodeFragmentPrinter()
231-
SwiftCodePattern.buildWasmConditionalCompilationDecls(
231+
SwiftCodePattern.buildExternFunctionDecl(
232232
printer: printer,
233-
wasmDecl: { printer in
234-
SwiftCodePattern.buildExternFunctionDecl(
235-
printer: printer,
236-
moduleName: moduleName,
237-
abiName: abiName,
238-
functionName: abiName,
239-
signature: signature
240-
)
241-
},
242-
elseDecl: { printer in
243-
printer.write(
244-
multilineString: """
245-
fileprivate func \(abiName)\(signature) {
246-
fatalError("Only available on WebAssembly")
247-
}
248-
"""
249-
)
250-
}
233+
moduleName: moduleName,
234+
abiName: abiName,
235+
functionName: abiName,
236+
signature: signature
251237
)
252238
return "\(raw: printer.lines.joined(separator: "\n"))"
253239
}
@@ -658,8 +644,20 @@ enum SwiftCodePattern {
658644
functionName: String,
659645
signature: String
660646
) {
661-
printer.write(buildExternAttribute(moduleName: moduleName, abiName: abiName))
662-
printer.write("fileprivate func \(functionName)\(signature)")
647+
buildWasmConditionalCompilationDecls(
648+
printer: printer,
649+
wasmDecl: { printer in
650+
printer.write(buildExternAttribute(moduleName: moduleName, abiName: abiName))
651+
printer.write("fileprivate func \(functionName)\(signature)")
652+
},
653+
elseDecl: { printer in
654+
printer.write("fileprivate func \(functionName)\(signature) {")
655+
printer.indent {
656+
printer.write("fatalError(\"Only available on WebAssembly\")")
657+
}
658+
printer.write("}")
659+
}
660+
)
663661
}
664662

665663
/// Builds the standard @_expose and @_cdecl attributes for WebAssembly-exposed functions

0 commit comments

Comments
 (0)