Skip to content

Commit 1a83f72

Browse files
BridgeJS: Add throws(JSException) to imported methods
1 parent 58fce8c commit 1a83f72

File tree

16 files changed

+95
-82
lines changed

16 files changed

+95
-82
lines changed

Benchmarks/Sources/Generated/BridgeJS.ImportTS.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
func benchmarkHelperNoop() -> Void {
9+
func benchmarkHelperNoop() throws(JSException) -> Void {
1010
#if arch(wasm32)
1111
@_extern(wasm, module: "Benchmarks", name: "bjs_benchmarkHelperNoop")
1212
func bjs_benchmarkHelperNoop() -> Void
@@ -18,7 +18,7 @@ func benchmarkHelperNoop() -> Void {
1818
bjs_benchmarkHelperNoop()
1919
}
2020

21-
func benchmarkHelperNoopWithNumber(_ n: Double) -> Void {
21+
func benchmarkHelperNoopWithNumber(_ n: Double) throws(JSException) -> Void {
2222
#if arch(wasm32)
2323
@_extern(wasm, module: "Benchmarks", name: "bjs_benchmarkHelperNoopWithNumber")
2424
func bjs_benchmarkHelperNoopWithNumber(_ n: Float64) -> Void
@@ -30,7 +30,7 @@ func benchmarkHelperNoopWithNumber(_ n: Double) -> Void {
3030
bjs_benchmarkHelperNoopWithNumber(n)
3131
}
3232

33-
func benchmarkRunner(_ name: String, _ body: JSObject) -> Void {
33+
func benchmarkRunner(_ name: String, _ body: JSObject) throws(JSException) -> Void {
3434
#if arch(wasm32)
3535
@_extern(wasm, module: "Benchmarks", name: "bjs_benchmarkRunner")
3636
func bjs_benchmarkRunner(_ name: Int32, _ body: Int32) -> Void

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.ExportSwift.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSource
2626
_swift_js_init_memory(swiftSourceBytes, b.baseAddress.unsafelyUnwrapped)
2727
return Int(swiftSourceLen)
2828
}
29-
let dtsSource = String(unsafeUninitializedCapacity: Int(dtsSourceLen)) { b in
29+
let dtsSource = String(unsafeUninitializedCapacity: Int(dtsSourceLen)) { b in
3030
_swift_js_init_memory(dtsSourceBytes, b.baseAddress.unsafelyUnwrapped)
3131
return Int(dtsSourceLen)
3232
}
33-
let ret = try Unmanaged<PlayBridgeJS>.fromOpaque(_self).takeUnretainedValue().update(swiftSource: swiftSource, dtsSource: dtsSource)
34-
return Unmanaged.passRetained(ret).toOpaque()
33+
let ret = try Unmanaged<PlayBridgeJS>.fromOpaque(_self).takeUnretainedValue().update(swiftSource: swiftSource, dtsSource: dtsSource)
34+
return Unmanaged.passRetained(ret).toOpaque()
3535
} catch let error {
3636
if let error = error.thrownValue.object {
3737
withExtendedLifetime(error) {

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.ImportTS.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
func createTS2Skeleton() -> TS2Skeleton {
9+
func createTS2Skeleton() throws(JSException) -> TS2Skeleton {
1010
#if arch(wasm32)
1111
@_extern(wasm, module: "PlayBridgeJS", name: "bjs_createTS2Skeleton")
1212
func bjs_createTS2Skeleton() -> Int32
@@ -30,7 +30,7 @@ struct TS2Skeleton {
3030
self.this = JSObject(id: UInt32(bitPattern: this))
3131
}
3232

33-
func convert(_ ts: String) -> String {
33+
func convert(_ ts: String) throws(JSException) -> String {
3434
#if arch(wasm32)
3535
@_extern(wasm, module: "PlayBridgeJS", name: "bjs_TS2Skeleton_convert")
3636
func bjs_TS2Skeleton_convert(_ self: Int32, _ ts: Int32) -> Int32

Examples/PlayBridgeJS/Sources/PlayBridgeJS/main.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import class Foundation.JSONDecoder
99
@JS func update(swiftSource: String, dtsSource: String) throws(JSException) -> PlayBridgeJSOutput {
1010
do {
1111
return try _update(swiftSource: swiftSource, dtsSource: dtsSource)
12+
} catch let error as JSException {
13+
throw error
1214
} catch {
1315
throw JSException(message: String(describing: error))
1416
}
@@ -20,8 +22,8 @@ import class Foundation.JSONDecoder
2022
try exportSwift.addSourceFile(sourceFile, "Playground.swift")
2123
let exportResult = try exportSwift.finalize()
2224
var importTS = ImportTS(progress: .silent, moduleName: "Playground")
23-
let ts2skeleton = createTS2Skeleton()
24-
let skeletonJSONString = ts2skeleton.convert(dtsSource)
25+
let ts2skeleton = try createTS2Skeleton()
26+
let skeletonJSONString = try ts2skeleton.convert(dtsSource)
2527
let decoder = JSONDecoder()
2628
let importSkeleton = try decoder.decode(
2729
ImportedFileSkeleton.self,

Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ struct ImportTS {
213213
)
214214
}
215215
}),
216-
effectSpecifiers: FunctionEffectSpecifiersSyntax(
217-
throwsClause: ThrowsClauseSyntax(throwsSpecifier: .keyword(.throws))
218-
),
219216
returnClause: ReturnClauseSyntax(
220217
arrow: .arrowToken(),
221218
type: IdentifierTypeSyntax(name: .identifier(abiReturnType.map { $0.swiftType } ?? "Void"))
@@ -256,6 +253,7 @@ struct ImportTS {
256253
)
257254
}
258255
}),
256+
effectSpecifiers: ImportTS.buildFunctionEffect(throws: true, async: false),
259257
returnClause: ReturnClauseSyntax(
260258
arrow: .arrowToken(),
261259
type: IdentifierTypeSyntax(name: .identifier(returnType.swiftType))
@@ -472,4 +470,17 @@ struct ImportTS {
472470
let lines = documentation.split { $0.isNewline }
473471
return Trivia(pieces: lines.flatMap { [TriviaPiece.docLineComment("/// \($0)"), .newlines(1)] })
474472
}
473+
474+
static func buildFunctionEffect(throws: Bool, async: Bool) -> FunctionEffectSpecifiersSyntax {
475+
return FunctionEffectSpecifiersSyntax(
476+
asyncSpecifier: `async` ? .keyword(.async) : nil,
477+
throwsClause: `throws`
478+
? ThrowsClauseSyntax(
479+
throwsSpecifier: .keyword(.throws),
480+
leftParen: .leftParenToken(),
481+
type: IdentifierTypeSyntax(name: .identifier("JSException")),
482+
rightParen: .rightParenToken()
483+
) : nil,
484+
)
485+
}
475486
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ImportTSTests/ArrayParameter.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,36 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
func checkArray(_ a: JSObject) -> Void {
9+
func checkArray(_ a: JSObject) throws(JSException) -> Void {
1010
#if arch(wasm32)
1111
@_extern(wasm, module: "Check", name: "bjs_checkArray")
12-
func bjs_checkArray(_ a: Int32) throws -> Void
12+
func bjs_checkArray(_ a: Int32) -> Void
1313
#else
14-
func bjs_checkArray(_ a: Int32) throws -> Void {
14+
func bjs_checkArray(_ a: Int32) -> Void {
1515
fatalError("Only available on WebAssembly")
1616
}
1717
#endif
1818
bjs_checkArray(Int32(bitPattern: a.id))
1919
}
2020

21-
func checkArrayWithLength(_ a: JSObject, _ b: Double) -> Void {
21+
func checkArrayWithLength(_ a: JSObject, _ b: Double) throws(JSException) -> Void {
2222
#if arch(wasm32)
2323
@_extern(wasm, module: "Check", name: "bjs_checkArrayWithLength")
24-
func bjs_checkArrayWithLength(_ a: Int32, _ b: Float64) throws -> Void
24+
func bjs_checkArrayWithLength(_ a: Int32, _ b: Float64) -> Void
2525
#else
26-
func bjs_checkArrayWithLength(_ a: Int32, _ b: Float64) throws -> Void {
26+
func bjs_checkArrayWithLength(_ a: Int32, _ b: Float64) -> Void {
2727
fatalError("Only available on WebAssembly")
2828
}
2929
#endif
3030
bjs_checkArrayWithLength(Int32(bitPattern: a.id), b)
3131
}
3232

33-
func checkArray(_ a: JSObject) -> Void {
33+
func checkArray(_ a: JSObject) throws(JSException) -> Void {
3434
#if arch(wasm32)
3535
@_extern(wasm, module: "Check", name: "bjs_checkArray")
36-
func bjs_checkArray(_ a: Int32) throws -> Void
36+
func bjs_checkArray(_ a: Int32) -> Void
3737
#else
38-
func bjs_checkArray(_ a: Int32) throws -> Void {
38+
func bjs_checkArray(_ a: Int32) -> Void {
3939
fatalError("Only available on WebAssembly")
4040
}
4141
#endif

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ImportTSTests/Interface.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
func returnAnimatable() -> Animatable {
9+
func returnAnimatable() throws(JSException) -> Animatable {
1010
#if arch(wasm32)
1111
@_extern(wasm, module: "Check", name: "bjs_returnAnimatable")
12-
func bjs_returnAnimatable() throws -> Int32
12+
func bjs_returnAnimatable() -> Int32
1313
#else
14-
func bjs_returnAnimatable() throws -> Int32 {
14+
func bjs_returnAnimatable() -> Int32 {
1515
fatalError("Only available on WebAssembly")
1616
}
1717
#endif
@@ -30,25 +30,25 @@ struct Animatable {
3030
self.this = JSObject(id: UInt32(bitPattern: this))
3131
}
3232

33-
func animate(_ keyframes: JSObject, _ options: JSObject) -> JSObject {
33+
func animate(_ keyframes: JSObject, _ options: JSObject) throws(JSException) -> JSObject {
3434
#if arch(wasm32)
3535
@_extern(wasm, module: "Check", name: "bjs_Animatable_animate")
36-
func bjs_Animatable_animate(_ self: Int32, _ keyframes: Int32, _ options: Int32) throws -> Int32
36+
func bjs_Animatable_animate(_ self: Int32, _ keyframes: Int32, _ options: Int32) -> Int32
3737
#else
38-
func bjs_Animatable_animate(_ self: Int32, _ keyframes: Int32, _ options: Int32) throws -> Int32 {
38+
func bjs_Animatable_animate(_ self: Int32, _ keyframes: Int32, _ options: Int32) -> Int32 {
3939
fatalError("Only available on WebAssembly")
4040
}
4141
#endif
4242
let ret = bjs_Animatable_animate(Int32(bitPattern: self.this.id), Int32(bitPattern: keyframes.id), Int32(bitPattern: options.id))
4343
return JSObject(id: UInt32(bitPattern: ret))
4444
}
4545

46-
func getAnimations(_ options: JSObject) -> JSObject {
46+
func getAnimations(_ options: JSObject) throws(JSException) -> JSObject {
4747
#if arch(wasm32)
4848
@_extern(wasm, module: "Check", name: "bjs_Animatable_getAnimations")
49-
func bjs_Animatable_getAnimations(_ self: Int32, _ options: Int32) throws -> Int32
49+
func bjs_Animatable_getAnimations(_ self: Int32, _ options: Int32) -> Int32
5050
#else
51-
func bjs_Animatable_getAnimations(_ self: Int32, _ options: Int32) throws -> Int32 {
51+
func bjs_Animatable_getAnimations(_ self: Int32, _ options: Int32) -> Int32 {
5252
fatalError("Only available on WebAssembly")
5353
}
5454
#endif

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ImportTSTests/PrimitiveParameters.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
func check(_ a: Double, _ b: Bool) -> Void {
9+
func check(_ a: Double, _ b: Bool) throws(JSException) -> Void {
1010
#if arch(wasm32)
1111
@_extern(wasm, module: "Check", name: "bjs_check")
12-
func bjs_check(_ a: Float64, _ b: Int32) throws -> Void
12+
func bjs_check(_ a: Float64, _ b: Int32) -> Void
1313
#else
14-
func bjs_check(_ a: Float64, _ b: Int32) throws -> Void {
14+
func bjs_check(_ a: Float64, _ b: Int32) -> Void {
1515
fatalError("Only available on WebAssembly")
1616
}
1717
#endif

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ImportTSTests/PrimitiveReturn.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
func checkNumber() -> Double {
9+
func checkNumber() throws(JSException) -> Double {
1010
#if arch(wasm32)
1111
@_extern(wasm, module: "Check", name: "bjs_checkNumber")
12-
func bjs_checkNumber() throws -> Float64
12+
func bjs_checkNumber() -> Float64
1313
#else
14-
func bjs_checkNumber() throws -> Float64 {
14+
func bjs_checkNumber() -> Float64 {
1515
fatalError("Only available on WebAssembly")
1616
}
1717
#endif
1818
let ret = bjs_checkNumber()
1919
return Double(ret)
2020
}
2121

22-
func checkBoolean() -> Bool {
22+
func checkBoolean() throws(JSException) -> Bool {
2323
#if arch(wasm32)
2424
@_extern(wasm, module: "Check", name: "bjs_checkBoolean")
25-
func bjs_checkBoolean() throws -> Int32
25+
func bjs_checkBoolean() -> Int32
2626
#else
27-
func bjs_checkBoolean() throws -> Int32 {
27+
func bjs_checkBoolean() -> Int32 {
2828
fatalError("Only available on WebAssembly")
2929
}
3030
#endif

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ImportTSTests/StringParameter.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
func checkString(_ a: String) -> Void {
9+
func checkString(_ a: String) throws(JSException) -> Void {
1010
#if arch(wasm32)
1111
@_extern(wasm, module: "Check", name: "bjs_checkString")
12-
func bjs_checkString(_ a: Int32) throws -> Void
12+
func bjs_checkString(_ a: Int32) -> Void
1313
#else
14-
func bjs_checkString(_ a: Int32) throws -> Void {
14+
func bjs_checkString(_ a: Int32) -> Void {
1515
fatalError("Only available on WebAssembly")
1616
}
1717
#endif
@@ -22,12 +22,12 @@ func checkString(_ a: String) -> Void {
2222
bjs_checkString(aId)
2323
}
2424

25-
func checkStringWithLength(_ a: String, _ b: Double) -> Void {
25+
func checkStringWithLength(_ a: String, _ b: Double) throws(JSException) -> Void {
2626
#if arch(wasm32)
2727
@_extern(wasm, module: "Check", name: "bjs_checkStringWithLength")
28-
func bjs_checkStringWithLength(_ a: Int32, _ b: Float64) throws -> Void
28+
func bjs_checkStringWithLength(_ a: Int32, _ b: Float64) -> Void
2929
#else
30-
func bjs_checkStringWithLength(_ a: Int32, _ b: Float64) throws -> Void {
30+
func bjs_checkStringWithLength(_ a: Int32, _ b: Float64) -> Void {
3131
fatalError("Only available on WebAssembly")
3232
}
3333
#endif

0 commit comments

Comments
 (0)