diff --git a/Plugins/BridgeJS/README.md b/Plugins/BridgeJS/README.md index 2fb6458a..f762c294 100644 --- a/Plugins/BridgeJS/README.md +++ b/Plugins/BridgeJS/README.md @@ -135,3 +135,4 @@ TBD declare var Foo: FooConstructor; ``` - [ ] Use `externref` once it's widely available +- [ ] Test SwiftObject roundtrip \ No newline at end of file diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift index b2bdbe84..f1605670 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift @@ -111,6 +111,7 @@ struct BridgeJSLink { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -134,6 +135,9 @@ struct BridgeJSLink { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } @@ -188,6 +192,11 @@ struct BridgeJSLink { var bodyLines: [String] = [] var cleanupLines: [String] = [] var parameterForwardings: [String] = [] + let effects: Effects + + init(effects: Effects) { + self.effects = effects + } func lowerParameter(param: Parameter) { switch param.type { @@ -245,7 +254,24 @@ struct BridgeJSLink { } func callConstructor(abiName: String) -> String { - return "instance.exports.\(abiName)(\(parameterForwardings.joined(separator: ", ")))" + let call = "instance.exports.\(abiName)(\(parameterForwardings.joined(separator: ", ")))" + bodyLines.append("const ret = \(call);") + return "ret" + } + + func checkExceptionLines() -> [String] { + guard effects.isThrows else { + return [] + } + return [ + "if (tmpRetException) {", + // TODO: Implement "take" operation + " const error = swift.memory.getObject(tmpRetException);", + " swift.memory.release(tmpRetException);", + " tmpRetException = undefined;", + " throw error;", + "}", + ] } func renderFunction( @@ -261,6 +287,7 @@ struct BridgeJSLink { ) funcLines.append(contentsOf: bodyLines.map { $0.indent(count: 4) }) funcLines.append(contentsOf: cleanupLines.map { $0.indent(count: 4) }) + funcLines.append(contentsOf: checkExceptionLines().map { $0.indent(count: 4) }) if let returnExpr = returnExpr { funcLines.append("return \(returnExpr);".indent(count: 4)) } @@ -274,7 +301,7 @@ struct BridgeJSLink { } func renderExportedFunction(function: ExportedFunction) -> (js: [String], dts: [String]) { - let thunkBuilder = ExportedThunkBuilder() + let thunkBuilder = ExportedThunkBuilder(effects: function.effects) for param in function.parameters { thunkBuilder.lowerParameter(param: param) } @@ -304,16 +331,17 @@ struct BridgeJSLink { jsLines.append("class \(klass.name) extends SwiftHeapObject {") if let constructor: ExportedConstructor = klass.constructor { - let thunkBuilder = ExportedThunkBuilder() + let thunkBuilder = ExportedThunkBuilder(effects: constructor.effects) for param in constructor.parameters { thunkBuilder.lowerParameter(param: param) } - let returnExpr = thunkBuilder.callConstructor(abiName: constructor.abiName) var funcLines: [String] = [] funcLines.append("constructor(\(constructor.parameters.map { $0.name }.joined(separator: ", "))) {") + let returnExpr = thunkBuilder.callConstructor(abiName: constructor.abiName) funcLines.append(contentsOf: thunkBuilder.bodyLines.map { $0.indent(count: 4) }) - funcLines.append("super(\(returnExpr), instance.exports.bjs_\(klass.name)_deinit);".indent(count: 4)) funcLines.append(contentsOf: thunkBuilder.cleanupLines.map { $0.indent(count: 4) }) + funcLines.append(contentsOf: thunkBuilder.checkExceptionLines().map { $0.indent(count: 4) }) + funcLines.append("super(\(returnExpr), instance.exports.bjs_\(klass.name)_deinit);".indent(count: 4)) funcLines.append("}") jsLines.append(contentsOf: funcLines.map { $0.indent(count: 4) }) @@ -324,7 +352,7 @@ struct BridgeJSLink { } for method in klass.methods { - let thunkBuilder = ExportedThunkBuilder() + let thunkBuilder = ExportedThunkBuilder(effects: method.effects) thunkBuilder.lowerSelf() for param in method.parameters { thunkBuilder.lowerParameter(param: param) diff --git a/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift b/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift index 34492682..873849f9 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift @@ -16,6 +16,11 @@ struct Parameter: Codable { let type: BridgeType } +struct Effects: Codable { + var isAsync: Bool + var isThrows: Bool +} + // MARK: - Exported Skeleton struct ExportedFunction: Codable { @@ -23,6 +28,7 @@ struct ExportedFunction: Codable { var abiName: String var parameters: [Parameter] var returnType: BridgeType + var effects: Effects } struct ExportedClass: Codable { @@ -34,6 +40,7 @@ struct ExportedClass: Codable { struct ExportedConstructor: Codable { var abiName: String var parameters: [Parameter] + var effects: Effects } struct ExportedSkeleton: Codable { diff --git a/Plugins/BridgeJS/Sources/BridgeJSTool/ExportSwift.swift b/Plugins/BridgeJS/Sources/BridgeJSTool/ExportSwift.swift index 25b1ed01..291c4a33 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSTool/ExportSwift.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSTool/ExportSwift.swift @@ -155,14 +155,43 @@ class ExportSwift { abiName = "bjs_\(className)_\(name)" } + guard let effects = collectEffects(signature: node.signature) else { + return nil + } + return ExportedFunction( name: name, abiName: abiName, parameters: parameters, - returnType: returnType + returnType: returnType, + effects: effects ) } + private func collectEffects(signature: FunctionSignatureSyntax) -> Effects? { + let isAsync = signature.effectSpecifiers?.asyncSpecifier != nil + var isThrows = false + if let throwsClause: ThrowsClauseSyntax = signature.effectSpecifiers?.throwsClause { + // Limit the thrown type to JSException for now + guard let thrownType = throwsClause.type else { + diagnose( + node: throwsClause, + message: "Thrown type is not specified, only JSException is supported for now" + ) + return nil + } + guard thrownType.trimmedDescription == "JSException" else { + diagnose( + node: throwsClause, + message: "Only JSException is supported for thrown type, got \(thrownType.trimmedDescription)" + ) + return nil + } + isThrows = true + } + return Effects(isAsync: isAsync, isThrows: isThrows) + } + override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind { guard node.attributes.hasJSAttribute() else { return .skipChildren } guard case .classBody(let name) = state else { @@ -180,9 +209,14 @@ class ExportSwift { parameters.append(Parameter(label: label, name: name, type: type)) } + guard let effects = collectEffects(signature: node.signature) else { + return .skipChildren + } + let constructor = ExportedConstructor( abiName: "bjs_\(name)_init", - parameters: parameters + parameters: parameters, + effects: effects ) exportedClasses[name]?.constructor = constructor return .skipChildren @@ -245,6 +279,8 @@ class ExportSwift { @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 + @_extern(wasm, module: "bjs", name: "swift_js_throw") + private func _swift_js_throw(_ id: Int32) """ func renderSwiftGlue() -> String? { @@ -268,6 +304,11 @@ class ExportSwift { var abiParameterForwardings: [LabeledExprSyntax] = [] var abiParameterSignatures: [(name: String, type: WasmCoreType)] = [] var abiReturnType: WasmCoreType? + let effects: Effects + + init(effects: Effects) { + self.effects = effects + } func liftParameter(param: Parameter) { switch param.type { @@ -350,35 +391,40 @@ class ExportSwift { } } - func call(name: String, returnType: BridgeType) { + private func renderCallStatement(callee: ExprSyntax, returnType: BridgeType) -> StmtSyntax { + var callExpr: ExprSyntax = + "\(raw: callee)(\(raw: abiParameterForwardings.map { $0.description }.joined(separator: ", ")))" + if effects.isAsync { + callExpr = ExprSyntax(AwaitExprSyntax(awaitKeyword: .keyword(.await), expression: callExpr)) + } + if effects.isThrows { + callExpr = ExprSyntax( + TryExprSyntax( + tryKeyword: .keyword(.try).with(\.trailingTrivia, .space), + expression: callExpr + ) + ) + } let retMutability = returnType == .string ? "var" : "let" - let callExpr: ExprSyntax = - "\(raw: name)(\(raw: abiParameterForwardings.map { $0.description }.joined(separator: ", ")))" if returnType == .void { - body.append("\(raw: callExpr)") + return StmtSyntax("\(raw: callExpr)") } else { - body.append( - """ - \(raw: retMutability) ret = \(raw: callExpr) - """ - ) + return StmtSyntax("\(raw: retMutability) ret = \(raw: callExpr)") } } + func call(name: String, returnType: BridgeType) { + let stmt = renderCallStatement(callee: "\(raw: name)", returnType: returnType) + body.append(CodeBlockItemSyntax(item: .stmt(stmt))) + } + func callMethod(klassName: String, methodName: String, returnType: BridgeType) { let _selfParam = self.abiParameterForwardings.removeFirst() - let retMutability = returnType == .string ? "var" : "let" - let callExpr: ExprSyntax = - "\(raw: _selfParam).\(raw: methodName)(\(raw: abiParameterForwardings.map { $0.description }.joined(separator: ", ")))" - if returnType == .void { - body.append("\(raw: callExpr)") - } else { - body.append( - """ - \(raw: retMutability) ret = \(raw: callExpr) - """ - ) - } + let stmt = renderCallStatement( + callee: "\(raw: _selfParam).\(raw: methodName)", + returnType: returnType + ) + body.append(CodeBlockItemSyntax(item: .stmt(stmt))) } func lowerReturnValue(returnType: BridgeType) { @@ -440,19 +486,54 @@ class ExportSwift { } func render(abiName: String) -> DeclSyntax { + let body: CodeBlockItemListSyntax + if effects.isThrows { + body = """ + do { + \(CodeBlockItemListSyntax(self.body)) + } catch let error { + if let error = error.thrownValue.object { + withExtendedLifetime(error) { + _swift_js_throw(Int32(bitPattern: $0.id)) + } + } else { + let jsError = JSError(message: String(describing: error)) + withExtendedLifetime(jsError.jsObject) { + _swift_js_throw(Int32(bitPattern: $0.id)) + } + } + \(raw: returnPlaceholderStmt()) + } + """ + } else { + body = CodeBlockItemListSyntax(self.body) + } return """ @_expose(wasm, "\(raw: abiName)") @_cdecl("\(raw: abiName)") public func _\(raw: abiName)(\(raw: parameterSignature())) -> \(raw: returnSignature()) { - \(CodeBlockItemListSyntax(body)) + \(body) } """ } + private func returnPlaceholderStmt() -> String { + switch abiReturnType { + case .i32: return "return 0" + case .i64: return "return 0" + case .f32: return "return 0.0" + case .f64: return "return 0.0" + case .pointer: return "return UnsafeMutableRawPointer(bitPattern: -1)" + case .none: return "return" + } + } + func parameterSignature() -> String { - abiParameterSignatures.map { "\($0.name): \($0.type.swiftType)" }.joined( - separator: ", " - ) + var nameAndType: [(name: String, abiType: String)] = [] + for (name, type) in abiParameterSignatures { + nameAndType.append((name, type.swiftType)) + } + return nameAndType.map { "\($0.name): \($0.abiType)" }.joined(separator: ", ") } func returnSignature() -> String { @@ -461,7 +542,7 @@ class ExportSwift { } func renderSingleExportedFunction(function: ExportedFunction) -> DeclSyntax { - let builder = ExportedThunkBuilder() + let builder = ExportedThunkBuilder(effects: function.effects) for param in function.parameters { builder.liftParameter(param: param) } @@ -520,7 +601,7 @@ class ExportSwift { func renderSingleExportedClass(klass: ExportedClass) -> [DeclSyntax] { var decls: [DeclSyntax] = [] if let constructor = klass.constructor { - let builder = ExportedThunkBuilder() + let builder = ExportedThunkBuilder(effects: constructor.effects) for param in constructor.parameters { builder.liftParameter(param: param) } @@ -529,7 +610,7 @@ class ExportSwift { decls.append(builder.render(abiName: constructor.abiName)) } for method in klass.methods { - let builder = ExportedThunkBuilder() + let builder = ExportedThunkBuilder(effects: method.effects) builder.liftParameter( param: Parameter(label: nil, name: "_self", type: .swiftHeapObject(klass.name)) ) diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Throws.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Throws.swift new file mode 100644 index 00000000..ce8c30fe --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Throws.swift @@ -0,0 +1,3 @@ +@JS func throwsSomething() throws(JSException) { + throw JSException(JSError(message: "TestError").jsValue) +} diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js index 73ef604f..1e9fa9d0 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js index 940c565f..328ff199 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js index a5b206c5..c86f3fea 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js index 7217750a..584e1308 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js index 3480cc97..d8b29c90 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js index 5aba76f1..42f805e4 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js index c9397bbd..e6dab48d 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js index 5b9808f6..844f6f35 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js index caa68521..76710fa7 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js index dfc6f048..abf1ea28 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js index 6b30cd68..0595b35a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } @@ -71,8 +75,9 @@ export async function createInstantiator(options, swift) { constructor(name) { const nameBytes = textEncoder.encode(name); const nameId = swift.memory.retain(nameBytes); - super(instance.exports.bjs_Greeter_init(nameId, nameBytes.length), instance.exports.bjs_Greeter_deinit); + const ret = instance.exports.bjs_Greeter_init(nameId, nameBytes.length); swift.memory.release(nameId); + super(ret, instance.exports.bjs_Greeter_deinit); } greet() { instance.exports.bjs_Greeter_greet(this.pointer); diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.d.ts b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.d.ts new file mode 100644 index 00000000..9199ad1a --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.d.ts @@ -0,0 +1,18 @@ +// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit, +// DO NOT EDIT. +// +// To update this file, just rebuild your project or run +// `swift package bridge-js`. + +export type Exports = { + throwsSomething(): void; +} +export type Imports = { +} +export function createInstantiator(options: { + imports: Imports; +}, swift: any): Promise<{ + addImports: (importObject: WebAssembly.Imports) => void; + setInstance: (instance: WebAssembly.Instance) => void; + createExports: (instance: WebAssembly.Instance) => Exports; +}>; \ No newline at end of file diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.js new file mode 100644 index 00000000..f15135ff --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.js @@ -0,0 +1,71 @@ +// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit, +// DO NOT EDIT. +// +// To update this file, just rebuild your project or run +// `swift package bridge-js`. + +export async function createInstantiator(options, swift) { + let instance; + let memory; + const textDecoder = new TextDecoder("utf-8"); + const textEncoder = new TextEncoder("utf-8"); + + let tmpRetString; + let tmpRetBytes; + let tmpRetException; + return { + /** @param {WebAssembly.Imports} importObject */ + addImports: (importObject) => { + const bjs = {}; + importObject["bjs"] = bjs; + bjs["return_string"] = function(ptr, len) { + const bytes = new Uint8Array(memory.buffer, ptr, len); + tmpRetString = textDecoder.decode(bytes); + } + bjs["init_memory"] = function(sourceId, bytesPtr) { + const source = swift.memory.getObject(sourceId); + const bytes = new Uint8Array(memory.buffer, bytesPtr); + bytes.set(source); + } + bjs["make_jsstring"] = function(ptr, len) { + const bytes = new Uint8Array(memory.buffer, ptr, len); + return swift.memory.retain(textDecoder.decode(bytes)); + } + bjs["init_memory_with_result"] = function(ptr, len) { + const target = new Uint8Array(memory.buffer, ptr, len); + target.set(tmpRetBytes); + tmpRetBytes = undefined; + } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } + bjs["swift_js_retain"] = function(id) { + return swift.memory.retainByRef(id); + } + bjs["swift_js_release"] = function(id) { + swift.memory.release(id); + } + + }, + setInstance: (i) => { + instance = i; + memory = instance.exports.memory; + }, + /** @param {WebAssembly.Instance} instance */ + createExports: (instance) => { + const js = swift.memory.heap; + + return { + throwsSomething: function bjs_throwsSomething() { + instance.exports.bjs_throwsSomething(); + if (tmpRetException) { + const error = swift.memory.getObject(tmpRetException); + swift.memory.release(tmpRetException); + tmpRetException = undefined; + throw error; + } + }, + }; + }, + } +} \ No newline at end of file diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js index 71133762..39306e28 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js index f86e6054..1e893f6e 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js index 166eeed0..01daf861 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js index 91b344c3..0fef27b4 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js @@ -12,6 +12,7 @@ export async function createInstantiator(options, swift) { let tmpRetString; let tmpRetBytes; + let tmpRetException; return { /** @param {WebAssembly.Imports} importObject */ addImports: (importObject) => { @@ -35,6 +36,9 @@ export async function createInstantiator(options, swift) { target.set(tmpRetBytes); tmpRetBytes = undefined; } + bjs["swift_js_throw"] = function(id) { + tmpRetException = swift.memory.retainByRef(id); + } bjs["swift_js_retain"] = function(id) { return swift.memory.retainByRef(id); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.json index 4b2dafa1..23fdeab8 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.json @@ -5,6 +5,10 @@ "functions" : [ { "abiName" : "bjs_check", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "check", "parameters" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.swift index 5181eece..8606b6d6 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.swift @@ -13,6 +13,8 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer? @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) @_expose(wasm, "bjs_check") @_cdecl("bjs_check") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.json index ae672cb5..f517c68a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.json @@ -5,6 +5,10 @@ "functions" : [ { "abiName" : "bjs_checkInt", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "checkInt", "parameters" : [ @@ -17,6 +21,10 @@ }, { "abiName" : "bjs_checkFloat", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "checkFloat", "parameters" : [ @@ -29,6 +37,10 @@ }, { "abiName" : "bjs_checkDouble", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "checkDouble", "parameters" : [ @@ -41,6 +53,10 @@ }, { "abiName" : "bjs_checkBool", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "checkBool", "parameters" : [ diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.swift index fb624231..314f916f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.swift @@ -13,6 +13,8 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer? @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) @_expose(wasm, "bjs_checkInt") @_cdecl("bjs_checkInt") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.json index 0fea9735..a86fb67e 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.json @@ -5,6 +5,10 @@ "functions" : [ { "abiName" : "bjs_checkString", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "checkString", "parameters" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.swift index d16cd81c..cbe2fb89 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.swift @@ -13,6 +13,8 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer? @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) @_expose(wasm, "bjs_checkString") @_cdecl("bjs_checkString") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.json index c773d0d2..b5536572 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.json @@ -5,6 +5,10 @@ "functions" : [ { "abiName" : "bjs_checkString", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "checkString", "parameters" : [ diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.swift index 4f3a9e89..e3fc3813 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.swift @@ -13,6 +13,8 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer? @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) @_expose(wasm, "bjs_checkString") @_cdecl("bjs_checkString") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.json index 2aff4c93..d37a9254 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.json @@ -3,6 +3,10 @@ { "constructor" : { "abiName" : "bjs_Greeter_init", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "parameters" : [ { "label" : "name", @@ -18,6 +22,10 @@ "methods" : [ { "abiName" : "bjs_Greeter_greet", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "greet", "parameters" : [ @@ -30,6 +38,10 @@ }, { "abiName" : "bjs_Greeter_changeName", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "changeName", "parameters" : [ { @@ -55,6 +67,10 @@ "functions" : [ { "abiName" : "bjs_takeGreeter", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "takeGreeter", "parameters" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.swift index fa0190f7..5602deba 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.swift @@ -13,6 +13,8 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer? @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) @_expose(wasm, "bjs_takeGreeter") @_cdecl("bjs_takeGreeter") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.json new file mode 100644 index 00000000..05363283 --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.json @@ -0,0 +1,23 @@ +{ + "classes" : [ + + ], + "functions" : [ + { + "abiName" : "bjs_throwsSomething", + "effects" : { + "isAsync" : false, + "isThrows" : true + }, + "name" : "throwsSomething", + "parameters" : [ + + ], + "returnType" : { + "void" : { + + } + } + } + ] +} \ No newline at end of file diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.swift new file mode 100644 index 00000000..73b8f492 --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.swift @@ -0,0 +1,37 @@ +// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit, +// DO NOT EDIT. +// +// To update this file, just rebuild your project or run +// `swift package bridge-js`. + +@_spi(JSObject_id) import JavaScriptKit + +@_extern(wasm, module: "bjs", name: "return_string") +private func _return_string(_ ptr: UnsafePointer?, _ len: Int32) +@_extern(wasm, module: "bjs", name: "init_memory") +private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer?) + +@_extern(wasm, module: "bjs", name: "swift_js_retain") +private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) + +@_expose(wasm, "bjs_throwsSomething") +@_cdecl("bjs_throwsSomething") +public func _bjs_throwsSomething() -> Void { + do { + try throwsSomething() + } catch let error { + if let error = error.thrownValue.object { + withExtendedLifetime(error) { + _swift_js_throw(Int32(bitPattern: $0.id)) + } + } else { + let jsError = JSError(message: String(describing: error)) + withExtendedLifetime(jsError.jsObject) { + _swift_js_throw(Int32(bitPattern: $0.id)) + } + } + return + } +} \ No newline at end of file diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.json index f82cdb82..96f875ab 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.json @@ -5,6 +5,10 @@ "functions" : [ { "abiName" : "bjs_check", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "check", "parameters" : [ diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.swift index a500740c..0fc0e157 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.swift @@ -13,6 +13,8 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer? @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) @_expose(wasm, "bjs_check") @_cdecl("bjs_check") diff --git a/Tests/BridgeJSRuntimeTests/ExportAPITests.swift b/Tests/BridgeJSRuntimeTests/ExportAPITests.swift index e113a514..2a5ae610 100644 --- a/Tests/BridgeJSRuntimeTests/ExportAPITests.swift +++ b/Tests/BridgeJSRuntimeTests/ExportAPITests.swift @@ -32,6 +32,14 @@ func runJsWorks() -> Void return v } +struct TestError: Error { + let message: String +} + +@JS func throwsSwiftError() throws(JSException) -> Void { + throw JSException(JSError(message: "TestError").jsValue) +} + @JS class Greeter { var name: String diff --git a/Tests/BridgeJSRuntimeTests/Generated/ExportSwift.swift b/Tests/BridgeJSRuntimeTests/Generated/ExportSwift.swift index 28514c8e..81202c56 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/ExportSwift.swift +++ b/Tests/BridgeJSRuntimeTests/Generated/ExportSwift.swift @@ -13,6 +13,8 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer? @_extern(wasm, module: "bjs", name: "swift_js_retain") private func _swift_js_retain(_ ptr: Int32) -> Int32 +@_extern(wasm, module: "bjs", name: "swift_js_throw") +private func _swift_js_throw(_ id: Int32) @_expose(wasm, "bjs_roundTripVoid") @_cdecl("bjs_roundTripVoid") @@ -75,6 +77,26 @@ public func _bjs_roundTripJSObject(v: Int32) -> Int32 { return _swift_js_retain(Int32(bitPattern: ret.id)) } +@_expose(wasm, "bjs_throwsSwiftError") +@_cdecl("bjs_throwsSwiftError") +public func _bjs_throwsSwiftError() -> Void { + do { + try throwsSwiftError() + } catch let error { + if let error = error.thrownValue.object { + withExtendedLifetime(error) { + _swift_js_throw(Int32(bitPattern: $0.id)) + } + } else { + let jsError = JSError(message: String(describing: error)) + withExtendedLifetime(jsError.jsObject) { + _swift_js_throw(Int32(bitPattern: $0.id)) + } + } + return + } +} + @_expose(wasm, "bjs_takeGreeter") @_cdecl("bjs_takeGreeter") public func _bjs_takeGreeter(g: UnsafeMutableRawPointer, nameBytes: Int32, nameLen: Int32) -> Void { diff --git a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/ExportSwift.json b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/ExportSwift.json index d72c17b9..cd87f654 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/ExportSwift.json +++ b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/ExportSwift.json @@ -3,6 +3,10 @@ { "constructor" : { "abiName" : "bjs_Greeter_init", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "parameters" : [ { "label" : "name", @@ -18,6 +22,10 @@ "methods" : [ { "abiName" : "bjs_Greeter_greet", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "greet", "parameters" : [ @@ -30,6 +38,10 @@ }, { "abiName" : "bjs_Greeter_changeName", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "changeName", "parameters" : [ { @@ -55,6 +67,10 @@ "functions" : [ { "abiName" : "bjs_roundTripVoid", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripVoid", "parameters" : [ @@ -67,6 +83,10 @@ }, { "abiName" : "bjs_roundTripInt", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripInt", "parameters" : [ { @@ -87,6 +107,10 @@ }, { "abiName" : "bjs_roundTripFloat", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripFloat", "parameters" : [ { @@ -107,6 +131,10 @@ }, { "abiName" : "bjs_roundTripDouble", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripDouble", "parameters" : [ { @@ -127,6 +155,10 @@ }, { "abiName" : "bjs_roundTripBool", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripBool", "parameters" : [ { @@ -147,6 +179,10 @@ }, { "abiName" : "bjs_roundTripString", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripString", "parameters" : [ { @@ -167,6 +203,10 @@ }, { "abiName" : "bjs_roundTripSwiftHeapObject", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripSwiftHeapObject", "parameters" : [ { @@ -187,6 +227,10 @@ }, { "abiName" : "bjs_roundTripJSObject", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "roundTripJSObject", "parameters" : [ { @@ -205,8 +249,28 @@ } } }, + { + "abiName" : "bjs_throwsSwiftError", + "effects" : { + "isAsync" : false, + "isThrows" : true + }, + "name" : "throwsSwiftError", + "parameters" : [ + + ], + "returnType" : { + "void" : { + + } + } + }, { "abiName" : "bjs_takeGreeter", + "effects" : { + "isAsync" : false, + "isThrows" : false + }, "name" : "takeGreeter", "parameters" : [ { diff --git a/Tests/prelude.mjs b/Tests/prelude.mjs index c79feb2a..1bc5bdba 100644 --- a/Tests/prelude.mjs +++ b/Tests/prelude.mjs @@ -105,6 +105,13 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) { const anyObject = {}; assert.equal(exports.roundTripJSObject(anyObject), anyObject); + + try { + exports.throwsSwiftError(); + assert.fail("Expected error"); + } catch (error) { + assert.equal(error.message, "TestError"); + } } function setupTestGlobals(global) {