Skip to content

Commit 7e242b0

Browse files
BridgeJS: support imports of JS Promise as async Swift (#707)
* BridgeJS: support imports of `Promise` JS as `async` Swift * E2e testing of bridging Promise<interface> returns * fix formatting * `JSTypedClosure`-based approach * Clean up `BridgeJSLink` * Fix missing `import _Concurrency` * Fix formatting * Use `JSTypedClosure` without wrapping the result value * Make closure parameters as `sending` * Check more stack ABI types * Add support for `async` in `@JSFunction` * Use namespaced import * Fix missing `fetchWeatherData` * Bring back `fetchWeatherData` * Regenerate `fetchWeatherData` bridging code * BridgeJS: Centralize closure sig collection in BridgeSkeletonWalker * BridgeJS: Stop spreading isAsync handling outside of CallJSEmission * BridgeJS: Remove error-prone default effects in thunk generation * BridgeJSLink: Centralize async handling in ImportedThunkBuilder * BridgeJS: Remove reundant returnType from `call` family of methods in ImportedThunkBuilder --------- Co-authored-by: Yuta Saito <yuta@goodnotes.com>
1 parent 7be7d4e commit 7e242b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+5227
-410
lines changed

Benchmarks/Sources/Generated/JavaScript/BridgeJS.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,6 +2839,11 @@
28392839
{
28402840
"functions" : [
28412841
{
2842+
"effects" : {
2843+
"isAsync" : false,
2844+
"isStatic" : false,
2845+
"isThrows" : true
2846+
},
28422847
"name" : "benchmarkHelperNoop",
28432848
"parameters" : [
28442849

@@ -2850,6 +2855,11 @@
28502855
}
28512856
},
28522857
{
2858+
"effects" : {
2859+
"isAsync" : false,
2860+
"isStatic" : false,
2861+
"isThrows" : true
2862+
},
28532863
"name" : "benchmarkHelperNoopWithNumber",
28542864
"parameters" : [
28552865
{
@@ -2868,6 +2878,11 @@
28682878
}
28692879
},
28702880
{
2881+
"effects" : {
2882+
"isAsync" : false,
2883+
"isStatic" : false,
2884+
"isThrows" : true
2885+
},
28712886
"name" : "benchmarkRunner",
28722887
"parameters" : [
28732888
{

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/JavaScript/BridgeJS.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@
242242
{
243243
"functions" : [
244244
{
245+
"effects" : {
246+
"isAsync" : false,
247+
"isStatic" : false,
248+
"isThrows" : true
249+
},
245250
"name" : "createTS2Swift",
246251
"parameters" : [
247252

@@ -260,6 +265,11 @@
260265
],
261266
"methods" : [
262267
{
268+
"effects" : {
269+
"isAsync" : false,
270+
"isStatic" : false,
271+
"isThrows" : true
272+
},
263273
"name" : "convert",
264274
"parameters" : [
265275
{

Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public struct ClosureCodegen {
1212
public init() {}
1313

1414
private func swiftClosureType(for signature: ClosureSignature) -> String {
15-
let closureParams = signature.parameters.map { "\($0.closureSwiftType)" }.joined(separator: ", ")
15+
let sendingPrefix = signature.sendingParameters ? "sending " : ""
16+
let closureParams = signature.parameters.map { "\(sendingPrefix)\($0.closureSwiftType)" }.joined(
17+
separator: ", "
18+
)
1619
let swiftEffects = (signature.isAsync ? " async" : "") + (signature.isThrows ? " throws" : "")
1720
let swiftReturnType = signature.returnType.closureSwiftType
1821
return "(\(closureParams))\(swiftEffects) -> \(swiftReturnType)"
@@ -29,6 +32,7 @@ public struct ClosureCodegen {
2932
let builder = try ImportTS.CallJSEmission(
3033
moduleName: "bjs",
3134
abiName: externName,
35+
effects: Effects(isAsync: signature.isAsync, isThrows: signature.isThrows),
3236
returnType: signature.returnType,
3337
context: .exportSwift
3438
)
@@ -185,11 +189,10 @@ public struct ClosureCodegen {
185189
}
186190

187191
public func renderSupport(for skeleton: BridgeJSSkeleton) throws -> String? {
188-
let collector = ClosureSignatureCollectorVisitor()
189-
var walker = BridgeTypeWalker(visitor: collector)
192+
let collector = ClosureSignatureCollectorVisitor(moduleName: skeleton.moduleName)
193+
var walker = BridgeSkeletonWalker(visitor: collector)
190194
walker.walk(skeleton)
191195
let closureSignatures = walker.visitor.signatures
192-
193196
guard !closureSignatures.isEmpty else { return nil }
194197

195198
var decls: [DeclSyntax] = []

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@ struct ProtocolCodegen {
12281228
let builder = try ImportTS.CallJSEmission(
12291229
moduleName: moduleName,
12301230
abiName: "_extern_\(method.name)",
1231+
effects: method.effects,
12311232
returnType: method.returnType,
12321233
context: .exportSwift
12331234
)
@@ -1327,6 +1328,7 @@ struct ProtocolCodegen {
13271328
let getterBuilder = try ImportTS.CallJSEmission(
13281329
moduleName: moduleName,
13291330
abiName: getterAbiName,
1331+
effects: Effects(isAsync: false, isThrows: false),
13301332
returnType: property.type,
13311333
context: .exportSwift
13321334
)
@@ -1360,6 +1362,7 @@ struct ProtocolCodegen {
13601362
let setterBuilder = try ImportTS.CallJSEmission(
13611363
moduleName: moduleName,
13621364
abiName: setterAbiName,
1365+
effects: Effects(isAsync: false, isThrows: false),
13631366
returnType: .void,
13641367
context: .exportSwift
13651368
)

0 commit comments

Comments
 (0)