diff --git a/Benchmarks/Sources/Generated/BridgeJS.swift b/Benchmarks/Sources/Generated/BridgeJS.swift index 5a480694c..515d3a531 100644 --- a/Benchmarks/Sources/Generated/BridgeJS.swift +++ b/Benchmarks/Sources/Generated/BridgeJS.swift @@ -312,7 +312,7 @@ extension Person: _BridgedSwiftStruct { self.address.bridgeJSLowerReturn() let __bjs_isSome_email = self.email != nil if let __bjs_unwrapped_email = self.email { - __bjs_unwrapped_email.bridgeJSLowerStackReturn() + __bjs_unwrapped_email.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_email ? 1 : 0) } @@ -1569,12 +1569,12 @@ public func _bjs_ArrayRoundtrip_takeNestedIntArray(_ _self: UnsafeMutableRawPoin let __count = Int(_swift_js_pop_i32()) var __result: [[Int]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Int].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) #else fatalError("Only available on WebAssembly") #endif @@ -1586,7 +1586,8 @@ public func _bjs_ArrayRoundtrip_makeNestedIntArray(_ _self: UnsafeMutableRawPoin #if arch(wasm32) let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeNestedIntArray() for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1601,14 +1602,15 @@ public func _bjs_ArrayRoundtrip_roundtripNestedIntArray(_ _self: UnsafeMutableRa let __count = Int(_swift_js_pop_i32()) var __result: [[Int]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Int].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1623,12 +1625,12 @@ public func _bjs_ArrayRoundtrip_takeNestedPointArray(_ _self: UnsafeMutableRawPo let __count = Int(_swift_js_pop_i32()) var __result: [[Point]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Point].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) #else fatalError("Only available on WebAssembly") #endif @@ -1640,7 +1642,8 @@ public func _bjs_ArrayRoundtrip_makeNestedPointArray(_ _self: UnsafeMutableRawPo #if arch(wasm32) let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeNestedPointArray() for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1655,14 +1658,15 @@ public func _bjs_ArrayRoundtrip_roundtripNestedPointArray(_ _self: UnsafeMutable let __count = Int(_swift_js_pop_i32()) var __result: [[Point]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Point].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1677,12 +1681,12 @@ public func _bjs_ArrayRoundtrip_takeOptionalIntArray(_ _self: UnsafeMutableRawPo let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) #else fatalError("Only available on WebAssembly") #endif @@ -1696,8 +1700,10 @@ public func _bjs_ArrayRoundtrip_makeOptionalIntArray(_ _self: UnsafeMutableRawPo for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1712,17 +1718,19 @@ public func _bjs_ArrayRoundtrip_roundtripOptionalIntArray(_ _self: UnsafeMutable let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1737,12 +1745,12 @@ public func _bjs_ArrayRoundtrip_takeOptionalPointArray(_ _self: UnsafeMutableRaw let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) #else fatalError("Only available on WebAssembly") #endif @@ -1756,8 +1764,10 @@ public func _bjs_ArrayRoundtrip_makeOptionalPointArray(_ _self: UnsafeMutableRaw for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1772,17 +1782,19 @@ public func _bjs_ArrayRoundtrip_roundtripOptionalPointArray(_ _self: UnsafeMutab let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -1799,7 +1811,7 @@ public func _bjs_ArrayRoundtrip_takeOptionalArray(_ _self: UnsafeMutableRawPoint } else { return [Int].bridgeJSLiftParameter() } - }()) + }()) #else fatalError("Only available on WebAssembly") #endif @@ -1812,7 +1824,8 @@ public func _bjs_ArrayRoundtrip_makeOptionalArraySome(_ _self: UnsafeMutableRawP let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeOptionalArraySome() let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") @@ -1826,7 +1839,8 @@ public func _bjs_ArrayRoundtrip_makeOptionalArrayNone(_ _self: UnsafeMutableRawP let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeOptionalArrayNone() let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") @@ -1843,10 +1857,11 @@ public func _bjs_ArrayRoundtrip_roundtripOptionalArray(_ _self: UnsafeMutableRaw } else { return [Int].bridgeJSLiftParameter() } - }()) + }()) let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift index 49f2b99bf..eefc63a7e 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift @@ -4,6 +4,9 @@ import SwiftSyntaxBuilder #if canImport(BridgeJSSkeleton) import BridgeJSSkeleton #endif +#if canImport(BridgeJSUtilities) +import BridgeJSUtilities +#endif public struct ClosureCodegen { public init() {} @@ -41,9 +44,6 @@ public struct ClosureCodegen { try builder.call(returnType: signature.returnType) try builder.liftReturnValue(returnType: signature.returnType) - // Get the body code - let bodyCode = builder.getBody() - // Generate extern declaration using CallJSEmission let externDecl = builder.renderImportDecl() @@ -58,79 +58,42 @@ public struct ClosureCodegen { #endif """ - let helperEnumDecl = EnumDeclSyntax( - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.private)) - }, - name: .identifier(helperName), - memberBlockBuilder: { - DeclSyntax( - FunctionDeclSyntax( - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.static)) - }, - name: .identifier("bridgeJSLift"), - signature: FunctionSignatureSyntax( - parameterClause: FunctionParameterClauseSyntax { - FunctionParameterSyntax( - firstName: .wildcardToken(), - secondName: .identifier("callbackId"), - colon: .colonToken(), - type: IdentifierTypeSyntax(name: .identifier("Int32")) - ) - }, - returnClause: ReturnClauseSyntax( - arrow: .arrowToken(), - type: IdentifierTypeSyntax(name: .identifier(swiftClosureType)) - ) - ), - body: CodeBlockSyntax { - "let callback = JSObject.bridgeJSLiftParameter(callbackId)" - ReturnStmtSyntax( - expression: ClosureExprSyntax( - leftBrace: .leftBraceToken(), - signature: ClosureSignatureSyntax( - capture: ClosureCaptureClauseSyntax( - leftSquare: .leftSquareToken(), - items: ClosureCaptureListSyntax { - #if canImport(SwiftSyntax602) - ClosureCaptureSyntax( - name: .identifier("", presence: .missing), - initializer: InitializerClauseSyntax( - equal: .equalToken(presence: .missing), - nil, - value: ExprSyntax("callback") - ), - trailingTrivia: nil - ) - #else - ClosureCaptureSyntax( - expression: ExprSyntax("callback") - ) - #endif - }, - rightSquare: .rightSquareToken() - ), - parameterClause: .simpleInput( - ClosureShorthandParameterListSyntax { - for (index, _) in signature.parameters.enumerated() { - ClosureShorthandParameterSyntax(name: .identifier("param\(index)")) - } - } - ), - inKeyword: .keyword(.in) - ), - statements: CodeBlockItemListSyntax { - SwiftCodePattern.buildWasmConditionalCompilation(wasmBody: bodyCode.statements) - }, - rightBrace: .rightBraceToken() - ) - ) + let helperEnumDeclPrinter = CodeFragmentPrinter() + helperEnumDeclPrinter.write("private enum \(helperName) {") + helperEnumDeclPrinter.indent { + helperEnumDeclPrinter.write("static func bridgeJSLift(_ callbackId: Int32) -> \(swiftClosureType) {") + helperEnumDeclPrinter.indent { + helperEnumDeclPrinter.write("let callback = JSObject.bridgeJSLiftParameter(callbackId)") + let parameters: String + if signature.parameters.isEmpty { + parameters = "" + } else if signature.parameters.count == 1 { + parameters = " param0" + } else { + parameters = + " (" + + signature.parameters.enumerated().map { index, param in + "param\(index)" + }.joined(separator: ", ") + ")" + } + helperEnumDeclPrinter.write("return { [callback]\(parameters) in") + helperEnumDeclPrinter.indent { + SwiftCodePattern.buildWasmConditionalCompilation( + printer: helperEnumDeclPrinter, + wasmBody: { printer in + printer.write(lines: builder.body.lines) } ) - ) + } + helperEnumDeclPrinter.write("}") + } - ) + helperEnumDeclPrinter.write("}") + } + helperEnumDeclPrinter.write("}") + + let helperEnumDecl: DeclSyntax = "\(raw: helperEnumDeclPrinter.lines.joined(separator: "\n"))" + let typedClosureExtension: DeclSyntax = """ extension JSTypedClosure where Signature == \(raw: swiftClosureType) { init(fileID: StaticString = #fileID, line: UInt32 = #line, _ body: @escaping \(raw: swiftClosureType)) { @@ -145,7 +108,7 @@ public struct ClosureCodegen { """ return [ - externDecl, makeClosureExternDecl, DeclSyntax(helperEnumDecl), typedClosureExtension, + externDecl, makeClosureExternDecl, helperEnumDecl, typedClosureExtension, ] } @@ -199,24 +162,20 @@ public struct ClosureCodegen { returnType: abiReturnWasmType ) - // Build body - let body = CodeBlockItemListSyntax { - "let closure = Unmanaged<\(raw: boxType)>.fromOpaque(boxPtr).takeUnretainedValue().closure" + // Build function declaration using helper + let funcDecl = SwiftCodePattern.buildExposedFunctionDecl( + abiName: abiName, + signature: funcSignature + ) { printer in + printer.write("let closure = Unmanaged<\(boxType)>.fromOpaque(boxPtr).takeUnretainedValue().closure") if signature.returnType == .void { - closureCallExpr + printer.write(closureCallExpr.description) } else { - "let result = \(closureCallExpr)" - "return result.bridgeJSLowerReturn()" + printer.write("let result = \(closureCallExpr)") + printer.write("return result.bridgeJSLowerReturn()") } } - // Build function declaration using helper - let funcDecl = SwiftCodePattern.buildExposedFunctionDecl( - abiName: abiName, - signature: funcSignature, - body: body - ) - return DeclSyntax(funcDecl) } diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift index 913bc076b..2d31cd212 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift @@ -91,8 +91,7 @@ public class ExportSwift { } } return withSpan("Format Export Glue") { - let format = BasicFormat() - return decls.map { $0.formatted(using: format).description }.joined(separator: "\n\n") + return decls.map { $0.description }.joined(separator: "\n\n") } } @@ -423,9 +422,10 @@ public class ExportSwift { // Build function declaration using helper function let funcDecl = SwiftCodePattern.buildExposedFunctionDecl( abiName: abiName, - signature: signature, - body: body - ) + signature: signature + ) { printer in + printer.write(multilineString: body.description) + } return DeclSyntax(funcDecl) } @@ -715,11 +715,10 @@ public class ExportSwift { signature: SwiftSignatureBuilder.buildABIFunctionSignature( abiParameters: [("pointer", .pointer)], returnType: nil - ), - body: CodeBlockItemListSyntax { - "Unmanaged<\(raw: klass.swiftCallName)>.fromOpaque(pointer).release()" - } - ) + ) + ) { printer in + printer.write("Unmanaged<\(klass.swiftCallName)>.fromOpaque(pointer).release()") + } decls.append(DeclSyntax(funcDecl)) } @@ -763,34 +762,29 @@ public class ExportSwift { returnType: .i32 ) - // Build extern function declaration (no body) - let externFuncDecl = SwiftCodePattern.buildExternFunctionDecl( - moduleName: moduleName, - abiName: externFunctionName, - functionName: wrapFunctionName, - signature: funcSignature - ) - - // Build stub function declaration (with fatalError body) - let stubFuncDecl = FunctionDeclSyntax( - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.fileprivate)) + let externDeclPrinter = CodeFragmentPrinter() + SwiftCodePattern.buildWasmConditionalCompilationDecls( + printer: externDeclPrinter, + wasmDecl: { printer in + SwiftCodePattern.buildExternFunctionDecl( + printer: printer, + moduleName: moduleName, + abiName: externFunctionName, + functionName: wrapFunctionName, + signature: funcSignature + ) }, - funcKeyword: .keyword(.func), - name: .identifier(wrapFunctionName), - signature: funcSignature, - body: CodeBlockSyntax { - "fatalError(\"Only available on WebAssembly\")" + elseDecl: { printer in + printer.write( + multilineString: """ + fileprivate func \(wrapFunctionName)\(funcSignature) { + fatalError("Only available on WebAssembly") + } + """ + ) } ) - - // Use helper function for conditional compilation - let externDecl = DeclSyntax( - SwiftCodePattern.buildWasmConditionalCompilationDecls( - wasmDecl: DeclSyntax(externFuncDecl), - elseDecl: DeclSyntax(stubFuncDecl) - ) - ) + let externDecl: DeclSyntax = "\(raw: externDeclPrinter.lines.joined(separator: "\n"))" return [extensionDecl, externDecl] } } @@ -1035,9 +1029,9 @@ struct StackCodegen { accessor: String, varPrefix: String ) -> [CodeBlockItemSyntax] { - var statements: [CodeBlockItemSyntax] = [] + var statements: [String] = [] let elementVarName = "__bjs_elem_\(varPrefix)" - statements.append("for \(raw: elementVarName) in \(raw: accessor) {") + statements.append("for \(elementVarName) in \(accessor) {") let elementStatements = lowerStatements( for: elementType, @@ -1045,12 +1039,13 @@ struct StackCodegen { varPrefix: "\(varPrefix)_elem" ) for stmt in elementStatements { - statements.append(stmt) + statements.append(stmt.description) } statements.append("}") - statements.append("_swift_js_push_i32(Int32(\(raw: accessor).count))") - return statements + statements.append("_swift_js_push_i32(Int32(\(accessor).count))") + let parsed: CodeBlockItemListSyntax = "\(raw: statements.joined(separator: "\n"))" + return Array(parsed) } private func lowerDictionaryStatements( @@ -1117,9 +1112,9 @@ struct StackCodegen { accessor: String, varPrefix: String ) -> [CodeBlockItemSyntax] { - var statements: [CodeBlockItemSyntax] = [] - statements.append("let __bjs_isSome_\(raw: varPrefix) = \(raw: accessor) != nil") - statements.append("if let __bjs_unwrapped_\(raw: varPrefix) = \(raw: accessor) {") + var statements: [String] = [] + statements.append("let __bjs_isSome_\(varPrefix) = \(accessor) != nil") + statements.append("if let __bjs_unwrapped_\(varPrefix) = \(accessor) {") let innerStatements = lowerUnwrappedOptionalStatements( wrappedType: wrappedType, @@ -1127,12 +1122,13 @@ struct StackCodegen { varPrefix: varPrefix ) for stmt in innerStatements { - statements.append(stmt) + statements.append(stmt.description) } statements.append("}") - statements.append("_swift_js_push_i32(__bjs_isSome_\(raw: varPrefix) ? 1 : 0)") - return statements + statements.append("_swift_js_push_i32(__bjs_isSome_\(varPrefix) ? 1 : 0)") + let parsed: CodeBlockItemListSyntax = "\(raw: statements.joined(separator: "\n"))" + return Array(parsed) } private func lowerUnwrappedOptionalStatements( @@ -1186,104 +1182,151 @@ struct EnumCodegen { } private func renderCaseEnumHelpers(_ enumDef: ExportedEnum) -> DeclSyntax { - let typeName = enumDef.swiftCallName - var initCases: [String] = [] - var valueCases: [String] = [] - for (index, enumCase) in enumDef.cases.enumerated() { - initCases.append("case \(index): self = .\(enumCase.name)") - valueCases.append("case .\(enumCase.name): return \(index)") - } - let initSwitch = (["switch bridgeJSRawValue {"] + initCases + ["default: return nil", "}"]).joined( - separator: "\n" - ) - let valueSwitch = (["switch self {"] + valueCases + ["}"]).joined(separator: "\n") + let printer = CodeFragmentPrinter() + printer.write("extension \(enumDef.swiftCallName): _BridgedSwiftCaseEnum {") + printer.indent { + printer.write( + multilineString: """ + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + return bridgeJSRawValue + } + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> \(enumDef.swiftCallName) { + return bridgeJSLiftParameter(value) + } + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> \(enumDef.swiftCallName) { + return \(enumDef.swiftCallName)(bridgeJSRawValue: value)! + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { + return bridgeJSLowerParameter() + } - return """ - extension \(raw: typeName): _BridgedSwiftCaseEnum { - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { - return bridgeJSRawValue - } - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> \(raw: typeName) { - return bridgeJSLiftParameter(value) - } - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> \(raw: typeName) { - return \(raw: typeName)(bridgeJSRawValue: value)! - } - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSLowerParameter() + """ + ) + printer.nextLine() + + printer.write("private init?(bridgeJSRawValue: Int32) {") + printer.indent { + printer.write("switch bridgeJSRawValue {") + for (index, enumCase) in enumDef.cases.enumerated() { + printer.write("case \(index):") + printer.indent { + printer.write("self = .\(enumCase.name)") + } } - - private init?(bridgeJSRawValue: Int32) { - \(raw: initSwitch) + printer.write("default:") + printer.indent { + printer.write("return nil") } - - private var bridgeJSRawValue: Int32 { - \(raw: valueSwitch) + printer.write("}") + } + printer.write("}") + printer.nextLine() + + printer.write("private var bridgeJSRawValue: Int32 {") + printer.indent { + printer.write("switch self {") + for (index, enumCase) in enumDef.cases.enumerated() { + printer.write("case .\(enumCase.name):") + printer.indent { + printer.write("return \(index)") + } } + printer.write("}") } - """ + printer.write("}") + } + printer.write("}") + return "\(raw: printer.lines.joined(separator: "\n"))" } private func renderRawValueEnumHelpers(_ enumDef: ExportedEnum) -> DeclSyntax { let typeName = enumDef.swiftCallName guard enumDef.rawType != nil else { return """ - extension \(raw: typeName): _BridgedSwiftEnumNoPayload {} + extension \(raw: typeName): _BridgedSwiftEnumNoPayload { + } """ } // When rawType is present, conform to _BridgedSwiftRawValueEnum which provides // default implementations for _BridgedSwiftStackType methods via protocol extension. return """ - extension \(raw: typeName): _BridgedSwiftEnumNoPayload, _BridgedSwiftRawValueEnum {} + extension \(raw: typeName): _BridgedSwiftEnumNoPayload, _BridgedSwiftRawValueEnum { + } """ } private func renderAssociatedValueEnumHelpers(_ enumDef: ExportedEnum) -> DeclSyntax { let typeName = enumDef.swiftCallName - return """ - extension \(raw: typeName): _BridgedSwiftAssociatedValueEnum { - private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> \(raw: typeName) { - switch caseId { - \(raw: generateStackLiftSwitchCases(enumDef: enumDef).joined(separator: "\n")) - default: fatalError("Unknown \(raw: typeName) case ID: \\(caseId)") - } + let printer = CodeFragmentPrinter() + printer.write("extension \(typeName): _BridgedSwiftAssociatedValueEnum {") + printer.indent { + printer.write("private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> \(typeName) {") + printer.indent { + printer.write("switch caseId {") + generateStackLiftSwitchCases(printer: printer, enumDef: enumDef) + printer.write("default:") + printer.indent { + printer.write("fatalError(\"Unknown \(typeName) case ID: \\(caseId)\")") } + printer.write("}") + } + printer.write("}") + printer.nextLine() - // MARK: Protocol Export - - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { - switch self { - \(raw: generateLowerParameterSwitchCases(enumDef: enumDef).joined(separator: "\n")) - } - } + printer.write("// MARK: Protocol Export") + printer.nextLine() - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> \(raw: typeName) { - return _bridgeJSLiftFromCaseId(caseId) - } + printer.write("@_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 {") + printer.indent { + printer.write("switch self {") + generateLowerParameterSwitchCases(printer: printer, enumDef: enumDef) + printer.write("}") + } + printer.write("}") + printer.nextLine() - // MARK: ExportSwift + printer.write( + multilineString: """ + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> \(typeName) { + return _bridgeJSLiftFromCaseId(caseId) + } + """ + ) + printer.nextLine() - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> \(raw: typeName) { - return _bridgeJSLiftFromCaseId(caseId) - } + printer.write("// MARK: ExportSwift") + printer.nextLine() - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { - switch self { - \(raw: generateReturnSwitchCases(enumDef: enumDef).joined(separator: "\n")) + printer.write( + multilineString: """ + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> \(typeName) { + return _bridgeJSLiftFromCaseId(caseId) } - } + """ + ) + printer.nextLine() + + printer.write("@_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() {") + printer.indent { + printer.write("switch self {") + generateReturnSwitchCases(printer: printer, enumDef: enumDef) + printer.write("}") } - """ + printer.write("}") + } + printer.write("}") + return "\(raw: printer.lines.joined(separator: "\n"))" } - private func generateStackLiftSwitchCases(enumDef: ExportedEnum) -> [String] { - var cases: [String] = [] + private func generateStackLiftSwitchCases(printer: CodeFragmentPrinter, enumDef: ExportedEnum) { for (caseIndex, enumCase) in enumDef.cases.enumerated() { if enumCase.associatedValues.isEmpty { - cases.append("case \(caseIndex): return .\(enumCase.name)") + printer.write("case \(caseIndex):") + printer.indent { + printer.write("return .\(enumCase.name)") + } } else { - var lines: [String] = [] - lines.append("case \(caseIndex):") + printer.write("case \(caseIndex):") let argList = enumCase.associatedValues.map { associatedValue in let labelPrefix: String if let label = associatedValue.label { @@ -1294,17 +1337,17 @@ struct EnumCodegen { let liftExpr = stackCodegen.liftExpression(for: associatedValue.type) return "\(labelPrefix)\(liftExpr)" } - lines.append("return .\(enumCase.name)(\(argList.joined(separator: ", ")))") - cases.append(lines.joined(separator: "\n")) + printer.indent { + printer.write("return .\(enumCase.name)(\(argList.joined(separator: ", ")))") + } } } - return cases } private func generatePayloadPushingCode( + printer: CodeFragmentPrinter, associatedValues: [AssociatedValue] - ) -> [String] { - var bodyLines: [String] = [] + ) { for (index, associatedValue) in associatedValues.enumerated() { let paramName = associatedValue.label ?? "param\(index)" let statements = stackCodegen.lowerStatements( @@ -1312,51 +1355,50 @@ struct EnumCodegen { accessor: paramName, varPrefix: paramName ) - for stmt in statements { - bodyLines.append(stmt.description) - } + printer.write(multilineString: CodeBlockItemListSyntax(statements).description) } - return bodyLines } - private func generateLowerParameterSwitchCases(enumDef: ExportedEnum) -> [String] { - var cases: [String] = [] + private func generateLowerParameterSwitchCases(printer: CodeFragmentPrinter, enumDef: ExportedEnum) { for (caseIndex, enumCase) in enumDef.cases.enumerated() { if enumCase.associatedValues.isEmpty { - cases.append("case .\(enumCase.name):") - cases.append("return Int32(\(caseIndex))") + printer.write("case .\(enumCase.name):") + printer.indent { + printer.write("return Int32(\(caseIndex))") + } } else { - let payloadCode = generatePayloadPushingCode(associatedValues: enumCase.associatedValues) let pattern = enumCase.associatedValues.enumerated() .map { index, associatedValue in "let \(associatedValue.label ?? "param\(index)")" } .joined(separator: ", ") - cases.append("case .\(enumCase.name)(\(pattern)):") - cases.append(contentsOf: payloadCode) - cases.append("return Int32(\(caseIndex))") + printer.write("case .\(enumCase.name)(\(pattern)):") + printer.indent { + generatePayloadPushingCode(printer: printer, associatedValues: enumCase.associatedValues) + printer.write("return Int32(\(caseIndex))") + } } } - return cases } - private func generateReturnSwitchCases(enumDef: ExportedEnum) -> [String] { - var cases: [String] = [] + private func generateReturnSwitchCases(printer: CodeFragmentPrinter, enumDef: ExportedEnum) { for (caseIndex, enumCase) in enumDef.cases.enumerated() { if enumCase.associatedValues.isEmpty { - cases.append("case .\(enumCase.name):") - cases.append("_swift_js_push_tag(Int32(\(caseIndex)))") + printer.write("case .\(enumCase.name):") + printer.indent { + printer.write("_swift_js_push_tag(Int32(\(caseIndex)))") + } } else { let pattern = enumCase.associatedValues.enumerated() .map { index, associatedValue in "let \(associatedValue.label ?? "param\(index)")" } .joined(separator: ", ") - cases.append("case .\(enumCase.name)(\(pattern)):") - let payloadCode = generatePayloadPushingCode(associatedValues: enumCase.associatedValues) - cases.append(contentsOf: payloadCode) - // Push tag AFTER payloads so it's popped first (LIFO) by the JS lift function. - // This ensures nested enum tags don't overwrite the outer tag. - cases.append("_swift_js_push_tag(Int32(\(caseIndex)))") + printer.write("case .\(enumCase.name)(\(pattern)):") + printer.indent { + generatePayloadPushingCode(printer: printer, associatedValues: enumCase.associatedValues) + // Push tag AFTER payloads so it's popped first (LIFO) by the JS lift function. + // This ensures nested enum tags don't overwrite the outer tag. + printer.write("_swift_js_push_tag(Int32(\(caseIndex)))") + } } } - return cases } } @@ -1367,40 +1409,60 @@ struct StructCodegen { func renderStructHelpers(_ structDef: ExportedStruct) -> [DeclSyntax] { let typeName = structDef.swiftCallName - let liftCode = generateStructLiftCode(structDef: structDef) - let lowerCode = generateStructLowerCode(structDef: structDef) - let accessControl = structDef.explicitAccessControl.map { "\($0) " } ?? "" let lowerExternName = "swift_js_struct_lower_\(structDef.name)" let liftExternName = "swift_js_struct_lift_\(structDef.name)" let lowerFunctionName = "_bjs_struct_lower_\(structDef.name)" let liftFunctionName = "_bjs_struct_lift_\(structDef.name)" - let bridgedStructExtension: DeclSyntax = """ - extension \(raw: typeName): _BridgedSwiftStruct { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter() -> \(raw: typeName) { - \(raw: liftCode.joined(separator: "\n")) - } - - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { - \(raw: lowerCode.joined(separator: "\n")) - } - - \(raw: accessControl)init(unsafelyCopying jsObject: JSObject) { - let __bjs_cleanupId = \(raw: lowerFunctionName)(jsObject.bridgeJSLowerParameter()) - defer { _swift_js_struct_cleanup(__bjs_cleanupId) } - self = Self.bridgeJSLiftParameter() - } + let printer = CodeFragmentPrinter() + printer.write("extension \(typeName): _BridgedSwiftStruct {") + printer.indent { + printer.write("@_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter() -> \(typeName) {") + printer.indent { + printer.write(lines: generateStructLiftCode(structDef: structDef)) + } + printer.write("}") + printer.nextLine() - \(raw: accessControl)func toJSObject() -> JSObject { - let __bjs_self = self - __bjs_self.bridgeJSLowerReturn() - return JSObject(id: UInt32(bitPattern: \(raw: liftFunctionName)())) - } + printer.write("@_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() {") + printer.indent { + printer.write(lines: generateStructLowerCode(structDef: structDef)) } - """ + printer.write("}") + printer.nextLine() + + let accessControl = structDef.explicitAccessControl.map { "\($0) " } ?? "" + printer.write( + multilineString: """ + \(accessControl)init(unsafelyCopying jsObject: JSObject) { + let __bjs_cleanupId = \(lowerFunctionName)(jsObject.bridgeJSLowerParameter()) + defer { + _swift_js_struct_cleanup(__bjs_cleanupId) + } + self = Self.bridgeJSLiftParameter() + } + """ + ) + printer.nextLine() + + printer.write( + multilineString: """ + \(accessControl)func toJSObject() -> JSObject { + let __bjs_self = self + __bjs_self.bridgeJSLowerReturn() + return JSObject(id: UInt32(bitPattern: \(liftFunctionName)())) + } + """ + ) + } + printer.write("}") - let lowerExternDecl = Self.renderStructExtern( + let bridgedStructExtension: DeclSyntax = "\(raw: printer.lines.joined(separator: "\n"))" + + let lowerExternDeclPrinter = CodeFragmentPrinter() + Self.renderStructExtern( + printer: lowerExternDeclPrinter, externName: lowerExternName, functionName: lowerFunctionName, signature: SwiftSignatureBuilder.buildABIFunctionSignature( @@ -1408,7 +1470,9 @@ struct StructCodegen { returnType: .i32 ) ) - let liftExternDecl = Self.renderStructExtern( + let liftExternDeclPrinter = CodeFragmentPrinter() + Self.renderStructExtern( + printer: liftExternDeclPrinter, externName: liftExternName, functionName: liftFunctionName, signature: SwiftSignatureBuilder.buildABIFunctionSignature( @@ -1417,39 +1481,39 @@ struct StructCodegen { ) ) - return [bridgedStructExtension, lowerExternDecl, liftExternDecl] + return [ + bridgedStructExtension, "\(raw: lowerExternDeclPrinter.lines.joined(separator: "\n"))", + "\(raw: liftExternDeclPrinter.lines.joined(separator: "\n"))", + ] } private static func renderStructExtern( + printer: CodeFragmentPrinter, externName: String, functionName: String, - signature: FunctionSignatureSyntax - ) -> DeclSyntax { - let externFuncDecl = SwiftCodePattern.buildExternFunctionDecl( - moduleName: "bjs", - abiName: externName, - functionName: functionName, - signature: signature - ) - - let stubFuncDecl = FunctionDeclSyntax( - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.fileprivate)) + signature: String + ) { + SwiftCodePattern.buildWasmConditionalCompilationDecls( + printer: printer, + wasmDecl: { printer in + SwiftCodePattern.buildExternFunctionDecl( + printer: printer, + moduleName: "bjs", + abiName: externName, + functionName: functionName, + signature: signature + ) }, - funcKeyword: .keyword(.func), - name: .identifier(functionName), - signature: signature, - body: CodeBlockSyntax { - "fatalError(\"Only available on WebAssembly\")" + elseDecl: { printer in + printer.write( + multilineString: """ + fileprivate func \(functionName)\(signature) { + fatalError("Only available on WebAssembly") + } + """ + ) } ) - - return DeclSyntax( - SwiftCodePattern.buildWasmConditionalCompilationDecls( - wasmDecl: DeclSyntax(externFuncDecl), - elseDecl: DeclSyntax(stubFuncDecl) - ) - ) } private func generateStructLiftCode(structDef: ExportedStruct) -> [String] { @@ -1469,7 +1533,7 @@ struct StructCodegen { } private func generateStructLowerCode(structDef: ExportedStruct) -> [String] { - var lines: [String] = [] + let printer = CodeFragmentPrinter() let instanceProps = structDef.properties.filter { !$0.isStatic } for property in instanceProps { @@ -1479,12 +1543,10 @@ struct StructCodegen { accessor: accessor, varPrefix: property.name ) - for stmt in statements { - lines.append(stmt.description) - } + printer.write(multilineString: CodeBlockItemListSyntax(statements).description) } - return lines + return printer.lines } } @@ -1495,7 +1557,7 @@ struct ProtocolCodegen { let wrapperName = "Any\(proto.name)" let protocolName = proto.name - var methodDecls: [DeclSyntax] = [] + var methodDecls: [CodeFragmentPrinter] = [] var externDecls: [DeclSyntax] = [] for method in proto.methods { @@ -1523,72 +1585,72 @@ struct ProtocolCodegen { abiParameters: builder.abiParameterSignatures, returnType: builder.abiReturnType ) - let externFuncDecl = SwiftCodePattern.buildExternFunctionDecl( + let externDeclPrinter = CodeFragmentPrinter() + SwiftCodePattern.buildExternFunctionDecl( + printer: externDeclPrinter, moduleName: moduleName, abiName: method.abiName, functionName: "_extern_\(method.name)", signature: externSignature ) - externDecls.append(DeclSyntax(externFuncDecl)) - let methodImplementation = DeclSyntax( - FunctionDeclSyntax( - name: .identifier(method.name), - signature: signature, - body: builder.getBody() - ) - ) - - methodDecls.append(methodImplementation) + externDecls.append(DeclSyntax("\(raw: externDeclPrinter.lines.joined(separator: "\n"))")) + let methodImplPrinter = CodeFragmentPrinter() + methodImplPrinter.write("func \(method.name)\(signature) {") + methodImplPrinter.indent { + methodImplPrinter.write(lines: builder.body.lines) + } + methodImplPrinter.write("}") + methodDecls.append(methodImplPrinter) } - var propertyDecls: [DeclSyntax] = [] + var propertyDecls: [CodeFragmentPrinter] = [] for property in proto.properties { - let (propertyImpl, propertyExternDecls) = try renderProtocolProperty( + let propertyDeclPrinter = CodeFragmentPrinter() + let propertyExternDecls = try renderProtocolProperty( + printer: propertyDeclPrinter, property: property, protocolName: protocolName, moduleName: moduleName ) - propertyDecls.append(propertyImpl) + propertyDecls.append(propertyDeclPrinter) externDecls.append(contentsOf: propertyExternDecls) } - let structDecl = StructDeclSyntax( - name: .identifier(wrapperName), - inheritanceClause: InheritanceClauseSyntax( - inheritedTypesBuilder: { - InheritedTypeSyntax(type: IdentifierTypeSyntax(name: .identifier(protocolName))) - InheritedTypeSyntax(type: IdentifierTypeSyntax(name: .identifier("_BridgedSwiftProtocolWrapper"))) - } - ), - memberBlockBuilder: { - DeclSyntax( - """ - let jsObject: JSObject - """ - ).with(\.trailingTrivia, .newlines(2)) + let structDeclPrinter = CodeFragmentPrinter() + structDeclPrinter.write("struct \(wrapperName): \(protocolName), _BridgedSwiftProtocolWrapper {") + structDeclPrinter.indent { + structDeclPrinter.write("let jsObject: JSObject") + structDeclPrinter.nextLine() - for decl in methodDecls + propertyDecls { - decl.with(\.trailingTrivia, .newlines(2)) - } + for methodDecl in methodDecls { + structDeclPrinter.write(lines: methodDecl.lines) + structDeclPrinter.nextLine() + } - DeclSyntax( - """ + for decl in propertyDecls { + structDeclPrinter.write(lines: decl.lines) + structDeclPrinter.nextLine() + } + structDeclPrinter.write( + multilineString: """ static func bridgeJSLiftParameter(_ value: Int32) -> Self { - return \(raw: wrapperName)(jsObject: JSObject(id: UInt32(bitPattern: value))) + return \(wrapperName)(jsObject: JSObject(id: UInt32(bitPattern: value))) } """ - ) - } - ) - return [DeclSyntax(structDecl)] + externDecls + ) + } + structDeclPrinter.write("}") + + return ["\(raw: structDeclPrinter.lines.joined(separator: "\n"))"] + externDecls } private func renderProtocolProperty( + printer: CodeFragmentPrinter, property: ExportedProtocolProperty, protocolName: String, moduleName: String - ) throws -> (propertyDecl: DeclSyntax, externDecls: [DeclSyntax]) { + ) throws -> [DeclSyntax] { let getterAbiName = ABINameGenerator.generateABIName( baseName: property.name, operation: "get", @@ -1614,37 +1676,27 @@ struct ProtocolCodegen { abiParameters: getterBuilder.abiParameterSignatures, returnType: getterBuilder.abiReturnType ) - let getterExternDecl = SwiftCodePattern.buildExternFunctionDecl( + let getterExternDeclPrinter = CodeFragmentPrinter() + SwiftCodePattern.buildExternFunctionDecl( + printer: getterExternDeclPrinter, moduleName: moduleName, abiName: getterAbiName, functionName: getterAbiName, signature: getterExternSignature ) + let getterExternDecl = DeclSyntax("\(raw: getterExternDeclPrinter.lines.joined(separator: "\n"))") + var externDecls: [DeclSyntax] = [getterExternDecl] + + printer.write("var \(property.name): \(property.type.swiftType) {") + try printer.indent { + printer.write("get {") + printer.indent { + printer.write(lines: getterBuilder.body.lines) + } + printer.write("}") + + if property.isReadonly { return } - if property.isReadonly { - let propertyDecl = VariableDeclSyntax( - bindingSpecifier: .keyword(.var), - bindings: PatternBindingListSyntax { - PatternBindingSyntax( - pattern: IdentifierPatternSyntax(identifier: .identifier(property.name)), - typeAnnotation: TypeAnnotationSyntax( - type: IdentifierTypeSyntax(name: .identifier(property.type.swiftType)) - ), - accessorBlock: AccessorBlockSyntax( - accessors: .accessors( - AccessorDeclListSyntax { - AccessorDeclSyntax( - accessorSpecifier: .keyword(.get), - body: getterBuilder.getBody() - ) - } - ) - ) - ) - } - ) - return (DeclSyntax(propertyDecl), [DeclSyntax(getterExternDecl)]) - } else { let setterBuilder = ImportTS.CallJSEmission( moduleName: moduleName, abiName: setterAbiName, @@ -1659,40 +1711,26 @@ struct ProtocolCodegen { abiParameters: setterBuilder.abiParameterSignatures, returnType: setterBuilder.abiReturnType ) - let setterExternDecl = SwiftCodePattern.buildExternFunctionDecl( + let setterExternDeclPrinter = CodeFragmentPrinter() + SwiftCodePattern.buildExternFunctionDecl( + printer: setterExternDeclPrinter, moduleName: moduleName, abiName: setterAbiName, functionName: setterAbiName, signature: setterExternSignature ) + let setterExternDecl = DeclSyntax("\(raw: setterExternDeclPrinter.lines.joined(separator: "\n"))") + externDecls.append(setterExternDecl) - let propertyDecl = VariableDeclSyntax( - bindingSpecifier: .keyword(.var), - bindings: PatternBindingListSyntax { - PatternBindingSyntax( - pattern: IdentifierPatternSyntax(identifier: .identifier(property.name)), - typeAnnotation: TypeAnnotationSyntax( - type: IdentifierTypeSyntax(name: .identifier(property.type.swiftType)) - ), - accessorBlock: AccessorBlockSyntax( - accessors: .accessors( - AccessorDeclListSyntax { - AccessorDeclSyntax( - accessorSpecifier: .keyword(.get), - body: getterBuilder.getBody() - ) - AccessorDeclSyntax( - accessorSpecifier: .keyword(.set), - body: setterBuilder.getBody() - ) - } - ) - ) - ) - } - ) - return (DeclSyntax(propertyDecl), [DeclSyntax(getterExternDecl), DeclSyntax(setterExternDecl)]) + printer.write("set {") + printer.indent { + printer.write(lines: setterBuilder.body.lines) + } + printer.write("}") } + printer.write("}") + + return externDecls } } diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift index d5cc945ee..8eead20b7 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift @@ -85,16 +85,16 @@ public struct ImportTS { let moduleName: String let context: BridgeContext - var body: [CodeBlockItemSyntax] = [] - var abiParameterForwardings: [LabeledExprSyntax] = [] + var body = CodeFragmentPrinter() + var abiParameterForwardings: [String] = [] var abiParameterSignatures: [(name: String, type: WasmCoreType)] = [] var abiReturnType: WasmCoreType? // Track destructured variable names for multiple lowered parameters var destructuredVarNames: [String] = [] // Stack-lowered parameters should be evaluated in reverse order to match LIFO stacks - var stackLoweringStmts: [CodeBlockItemSyntax] = [] + var stackLoweringStmts: [String] = [] // Values to extend lifetime during call - var valuesToExtendLifetimeDuringCall: [ExprSyntax] = [] + var valuesToExtendLifetimeDuringCall: [String] = [] init(moduleName: String, abiName: String, context: BridgeContext = .importTS) { self.moduleName = moduleName @@ -108,34 +108,17 @@ public struct ImportTS { switch param.type { case .closure(let signature, useJSTypedClosure: false): let jsTypedClosureType = BridgeType.closure(signature, useJSTypedClosure: true).swiftType - body.append("let \(raw: param.name) = \(raw: jsTypedClosureType)(\(raw: param.name))") + body.write("let \(param.name) = \(jsTypedClosureType)(\(param.name))") // The just created JSObject is not owned by the caller unlike those passed in parameters, // so we need to extend its lifetime during the call to ensure the JSObject.id is valid. - valuesToExtendLifetimeDuringCall.append("\(raw: param.name)") + valuesToExtendLifetimeDuringCall.append(param.name) default: break } let initializerExpr = ExprSyntax("\(raw: param.name).bridgeJSLowerParameter()") if loweringInfo.loweredParameters.isEmpty { - let stmt = CodeBlockItemSyntax( - item: .decl( - DeclSyntax( - VariableDeclSyntax( - bindingSpecifier: .keyword(.let), - bindings: PatternBindingListSyntax { - PatternBindingSyntax( - pattern: PatternSyntax( - IdentifierPatternSyntax(identifier: .wildcardToken()) - ), - initializer: InitializerClauseSyntax(value: initializerExpr) - ) - } - ) - ) - ) - ) - stackLoweringStmts.insert(stmt, at: 0) + stackLoweringStmts.insert("let _ = \(initializerExpr)", at: 0) return } @@ -145,40 +128,14 @@ public struct ImportTS { } // Always add destructuring statement to body (unified for single and multiple) - let pattern: PatternSyntax + let pattern: String if destructuredNames.count == 1 { - pattern = PatternSyntax(IdentifierPatternSyntax(identifier: .identifier(destructuredNames[0]))) + pattern = destructuredNames[0] } else { - pattern = PatternSyntax( - TuplePatternSyntax { - for name in destructuredNames { - TuplePatternElementSyntax( - pattern: IdentifierPatternSyntax(identifier: .identifier(name)) - ) - } - } - ) + pattern = "(" + destructuredNames.joined(separator: ", ") + ")" } - body.append( - CodeBlockItemSyntax( - item: .decl( - DeclSyntax( - VariableDeclSyntax( - bindingSpecifier: .keyword(.let), - bindings: PatternBindingListSyntax { - PatternBindingSyntax( - pattern: pattern, - initializer: InitializerClauseSyntax( - value: initializerExpr - ) - ) - } - ) - ) - ) - ) - ) + body.write("let \(pattern) = \(initializerExpr)") destructuredVarNames.append(contentsOf: destructuredNames) // Add to signatures and forwardings (unified for both single and multiple) @@ -194,63 +151,36 @@ public struct ImportTS { // Always use destructured variable in call without labels // Swift allows omitting labels when they match parameter names - let callExpr = ExprSyntax("\(raw: destructuredNames[index])") - abiParameterForwardings.append( - LabeledExprSyntax(expression: callExpr) - ) + abiParameterForwardings.append(destructuredNames[index]) } } func call(returnType: BridgeType) throws { - let liftingInfo = try returnType.liftingReturnInfo(context: context) - body.append(contentsOf: stackLoweringStmts) - - var callExpr = FunctionCallExprSyntax( - calledExpression: ExprSyntax("\(raw: abiName)"), - leftParen: .leftParenToken(), - arguments: LabeledExprListSyntax { - for forwarding in abiParameterForwardings { - forwarding - } - }, - rightParen: .rightParenToken() - ) + let liftingInfo: BridgeType.LiftingReturnInfo = try returnType.liftingReturnInfo(context: context) + for stmt in stackLoweringStmts { + body.write(stmt.description) + } + + let assign = + (returnType == .void || returnType.usesSideChannelForOptionalReturn() || liftingInfo.valueToLift == nil) + ? "" : "let ret = " + let callExpr = "\(abiName)(\(abiParameterForwardings.joined(separator: ", ")))" if !valuesToExtendLifetimeDuringCall.isEmpty { - callExpr = FunctionCallExprSyntax( - calledExpression: ExprSyntax("withExtendedLifetime"), - leftParen: .leftParenToken(), - arguments: LabeledExprListSyntax { - LabeledExprSyntax( - expression: TupleExprSyntax( - elements: LabeledExprListSyntax { - for value in valuesToExtendLifetimeDuringCall { - LabeledExprSyntax(expression: value) - } - } - ) - ) - }, - rightParen: .rightParenToken(), - trailingClosure: ClosureExprSyntax( - leftBrace: .leftBraceToken(), - statements: CodeBlockItemListSyntax { - CodeBlockItemSyntax(item: .stmt(StmtSyntax(ExpressionStmtSyntax(expression: callExpr)))) - }, - rightBrace: .rightBraceToken() - ) + body.write( + "\(assign)withExtendedLifetime((\(valuesToExtendLifetimeDuringCall.joined(separator: ", ")))) {" ) - } - - if returnType == .void || returnType.usesSideChannelForOptionalReturn() || liftingInfo.valueToLift == nil { - body.append(CodeBlockItemSyntax(item: .stmt(StmtSyntax(ExpressionStmtSyntax(expression: callExpr))))) + body.indent { + body.write(callExpr) + } + body.write("}") } else { - body.append("let ret = \(raw: callExpr)") + body.write("\(assign)\(callExpr)") } // Add exception check for ImportTS context if context == .importTS { - body.append("if let error = _swift_js_take_exception() { throw error }") + body.write("if let error = _swift_js_take_exception() { throw error }") } } @@ -265,54 +195,30 @@ public struct ImportTS { if returnType.usesSideChannelForOptionalReturn() { // Side channel returns: extern function returns Void, value is retrieved via side channel abiReturnType = nil - body.append( - CodeBlockItemSyntax( - item: .stmt( - StmtSyntax( - ReturnStmtSyntax( - expression: ExprSyntax( - "\(raw: returnType.swiftType).bridgeJSLiftReturnFromSideChannel()" - ) - ) - ) - ) - ) - ) + body.write("return \(returnType.swiftType).bridgeJSLiftReturnFromSideChannel()") } else { abiReturnType = liftingInfo.valueToLift - let liftExpr: ExprSyntax + let liftExpr: String switch returnType { case .closure(let signature, _): - liftExpr = ExprSyntax("_BJS_Closure_\(raw: signature.mangleName).bridgeJSLift(ret)") + liftExpr = "_BJS_Closure_\(signature.mangleName).bridgeJSLift(ret)" default: if liftingInfo.valueToLift != nil { - liftExpr = "\(raw: returnType.swiftType).bridgeJSLiftReturn(ret)" + liftExpr = "\(returnType.swiftType).bridgeJSLiftReturn(ret)" } else { - liftExpr = "\(raw: returnType.swiftType).bridgeJSLiftReturn()" + liftExpr = "\(returnType.swiftType).bridgeJSLiftReturn()" } } - body.append( - CodeBlockItemSyntax( - item: .stmt( - StmtSyntax( - ReturnStmtSyntax(expression: liftExpr) - ) - ) - ) - ) + body.write("return \(liftExpr)") } } - func getBody() -> CodeBlockSyntax { - return CodeBlockSyntax(statements: CodeBlockItemListSyntax(body)) - } - func assignThis(returnType: BridgeType) { guard case .jsObject = returnType else { preconditionFailure("assignThis can only be called with a jsObject return type") } abiReturnType = .i32 - body.append("self.jsObject = JSObject(id: UInt32(bitPattern: ret))") + body.write("self.jsObject = JSObject(id: UInt32(bitPattern: ret))") } func renderImportDecl() -> DeclSyntax { @@ -321,40 +227,33 @@ public struct ImportTS { returnType: abiReturnType ) - // Build extern function declaration (no body) - let externFuncDecl = FunctionDeclSyntax( - attributes: SwiftCodePattern.buildExternAttribute(moduleName: moduleName, abiName: abiName), - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.fileprivate)).with(\.trailingTrivia, .space) - }, - funcKeyword: .keyword(.func).with(\.trailingTrivia, .space), - name: .identifier(abiName), - signature: signature - ) - - // Build stub function declaration (with fatalError body) - let stubFuncDecl = FunctionDeclSyntax( - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.fileprivate)).with(\.trailingTrivia, .space) + let printer = CodeFragmentPrinter() + SwiftCodePattern.buildWasmConditionalCompilationDecls( + printer: printer, + wasmDecl: { printer in + SwiftCodePattern.buildExternFunctionDecl( + printer: printer, + moduleName: moduleName, + abiName: abiName, + functionName: abiName, + signature: signature + ) }, - funcKeyword: .keyword(.func).with(\.trailingTrivia, .space), - name: .identifier(abiName), - signature: signature, - body: CodeBlockSyntax { - "fatalError(\"Only available on WebAssembly\")" + elseDecl: { printer in + printer.write( + multilineString: """ + fileprivate func \(abiName)\(signature) { + fatalError("Only available on WebAssembly") + } + """ + ) } ) - - // Use conditional compilation helper - return DeclSyntax( - SwiftCodePattern.buildWasmConditionalCompilationDecls( - wasmDecl: DeclSyntax(externFuncDecl), - elseDecl: DeclSyntax(stubFuncDecl) - ) - ) + return "\(raw: printer.lines.joined(separator: "\n"))" } func renderThunkDecl(name: String, parameters: [Parameter], returnType: BridgeType) -> DeclSyntax { + let printer = CodeFragmentPrinter() let effects = Effects(isAsync: false, isThrows: true) let signature = SwiftSignatureBuilder.buildFunctionSignature( parameters: parameters, @@ -362,35 +261,28 @@ public struct ImportTS { effects: effects, useWildcardLabels: true ) - return DeclSyntax( - FunctionDeclSyntax( - name: .identifier(name.backtickIfNeeded()), - signature: signature, - body: CodeBlockSyntax { - body - } - ) - ) + printer.write("func \(name.backtickIfNeeded())\(signature) {") + printer.indent { + printer.write(lines: body.lines) + } + printer.write("}") + return "\(raw: printer.lines.joined(separator: "\n"))" } func renderConstructorDecl(parameters: [Parameter]) -> DeclSyntax { + let printer = CodeFragmentPrinter() let effects = Effects(isAsync: false, isThrows: true) - // Constructors don't have return types, so build signature without return clause - let signature = FunctionSignatureSyntax( - parameterClause: SwiftSignatureBuilder.buildParameterClause( - parameters: parameters, - useWildcardLabels: true - ), - effectSpecifiers: SwiftSignatureBuilder.buildEffectSpecifiers(effects: effects) - ) - return DeclSyntax( - InitializerDeclSyntax( - signature: signature, - bodyBuilder: { - body - } - ) + let parameterClause = SwiftSignatureBuilder.buildParameterClause( + parameters: parameters, + useWildcardLabels: true ) + let effectSpecifiers = SwiftSignatureBuilder.buildEffectSpecifiers(effects: effects) + printer.write("init\(parameterClause)\(effectSpecifiers.map { " \($0)" } ?? "") {") + printer.indent { + printer.write(lines: body.lines) + } + printer.write("}") + return "\(raw: printer.lines.joined(separator: "\n"))" } } @@ -600,12 +492,17 @@ struct SwiftSignatureBuilder { returnType: BridgeType, effects: Effects? = nil, useWildcardLabels: Bool = false - ) -> FunctionSignatureSyntax { - return FunctionSignatureSyntax( - parameterClause: buildParameterClause(parameters: parameters, useWildcardLabels: useWildcardLabels), - effectSpecifiers: effects.flatMap { buildEffectSpecifiers(effects: $0) }, - returnClause: buildReturnClause(returnType: returnType) - ) + ) -> String { + let parameterClause = buildParameterClause(parameters: parameters, useWildcardLabels: useWildcardLabels) + let effectSpecifiers = effects.flatMap { buildEffectSpecifiers(effects: $0) } + let returnClause = buildReturnClause(returnType: returnType) + var out = "" + out += parameterClause + if let effectSpecifiers { + out += " \(effectSpecifiers)" + } + out += returnClause + return out } /// Builds a function signature for ABI/extern functions using WasmCoreType parameters @@ -619,12 +516,14 @@ struct SwiftSignatureBuilder { abiParameters: [(name: String, type: WasmCoreType)], returnType: WasmCoreType?, effects: Effects? = nil - ) -> FunctionSignatureSyntax { - return FunctionSignatureSyntax( - parameterClause: buildABIParameterClause(abiParameters: abiParameters), - effectSpecifiers: effects.flatMap { buildEffectSpecifiers(effects: $0) }, - returnClause: buildABIReturnClause(returnType: returnType) - ) + ) -> String { + var out = "" + out += buildABIParameterClause(abiParameters: abiParameters) + if let effects = effects, let effectSpecifiers = buildEffectSpecifiers(effects: effects) { + out += " \(effectSpecifiers)" + } + out += buildABIReturnClause(returnType: returnType) + return out } /// Builds a parameter clause from an array of Parameter structs @@ -639,48 +538,28 @@ struct SwiftSignatureBuilder { static func buildParameterClause( parameters: [Parameter], useWildcardLabels: Bool = false - ) -> FunctionParameterClauseSyntax { - return FunctionParameterClauseSyntax(parametersBuilder: { - for param in parameters { - let paramTypeSyntax = buildParameterTypeSyntax(from: param.type) - if useWildcardLabels { - // Always use wildcard labels: "_ name: Type" - FunctionParameterSyntax( - firstName: .wildcardToken(), - secondName: .identifier(param.name), - colon: .colonToken(), - type: paramTypeSyntax - ) - } else { - let label = param.label ?? param.name - if label == param.name { - // External label same as parameter name: "count: Int" - FunctionParameterSyntax( - firstName: .identifier(label), - secondName: nil, - colon: .colonToken(), - type: paramTypeSyntax - ) - } else if param.label == nil { - // No label specified: use wildcard "_ name: Type" - FunctionParameterSyntax( - firstName: .wildcardToken(), - secondName: .identifier(param.name), - colon: .colonToken(), - type: paramTypeSyntax - ) - } else { - // External label differs: "label count: Int" - FunctionParameterSyntax( - firstName: .identifier(label), - secondName: .identifier(param.name), - colon: .colonToken(), - type: paramTypeSyntax - ) - } - } + ) -> String { + var out = "(" + out += parameters.map { param in + let label = param.label ?? param.name + let paramType = buildParameterTypeSyntax(from: param.type) + + if useWildcardLabels { + // Always use wildcard labels: "_ name: Type" + return "_ \(param.name): \(paramType)" + } else if label == param.name { + // External label same as parameter name: "count: Int" + return "\(param.name): \(paramType)" + } else if param.label == nil { + // No label specified: use wildcard "_ name: Type" + return "_ \(param.name): \(paramType)" + } else { + // External label differs: "label count: Int" + return "\(label) \(param.name): \(paramType)" } - }) + }.joined(separator: ", ") + out += ")" + return out } /// Builds a parameter clause for ABI/extern functions @@ -688,77 +567,56 @@ struct SwiftSignatureBuilder { /// All parameters use wildcard labels: "_ name: Type" static func buildABIParameterClause( abiParameters: [(name: String, type: WasmCoreType)] - ) -> FunctionParameterClauseSyntax { - return FunctionParameterClauseSyntax(parametersBuilder: { - for param in abiParameters { - FunctionParameterSyntax( - firstName: .wildcardToken().with(\.trailingTrivia, .space), - secondName: .identifier(param.name), - type: IdentifierTypeSyntax(name: .identifier(param.type.swiftType)) - ) - } - }) + ) -> String { + "(" + + abiParameters.map { param in + "_ \(param.name): \(param.type.swiftType)" + }.joined(separator: ", ") + ")" } /// Builds a return clause from a BridgeType /// /// Always returns a ReturnClauseSyntax, including for Void types /// (to match original behavior that explicitly includes "-> Void") - static func buildReturnClause(returnType: BridgeType) -> ReturnClauseSyntax? { - return ReturnClauseSyntax( - arrow: .arrowToken(), - type: buildTypeSyntax(from: returnType) - ) + static func buildReturnClause(returnType: BridgeType) -> String { + return " -> \(returnType.swiftType)" } /// Builds a return clause for ABI/extern functions /// /// Returns nil for Void (when returnType is nil), otherwise returns a ReturnClauseSyntax - static func buildABIReturnClause(returnType: WasmCoreType?) -> ReturnClauseSyntax? { + static func buildABIReturnClause(returnType: WasmCoreType?) -> String { guard let returnType = returnType else { - return ReturnClauseSyntax( - arrow: .arrowToken(), - type: IdentifierTypeSyntax(name: .identifier("Void")) - ) + return " -> Void" } - return ReturnClauseSyntax( - arrow: .arrowToken(), - type: IdentifierTypeSyntax(name: .identifier(returnType.swiftType)) - ) + return " -> \(returnType.swiftType)" } /// Builds effect specifiers (async/throws) from an Effects struct /// /// Uses JSException as the thrown error type for throws clauses - static func buildEffectSpecifiers(effects: Effects) -> FunctionEffectSpecifiersSyntax? { + static func buildEffectSpecifiers(effects: Effects) -> String? { guard effects.isAsync || effects.isThrows else { return nil } - return FunctionEffectSpecifiersSyntax( - asyncSpecifier: effects.isAsync ? .keyword(.async) : nil, - throwsClause: effects.isThrows - ? ThrowsClauseSyntax( - throwsSpecifier: .keyword(.throws), - leftParen: .leftParenToken(), - type: IdentifierTypeSyntax(name: .identifier("JSException")), - rightParen: .rightParenToken() - ) : nil - ) + var items: [String] = [] + if effects.isAsync { items.append("async") } + if effects.isThrows { items.append("throws(JSException)") } + return items.joined(separator: " ") } /// Builds a TypeSyntax node from a BridgeType /// /// Converts BridgeType to its Swift type representation as a TypeSyntax node - static func buildTypeSyntax(from type: BridgeType) -> TypeSyntax { - let identifierType = IdentifierTypeSyntax(name: .identifier(type.swiftType)) - return TypeSyntax(identifierType) + static func buildTypeSyntax(from type: BridgeType) -> String { + return type.swiftType } /// Builds a parameter type syntax from a BridgeType. - static func buildParameterTypeSyntax(from type: BridgeType) -> TypeSyntax { + static func buildParameterTypeSyntax(from type: BridgeType) -> String { switch type { case .closure(_, useJSTypedClosure: false): - return TypeSyntax("@escaping \(raw: type.swiftType)") + return "@escaping \(type.swiftType)" default: return buildTypeSyntax(from: type) } @@ -768,167 +626,69 @@ struct SwiftSignatureBuilder { enum SwiftCodePattern { /// Builds a conditional compilation block with #if arch(wasm32) and #else fatalError static func buildWasmConditionalCompilation( - wasmBody: CodeBlockItemListSyntax - ) -> IfConfigDeclSyntax { - return IfConfigDeclSyntax( - clauses: IfConfigClauseListSyntax { - IfConfigClauseSyntax( - poundKeyword: .poundIfToken(), - condition: ExprSyntax("arch(wasm32)"), - elements: .statements(wasmBody) - ) - IfConfigClauseSyntax( - poundKeyword: .poundElseToken(), - elements: .statements( - CodeBlockItemListSyntax { - "fatalError(\"Only available on WebAssembly\")" - } - ) - ) - } - ) + printer: CodeFragmentPrinter, + wasmBody: (_ printer: CodeFragmentPrinter) -> Void + ) { + printer.write("#if arch(wasm32)") + wasmBody(printer) + printer.write("#else") + printer.write("fatalError(\"Only available on WebAssembly\")") + printer.write("#endif") } /// Builds a conditional compilation block with #if arch(wasm32) and #else for declarations static func buildWasmConditionalCompilationDecls( - wasmDecl: DeclSyntax, - elseDecl: DeclSyntax - ) -> IfConfigDeclSyntax { - return IfConfigDeclSyntax( - clauses: IfConfigClauseListSyntax { - IfConfigClauseSyntax( - poundKeyword: .poundIfToken(), - condition: ExprSyntax("arch(wasm32)"), - elements: .statements( - CodeBlockItemListSyntax { - CodeBlockItemSyntax(item: .decl(wasmDecl)) - } - ) - ) - IfConfigClauseSyntax( - poundKeyword: .poundElseToken(), - elements: .statements( - CodeBlockItemListSyntax { - CodeBlockItemSyntax(item: .decl(elseDecl)) - } - ) - ) - } - ) + printer: CodeFragmentPrinter, + wasmDecl: (_ printer: CodeFragmentPrinter) -> Void, + elseDecl: (_ printer: CodeFragmentPrinter) -> Void + ) { + printer.write("#if arch(wasm32)") + wasmDecl(printer) + printer.write("#else") + elseDecl(printer) + printer.write("#endif") } /// Builds the @_extern attribute for WebAssembly extern function declarations /// Builds an @_extern function declaration (no body, just the declaration) static func buildExternFunctionDecl( + printer: CodeFragmentPrinter, moduleName: String, abiName: String, functionName: String, - signature: FunctionSignatureSyntax - ) -> FunctionDeclSyntax { - return FunctionDeclSyntax( - attributes: buildExternAttribute(moduleName: moduleName, abiName: abiName), - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.fileprivate)) - }, - funcKeyword: .keyword(.func), - name: .identifier(functionName), - signature: signature - ) + signature: String + ) { + printer.write(buildExternAttribute(moduleName: moduleName, abiName: abiName)) + printer.write("fileprivate func \(functionName)\(signature)") } /// Builds the standard @_expose and @_cdecl attributes for WebAssembly-exposed functions - static func buildExposeAttributes(abiName: String) -> AttributeListSyntax { - return AttributeListSyntax { - #if canImport(SwiftSyntax602) - let exposeAttrArgs = AttributeSyntax.Arguments.argumentList( - LabeledExprListSyntax { - LabeledExprSyntax(label: nil, expression: DeclReferenceExprSyntax(baseName: "wasm")) - .with(\.trailingComma, .commaToken()) - LabeledExprSyntax(label: nil, expression: StringLiteralExprSyntax(content: abiName)) - } - ) - let cdeclAttrArgs = AttributeSyntax.Arguments.argumentList( - [ - LabeledExprSyntax(label: nil, expression: StringLiteralExprSyntax(content: abiName)) - ] - ) - #else - let exposeAttrArgs = AttributeSyntax.Arguments.exposeAttributeArguments( - ExposeAttributeArgumentsSyntax( - language: .identifier("wasm"), - comma: .commaToken(), - cxxName: StringLiteralExprSyntax(content: abiName) - ) - ) - let cdeclAttrArgs = AttributeSyntax.Arguments.string(StringLiteralExprSyntax(content: abiName)) - #endif - AttributeSyntax( - attributeName: IdentifierTypeSyntax(name: .identifier("_expose")), - leftParen: .leftParenToken(), - arguments: exposeAttrArgs, - rightParen: .rightParenToken() - ) - .with(\.trailingTrivia, .newline) - - AttributeSyntax( - attributeName: IdentifierTypeSyntax(name: .identifier("_cdecl")), - leftParen: .leftParenToken(), - arguments: cdeclAttrArgs, - rightParen: .rightParenToken() - ) - .with(\.trailingTrivia, .newline) - } + static func buildExposeAttributes(abiName: String) -> String { + return """ + @_expose(wasm, "\(abiName)") + @_cdecl("\(abiName)") + """ } /// Builds a function declaration with @_expose/@_cdecl attributes and conditional compilation static func buildExposedFunctionDecl( abiName: String, - signature: FunctionSignatureSyntax, - body: CodeBlockItemListSyntax - ) -> FunctionDeclSyntax { - let funcBody = CodeBlockSyntax { - buildWasmConditionalCompilation(wasmBody: body) + signature: String, + body: (CodeFragmentPrinter) -> Void + ) -> DeclSyntax { + let printer = CodeFragmentPrinter() + printer.write(buildExposeAttributes(abiName: abiName)) + printer.write("public func _\(abiName)\(signature) {") + printer.indent { + buildWasmConditionalCompilation(printer: printer, wasmBody: body) } - - return FunctionDeclSyntax( - attributes: buildExposeAttributes(abiName: abiName), - modifiers: DeclModifierListSyntax { - DeclModifierSyntax(name: .keyword(.public)) - }, - funcKeyword: .keyword(.func), - name: .identifier("_\(abiName)"), - signature: signature, - body: funcBody - ) + printer.write("}") + return "\(raw: printer.lines.joined(separator: "\n"))" } /// Builds the @_extern attribute for WebAssembly extern function declarations - static func buildExternAttribute(moduleName: String, abiName: String) -> AttributeListSyntax { - return AttributeListSyntax { - AttributeSyntax( - attributeName: IdentifierTypeSyntax(name: .identifier("_extern")), - leftParen: .leftParenToken(), - arguments: .argumentList( - LabeledExprListSyntax { - LabeledExprSyntax( - expression: ExprSyntax("wasm") - ) - LabeledExprSyntax( - label: .identifier("module"), - colon: .colonToken(), - expression: StringLiteralExprSyntax(content: moduleName) - ) - LabeledExprSyntax( - label: .identifier("name"), - colon: .colonToken(), - expression: StringLiteralExprSyntax(content: abiName) - ) - } - ), - rightParen: .rightParenToken() - ) - .with(\.trailingTrivia, .newline) - } + static func buildExternAttribute(moduleName: String, abiName: String) -> String { + return "@_extern(wasm, module: \"\(moduleName)\", name: \"\(abiName)\")" } } diff --git a/Plugins/BridgeJS/Sources/BridgeJSMacros/JSSetterMacro.swift b/Plugins/BridgeJS/Sources/BridgeJSMacros/JSSetterMacro.swift index ba171e8eb..4d32db449 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSMacros/JSSetterMacro.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSMacros/JSSetterMacro.swift @@ -54,7 +54,7 @@ extension JSSetterMacro: BodyMacro { firstName: .wildcardToken(trailingTrivia: .space), secondName: .identifier("value"), colon: .colonToken(trailingTrivia: .space), - type: TypeSyntax(stringLiteral: "<#Type#>") + type: IdentifierTypeSyntax(name: TokenSyntax(.identifier("<#Type#>"), presence: .present)) ) let newClause = FunctionParameterClauseSyntax( leftParen: .leftParenToken(), diff --git a/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift b/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift index ed6fa04f0..68c07f225 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift @@ -60,6 +60,12 @@ public final class CodeFragmentPrinter { self.write(lines: printer.lines) } + public func write(multilineString: String) { + for line in multilineString.split(separator: "\n") { + write(line) + } + } + public func indent() { indentLevel += 1 } diff --git a/Plugins/BridgeJS/Sources/TS2Swift/TS2Swift.swift b/Plugins/BridgeJS/Sources/TS2Swift/TS2Swift.swift index 795b7b390..6337dcf27 100644 --- a/Plugins/BridgeJS/Sources/TS2Swift/TS2Swift.swift +++ b/Plugins/BridgeJS/Sources/TS2Swift/TS2Swift.swift @@ -20,6 +20,12 @@ import BridgeJSCore import BridgeJSSkeleton #endif +#if os(Windows) +let PATH_SEPARATOR: Character = ";" +#else +let PATH_SEPARATOR: Character = ":" +#endif + internal func which( _ executable: String, environment: [String: String] = ProcessInfo.processInfo.environment @@ -39,13 +45,8 @@ internal func which( } } } - let pathSeparator: Character - #if os(Windows) - pathSeparator = ";" - #else - pathSeparator = ":" - #endif - let paths = environment["PATH"]?.split(separator: pathSeparator) ?? [] + + let paths = environment["PATH"]?.split(separator: PATH_SEPARATOR) ?? [] for path in paths { let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable) if checkCandidate(url) { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/TS2SwiftVitestTests.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/TS2SwiftVitestTests.swift index bb3d2e0a4..f658b001f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/TS2SwiftVitestTests.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/TS2SwiftVitestTests.swift @@ -1,5 +1,6 @@ import Foundation import Testing +@testable import TS2Swift /// Runs the TS2Swift JavaScript test suite (Vitest) so that `swift test --package-path ./Plugins/BridgeJS` /// validates both the TypeScript ts2swift output and the Swift codegen. For fast iteration on ts2swift, @@ -17,8 +18,16 @@ import Testing .appendingPathComponent("TS2Swift") .appendingPathComponent("JavaScript") let process = Process() - process.executableURL = URL(fileURLWithPath: "/usr/bin/env") - var arguments = ["npm", "run", "test"] + guard let npmExecutable = which("npm"), let nodeExecutable = which("node") else { + Issue.record("No \"npm\" command found in your system") + return + } + process.executableURL = npmExecutable + var environment = ProcessInfo.processInfo.environment + environment["PATH"] = + "\(nodeExecutable.deletingLastPathComponent().path)\(PATH_SEPARATOR) \(environment["PATH"] ?? "")" + process.environment = environment + var arguments = ["run", "test"] if ProcessInfo.processInfo.environment["UPDATE_SNAPSHOTS"] != nil { arguments.append(contentsOf: ["--", "--update"]) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ArrayTypes.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ArrayTypes.swift index 023caf21f..261c0b066 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ArrayTypes.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ArrayTypes.swift @@ -229,17 +229,19 @@ public func _bjs_processOptionalIntArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -254,17 +256,19 @@ public func _bjs_processOptionalStringArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -281,10 +285,11 @@ public func _bjs_processOptionalArray(_ values: Int32) -> Void { } else { return [Int].bridgeJSLiftParameter() } - }()) + }()) let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") @@ -299,17 +304,19 @@ public func _bjs_processOptionalPointArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -324,17 +331,19 @@ public func _bjs_processOptionalDirectionArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -349,17 +358,19 @@ public func _bjs_processOptionalStatusArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -374,14 +385,15 @@ public func _bjs_processNestedIntArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Int]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Int].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -396,14 +408,15 @@ public func _bjs_processNestedStringArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[String]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([String].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -418,14 +431,15 @@ public func _bjs_processNestedPointArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Point]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Point].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -451,14 +465,15 @@ public func _bjs_processNestedItemArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Item]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Item].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -484,17 +499,19 @@ public func _bjs_processOptionalJSObjectArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -509,14 +526,15 @@ public func _bjs_processNestedJSObjectArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[JSObject]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([JSObject].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Async.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Async.swift index 4c97553de..f5230f213 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Async.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Async.swift @@ -4,7 +4,7 @@ public func _bjs_asyncReturnVoid() -> Int32 { #if arch(wasm32) let ret = JSPromise.async { await asyncReturnVoid() - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -17,7 +17,7 @@ public func _bjs_asyncRoundTripInt(_ v: Int32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripInt(_: Int.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -30,7 +30,7 @@ public func _bjs_asyncRoundTripString(_ vBytes: Int32, _ vLength: Int32) -> Int3 #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripString(_: String.bridgeJSLiftParameter(vBytes, vLength)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -43,7 +43,7 @@ public func _bjs_asyncRoundTripBool(_ v: Int32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripBool(_: Bool.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -56,7 +56,7 @@ public func _bjs_asyncRoundTripFloat(_ v: Float32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripFloat(_: Float.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -69,7 +69,7 @@ public func _bjs_asyncRoundTripDouble(_ v: Float64) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripDouble(_: Double.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -82,7 +82,7 @@ public func _bjs_asyncRoundTripJSObject(_ v: Int32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripJSObject(_: JSObject.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumAssociatedValue.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumAssociatedValue.swift index 26454a2a7..f2029749c 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumAssociatedValue.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumAssociatedValue.swift @@ -309,36 +309,36 @@ extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { case .success(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(0) case .failure(let param0, let param1): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) return Int32(1) case .status(let param0, let param1, let param2): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) let __bjs_isSome_param2 = param2 != nil if let __bjs_unwrapped_param2 = param2 { - __bjs_unwrapped_param2.bridgeJSLowerStackReturn() + __bjs_unwrapped_param2.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param2 ? 1 : 0) return Int32(2) @@ -360,36 +360,36 @@ extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { case .success(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(0)) case .failure(let param0, let param1): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) _swift_js_push_tag(Int32(1)) case .status(let param0, let param1, let param2): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) let __bjs_isSome_param2 = param2 != nil if let __bjs_unwrapped_param2 = param2 { - __bjs_unwrapped_param2.bridgeJSLowerStackReturn() + __bjs_unwrapped_param2.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param2 ? 1 : 0) _swift_js_push_tag(Int32(2)) @@ -474,14 +474,14 @@ extension TypedPayloadResult: _BridgedSwiftAssociatedValueEnum { case .optPrecision(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(2) case .optDirection(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(3) @@ -511,14 +511,14 @@ extension TypedPayloadResult: _BridgedSwiftAssociatedValueEnum { case .optPrecision(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(2)) case .optDirection(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(3)) @@ -618,13 +618,13 @@ extension OptionalAllTypesResult: _BridgedSwiftAssociatedValueEnum { return .optNestedEnum(Optional.bridgeJSLiftParameter()) case 4: return .optArray({ - let __isSome = _swift_js_pop_i32() - if __isSome == 0 { - return Optional<[Int]>.none - } else { - return [Int].bridgeJSLiftParameter() - } - }()) + let __isSome = _swift_js_pop_i32() + if __isSome == 0 { + return Optional<[Int]>.none + } else { + return [Int].bridgeJSLiftParameter() + } +}()) case 5: return .empty default: @@ -639,35 +639,35 @@ extension OptionalAllTypesResult: _BridgedSwiftAssociatedValueEnum { case .optStruct(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(0) case .optClass(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(1) case .optJSObject(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(2) case .optNestedEnum(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) + _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(3) case .optArray(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(4) @@ -691,35 +691,35 @@ extension OptionalAllTypesResult: _BridgedSwiftAssociatedValueEnum { case .optStruct(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(0)) case .optClass(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(1)) case .optJSObject(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(2)) case .optNestedEnum(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) + _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(3)) case .optArray(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(4)) diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ImportedTypeInExportedInterface.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ImportedTypeInExportedInterface.swift index 6d9bee080..cd0f56694 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ImportedTypeInExportedInterface.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/ImportedTypeInExportedInterface.swift @@ -1,8 +1,6 @@ extension FooContainer: _BridgedSwiftStruct { @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter() -> FooContainer { - let optionalFoo = Optional.bridgeJSLiftParameter().map { - Foo(unsafelyWrapping: $0) - } + let optionalFoo = Optional.bridgeJSLiftParameter().map { Foo(unsafelyWrapping: $0) } let foo = Foo(unsafelyWrapping: JSObject.bridgeJSLiftParameter()) return FooContainer(foo: foo, optionalFoo: optionalFoo) } @@ -11,7 +9,7 @@ extension FooContainer: _BridgedSwiftStruct { self.foo.jsObject.bridgeJSLowerStackReturn() let __bjs_isSome_optionalFoo = self.optionalFoo != nil if let __bjs_unwrapped_optionalFoo = self.optionalFoo { - __bjs_unwrapped_optionalFoo.jsObject.bridgeJSLowerStackReturn() + __bjs_unwrapped_optionalFoo.jsObject.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optionalFoo ? 1 : 0) } @@ -82,15 +80,13 @@ public func _bjs_processFooArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Foo] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Foo(unsafelyWrapping: JSObject.bridgeJSLiftParameter())) } __result.reverse() return __result - }()) - ret.map { - $0.jsObject - } .bridgeJSLowerReturn() + }()) + ret.map { $0.jsObject }.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif @@ -104,19 +100,19 @@ public func _bjs_processOptionalFooArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { - __result.append(Optional.bridgeJSLiftParameter().map { - Foo(unsafelyWrapping: $0) - }) + for _ in 0..<__count { + __result.append(Optional.bridgeJSLiftParameter().map { Foo(unsafelyWrapping: $0) }) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.jsObject.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.jsObject.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSValue.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSValue.swift index f3550129c..b4e45bbc9 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSValue.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSValue.swift @@ -41,10 +41,11 @@ public func _bjs_roundTripOptionalJSValueArray(_ values: Int32) -> Void { } else { return [JSValue].bridgeJSLiftParameter() } - }()) + }()) let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift index 3536b53cb..55521dbaf 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift @@ -417,9 +417,7 @@ extension Priority: _BridgedSwiftEnumNoPayload, _BridgedSwiftRawValueEnum { public func _bjs_processDelegates() -> Void { #if arch(wasm32) let ret = processDelegates(_: [AnyMyViewControllerDelegate].bridgeJSLiftParameter()) - ret.map { - $0 as! AnyMyViewControllerDelegate - } .bridgeJSLowerReturn() + ret.map { $0 as! AnyMyViewControllerDelegate }.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif @@ -590,9 +588,7 @@ public func _bjs_MyViewController_delegate_set(_ _self: UnsafeMutableRawPointer, @_cdecl("bjs_MyViewController_secondDelegate_get") public func _bjs_MyViewController_secondDelegate_get(_ _self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = MyViewController.bridgeJSLiftParameter(_self).secondDelegate.flatMap { - $0 as? AnyMyViewControllerDelegate - } + let ret = MyViewController.bridgeJSLiftParameter(_self).secondDelegate.flatMap { $0 as? AnyMyViewControllerDelegate } return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -660,9 +656,7 @@ public func _bjs_DelegateManager_notifyAll(_ _self: UnsafeMutableRawPointer) -> public func _bjs_DelegateManager_delegates_get(_ _self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) let ret = DelegateManager.bridgeJSLiftParameter(_self).delegates - ret.map { - $0 as! AnyMyViewControllerDelegate - } .bridgeJSLowerReturn() + ret.map { $0 as! AnyMyViewControllerDelegate }.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftStruct.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftStruct.swift index ca2467249..1ff0a4ce8 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftStruct.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftStruct.swift @@ -17,12 +17,12 @@ extension DataPoint: _BridgedSwiftStruct { self.label.bridgeJSLowerStackReturn() let __bjs_isSome_optCount = self.optCount != nil if let __bjs_unwrapped_optCount = self.optCount { - __bjs_unwrapped_optCount.bridgeJSLowerStackReturn() + __bjs_unwrapped_optCount.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optCount ? 1 : 0) let __bjs_isSome_optFlag = self.optFlag != nil if let __bjs_unwrapped_optFlag = self.optFlag { - __bjs_unwrapped_optFlag.bridgeJSLowerStackReturn() + __bjs_unwrapped_optFlag.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optFlag ? 1 : 0) } @@ -84,7 +84,7 @@ extension Address: _BridgedSwiftStruct { self.city.bridgeJSLowerStackReturn() let __bjs_isSome_zipCode = self.zipCode != nil if let __bjs_unwrapped_zipCode = self.zipCode { - __bjs_unwrapped_zipCode.bridgeJSLowerStackReturn() + __bjs_unwrapped_zipCode.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_zipCode ? 1 : 0) } @@ -137,7 +137,7 @@ extension Person: _BridgedSwiftStruct { self.address.bridgeJSLowerReturn() let __bjs_isSome_email = self.email != nil if let __bjs_unwrapped_email = self.email { - __bjs_unwrapped_email.bridgeJSLowerStackReturn() + __bjs_unwrapped_email.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_email ? 1 : 0) } @@ -233,7 +233,7 @@ extension Measurement: _BridgedSwiftStruct { self.precision.bridgeJSLowerStackReturn() let __bjs_isSome_optionalPrecision = self.optionalPrecision != nil if let __bjs_unwrapped_optionalPrecision = self.optionalPrecision { - __bjs_unwrapped_optionalPrecision.bridgeJSLowerStackReturn() + __bjs_unwrapped_optionalPrecision.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optionalPrecision ? 1 : 0) } @@ -277,7 +277,6 @@ extension ConfigStruct: _BridgedSwiftStruct { } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { - } init(unsafelyCopying jsObject: JSObject) { @@ -399,7 +398,7 @@ extension Container: _BridgedSwiftStruct { self.object.bridgeJSLowerStackReturn() let __bjs_isSome_optionalObject = self.optionalObject != nil if let __bjs_unwrapped_optionalObject = self.optionalObject { - __bjs_unwrapped_optionalObject.bridgeJSLowerStackReturn() + __bjs_unwrapped_optionalObject.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optionalObject ? 1 : 0) } diff --git a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift index b10a82fdf..af9f15dbc 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift +++ b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift @@ -655,7 +655,7 @@ fileprivate func make_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsS private enum _BJS_Closure_20BridgeJSRuntimeTestsSiSSSd_SS { static func bridgeJSLift(_ callbackId: Int32) -> (Int, String, Double) -> String { let callback = JSObject.bridgeJSLiftParameter(callbackId) - return { [callback] param0, param1, param2 in + return { [callback] (param0, param1, param2) in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() let param0Value = param0.bridgeJSLowerParameter() @@ -714,7 +714,7 @@ fileprivate func make_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsS private enum _BJS_Closure_20BridgeJSRuntimeTestsSiSiSi_Si { static func bridgeJSLift(_ callbackId: Int32) -> (Int, Int, Int) -> Int { let callback = JSObject.bridgeJSLiftParameter(callbackId) - return { [callback] param0, param1, param2 in + return { [callback] (param0, param1, param2) in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() let param0Value = param0.bridgeJSLowerParameter() @@ -773,7 +773,7 @@ fileprivate func make_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsS private enum _BJS_Closure_20BridgeJSRuntimeTestsSiSi_Si { static func bridgeJSLift(_ callbackId: Int32) -> (Int, Int) -> Int { let callback = JSObject.bridgeJSLiftParameter(callbackId) - return { [callback] param0, param1 in + return { [callback] (param0, param1) in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() let param0Value = param0.bridgeJSLowerParameter() @@ -2443,14 +2443,14 @@ extension TypedPayloadResult: _BridgedSwiftAssociatedValueEnum { case .optPrecision(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(2) case .optDirection(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(3) @@ -2480,14 +2480,14 @@ extension TypedPayloadResult: _BridgedSwiftAssociatedValueEnum { case .optPrecision(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(2)) case .optDirection(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(3)) @@ -2815,17 +2815,15 @@ extension OptionalAllTypesResult: _BridgedSwiftAssociatedValueEnum { return .optNestedEnum(Optional.bridgeJSLiftParameter()) case 4: return .optArray({ - let __isSome = _swift_js_pop_i32() - if __isSome == 0 { - return Optional<[Int]>.none - } else { - return [Int].bridgeJSLiftParameter() - } - }()) + let __isSome = _swift_js_pop_i32() + if __isSome == 0 { + return Optional<[Int]>.none + } else { + return [Int].bridgeJSLiftParameter() + } +}()) case 5: - return .optJsClass(Optional.bridgeJSLiftParameter().map { - Foo(unsafelyWrapping: $0) - }) + return .optJsClass(Optional.bridgeJSLiftParameter().map { Foo(unsafelyWrapping: $0) }) case 6: return .empty default: @@ -2840,42 +2838,42 @@ extension OptionalAllTypesResult: _BridgedSwiftAssociatedValueEnum { case .optStruct(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(0) case .optClass(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(1) case .optJSObject(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(2) case .optNestedEnum(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) + _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(3) case .optArray(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(4) case .optJsClass(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.jsObject.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.jsObject.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(5) @@ -2899,42 +2897,42 @@ extension OptionalAllTypesResult: _BridgedSwiftAssociatedValueEnum { case .optStruct(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(0)) case .optClass(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(1)) case .optJSObject(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(2)) case .optNestedEnum(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) + _swift_js_push_i32(__bjs_unwrapped_param0.bridgeJSLowerParameter()) } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(3)) case .optArray(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerReturn() + __bjs_unwrapped_param0.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(4)) case .optJsClass(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.jsObject.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.jsObject.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(5)) @@ -2965,36 +2963,36 @@ extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { case .success(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) return Int32(0) case .failure(let param0, let param1): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) return Int32(1) case .status(let param0, let param1, let param2): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) let __bjs_isSome_param2 = param2 != nil if let __bjs_unwrapped_param2 = param2 { - __bjs_unwrapped_param2.bridgeJSLowerStackReturn() + __bjs_unwrapped_param2.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param2 ? 1 : 0) return Int32(2) @@ -3016,36 +3014,36 @@ extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { case .success(let param0): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) _swift_js_push_tag(Int32(0)) case .failure(let param0, let param1): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) _swift_js_push_tag(Int32(1)) case .status(let param0, let param1, let param2): let __bjs_isSome_param0 = param0 != nil if let __bjs_unwrapped_param0 = param0 { - __bjs_unwrapped_param0.bridgeJSLowerStackReturn() + __bjs_unwrapped_param0.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param0 ? 1 : 0) let __bjs_isSome_param1 = param1 != nil if let __bjs_unwrapped_param1 = param1 { - __bjs_unwrapped_param1.bridgeJSLowerStackReturn() + __bjs_unwrapped_param1.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param1 ? 1 : 0) let __bjs_isSome_param2 = param2 != nil if let __bjs_unwrapped_param2 = param2 { - __bjs_unwrapped_param2.bridgeJSLowerStackReturn() + __bjs_unwrapped_param2.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_param2 ? 1 : 0) _swift_js_push_tag(Int32(2)) @@ -3176,12 +3174,12 @@ extension DataPoint: _BridgedSwiftStruct { self.label.bridgeJSLowerStackReturn() let __bjs_isSome_optCount = self.optCount != nil if let __bjs_unwrapped_optCount = self.optCount { - __bjs_unwrapped_optCount.bridgeJSLowerStackReturn() + __bjs_unwrapped_optCount.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optCount ? 1 : 0) let __bjs_isSome_optFlag = self.optFlag != nil if let __bjs_unwrapped_optFlag = self.optFlag { - __bjs_unwrapped_optFlag.bridgeJSLowerStackReturn() + __bjs_unwrapped_optFlag.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optFlag ? 1 : 0) } @@ -3243,7 +3241,7 @@ extension Address: _BridgedSwiftStruct { self.city.bridgeJSLowerStackReturn() let __bjs_isSome_zipCode = self.zipCode != nil if let __bjs_unwrapped_zipCode = self.zipCode { - __bjs_unwrapped_zipCode.bridgeJSLowerStackReturn() + __bjs_unwrapped_zipCode.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_zipCode ? 1 : 0) } @@ -3297,12 +3295,12 @@ extension Contact: _BridgedSwiftStruct { self.address.bridgeJSLowerReturn() let __bjs_isSome_email = self.email != nil if let __bjs_unwrapped_email = self.email { - __bjs_unwrapped_email.bridgeJSLowerStackReturn() + __bjs_unwrapped_email.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_email ? 1 : 0) let __bjs_isSome_secondaryAddress = self.secondaryAddress != nil if let __bjs_unwrapped_secondaryAddress = self.secondaryAddress { - __bjs_unwrapped_secondaryAddress.bridgeJSLowerReturn() + __bjs_unwrapped_secondaryAddress.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_secondaryAddress ? 1 : 0) } @@ -3353,12 +3351,12 @@ extension Config: _BridgedSwiftStruct { self.name.bridgeJSLowerStackReturn() let __bjs_isSome_theme = self.theme != nil if let __bjs_unwrapped_theme = self.theme { - __bjs_unwrapped_theme.bridgeJSLowerStackReturn() + __bjs_unwrapped_theme.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_theme ? 1 : 0) let __bjs_isSome_direction = self.direction != nil if let __bjs_unwrapped_direction = self.direction { - __bjs_unwrapped_direction.bridgeJSLowerStackReturn() + __bjs_unwrapped_direction.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_direction ? 1 : 0) self.status.bridgeJSLowerStackReturn() @@ -3408,7 +3406,7 @@ extension SessionData: _BridgedSwiftStruct { self.id.bridgeJSLowerStackReturn() let __bjs_isSome_owner = self.owner != nil if let __bjs_unwrapped_owner = self.owner { - __bjs_unwrapped_owner.bridgeJSLowerStackReturn() + __bjs_unwrapped_owner.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_owner ? 1 : 0) } @@ -3460,12 +3458,12 @@ extension ValidationReport: _BridgedSwiftStruct { self.result.bridgeJSLowerReturn() let __bjs_isSome_status = self.status != nil if let __bjs_unwrapped_status = self.status { - __bjs_unwrapped_status.bridgeJSLowerStackReturn() + __bjs_unwrapped_status.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_status ? 1 : 0) let __bjs_isSome_outcome = self.outcome != nil if let __bjs_unwrapped_outcome = self.outcome { - _swift_js_push_i32(__bjs_unwrapped_outcome.bridgeJSLowerParameter()) + _swift_js_push_i32(__bjs_unwrapped_outcome.bridgeJSLowerParameter()) } _swift_js_push_i32(__bjs_isSome_outcome ? 1 : 0) } @@ -3526,23 +3524,23 @@ extension AdvancedConfig: _BridgedSwiftStruct { self.status.bridgeJSLowerStackReturn() let __bjs_isSome_result = self.result != nil if let __bjs_unwrapped_result = self.result { - _swift_js_push_i32(__bjs_unwrapped_result.bridgeJSLowerParameter()) + _swift_js_push_i32(__bjs_unwrapped_result.bridgeJSLowerParameter()) } _swift_js_push_i32(__bjs_isSome_result ? 1 : 0) let __bjs_isSome_metadata = self.metadata != nil if let __bjs_unwrapped_metadata = self.metadata { - __bjs_unwrapped_metadata.bridgeJSLowerStackReturn() + __bjs_unwrapped_metadata.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_metadata ? 1 : 0) let __bjs_isSome_location = self.location != nil if let __bjs_unwrapped_location = self.location { - __bjs_unwrapped_location.bridgeJSLowerReturn() + __bjs_unwrapped_location.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_location ? 1 : 0) self.defaults.bridgeJSLowerReturn() let __bjs_isSome_overrideDefaults = self.overrideDefaults != nil if let __bjs_unwrapped_overrideDefaults = self.overrideDefaults { - __bjs_unwrapped_overrideDefaults.bridgeJSLowerReturn() + __bjs_unwrapped_overrideDefaults.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_overrideDefaults ? 1 : 0) } @@ -3594,12 +3592,12 @@ extension MeasurementConfig: _BridgedSwiftStruct { self.ratio.bridgeJSLowerStackReturn() let __bjs_isSome_optionalPrecision = self.optionalPrecision != nil if let __bjs_unwrapped_optionalPrecision = self.optionalPrecision { - __bjs_unwrapped_optionalPrecision.bridgeJSLowerStackReturn() + __bjs_unwrapped_optionalPrecision.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optionalPrecision ? 1 : 0) let __bjs_isSome_optionalRatio = self.optionalRatio != nil if let __bjs_unwrapped_optionalRatio = self.optionalRatio { - __bjs_unwrapped_optionalRatio.bridgeJSLowerStackReturn() + __bjs_unwrapped_optionalRatio.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optionalRatio ? 1 : 0) } @@ -3735,7 +3733,7 @@ extension CopyableCart: _BridgedSwiftStruct { self.x.bridgeJSLowerStackReturn() let __bjs_isSome_note = self.note != nil if let __bjs_unwrapped_note = self.note { - __bjs_unwrapped_note.bridgeJSLowerStackReturn() + __bjs_unwrapped_note.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_note ? 1 : 0) } @@ -3842,7 +3840,7 @@ extension CopyableNestedCart: _BridgedSwiftStruct { self.item.bridgeJSLowerReturn() let __bjs_isSome_shippingAddress = self.shippingAddress != nil if let __bjs_unwrapped_shippingAddress = self.shippingAddress { - __bjs_unwrapped_shippingAddress.bridgeJSLowerReturn() + __bjs_unwrapped_shippingAddress.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_shippingAddress ? 1 : 0) } @@ -4011,7 +4009,7 @@ extension JSObjectContainer: _BridgedSwiftStruct { self.object.bridgeJSLowerStackReturn() let __bjs_isSome_optionalObject = self.optionalObject != nil if let __bjs_unwrapped_optionalObject = self.optionalObject { - __bjs_unwrapped_optionalObject.bridgeJSLowerStackReturn() + __bjs_unwrapped_optionalObject.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optionalObject ? 1 : 0) } @@ -4051,9 +4049,7 @@ fileprivate func _bjs_struct_lift_JSObjectContainer() -> Int32 { extension FooContainer: _BridgedSwiftStruct { @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter() -> FooContainer { - let optionalFoo = Optional.bridgeJSLiftParameter().map { - Foo(unsafelyWrapping: $0) - } + let optionalFoo = Optional.bridgeJSLiftParameter().map { Foo(unsafelyWrapping: $0) } let foo = Foo(unsafelyWrapping: JSObject.bridgeJSLiftParameter()) return FooContainer(foo: foo, optionalFoo: optionalFoo) } @@ -4062,7 +4058,7 @@ extension FooContainer: _BridgedSwiftStruct { self.foo.jsObject.bridgeJSLowerStackReturn() let __bjs_isSome_optionalFoo = self.optionalFoo != nil if let __bjs_unwrapped_optionalFoo = self.optionalFoo { - __bjs_unwrapped_optionalFoo.jsObject.bridgeJSLowerStackReturn() + __bjs_unwrapped_optionalFoo.jsObject.bridgeJSLowerStackReturn() } _swift_js_push_i32(__bjs_isSome_optionalFoo ? 1 : 0) } @@ -4109,7 +4105,7 @@ extension ArrayMembers: _BridgedSwiftStruct { } else { return [String].bridgeJSLiftParameter() } - }() +}() let ints = [Int].bridgeJSLiftParameter() return ArrayMembers(ints: ints, optStrings: optStrings) } @@ -4118,7 +4114,7 @@ extension ArrayMembers: _BridgedSwiftStruct { self.ints.bridgeJSLowerReturn() let __bjs_isSome_optStrings = self.optStrings != nil if let __bjs_unwrapped_optStrings = self.optStrings { - __bjs_unwrapped_optStrings.bridgeJSLowerReturn() + __bjs_unwrapped_optStrings.bridgeJSLowerReturn() } _swift_js_push_i32(__bjs_isSome_optStrings ? 1 : 0) } @@ -4396,10 +4392,11 @@ public func _bjs_roundTripOptionalJSValueArray(_ v: Int32) -> Void { } else { return [JSValue].bridgeJSLiftParameter() } - }()) + }()) let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") @@ -4636,7 +4633,7 @@ public func _bjs_asyncRoundTripVoid() -> Int32 { #if arch(wasm32) let ret = JSPromise.async { await asyncRoundTripVoid() - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -4649,7 +4646,7 @@ public func _bjs_asyncRoundTripInt(_ v: Int32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripInt(v: Int.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -4662,7 +4659,7 @@ public func _bjs_asyncRoundTripFloat(_ v: Float32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripFloat(v: Float.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -4675,7 +4672,7 @@ public func _bjs_asyncRoundTripDouble(_ v: Float64) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripDouble(v: Double.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -4688,7 +4685,7 @@ public func _bjs_asyncRoundTripBool(_ v: Int32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripBool(v: Bool.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -4701,7 +4698,7 @@ public func _bjs_asyncRoundTripString(_ vBytes: Int32, _ vLength: Int32) -> Int3 #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripString(v: String.bridgeJSLiftParameter(vBytes, vLength)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -4714,7 +4711,7 @@ public func _bjs_asyncRoundTripSwiftHeapObject(_ v: UnsafeMutableRawPointer) -> #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripSwiftHeapObject(v: Greeter.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -4727,7 +4724,7 @@ public func _bjs_asyncRoundTripJSObject(_ v: Int32) -> Int32 { #if arch(wasm32) let ret = JSPromise.async { return await asyncRoundTripJSObject(v: JSObject.bridgeJSLiftParameter(v)).jsValue - } .jsObject + }.jsObject return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -5424,7 +5421,7 @@ public func _bjs_arrayWithOptionalDefault(_ values: Int32) -> Int32 { } else { return [Int].bridgeJSLiftParameter() } - }()) + }()) return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") @@ -5593,17 +5590,19 @@ public func _bjs_roundTripOptionalIntArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5618,17 +5617,19 @@ public func _bjs_roundTripOptionalStringArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5643,17 +5644,19 @@ public func _bjs_roundTripOptionalDataPointArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5668,17 +5671,19 @@ public func _bjs_roundTripOptionalDirectionArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5693,17 +5698,19 @@ public func _bjs_roundTripOptionalStatusArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5720,10 +5727,11 @@ public func _bjs_roundTripOptionalIntArrayType(_ values: Int32) -> Void { } else { return [Int].bridgeJSLiftParameter() } - }()) + }()) let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") @@ -5740,10 +5748,11 @@ public func _bjs_roundTripOptionalStringArrayType(_ values: Int32) -> Void { } else { return [String].bridgeJSLiftParameter() } - }()) + }()) let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") @@ -5760,10 +5769,11 @@ public func _bjs_roundTripOptionalGreeterArrayType(_ greeters: Int32) -> Void { } else { return [Greeter].bridgeJSLiftParameter() } - }()) + }()) let __bjs_isSome_ret = ret != nil if let __bjs_unwrapped_ret = ret { - __bjs_unwrapped_ret.bridgeJSLowerReturn()} + __bjs_unwrapped_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(__bjs_isSome_ret ? 1 : 0) #else fatalError("Only available on WebAssembly") @@ -5778,14 +5788,15 @@ public func _bjs_roundTripNestedIntArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Int]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Int].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5800,14 +5811,15 @@ public func _bjs_roundTripNestedStringArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[String]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([String].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5822,14 +5834,15 @@ public func _bjs_roundTripNestedDoubleArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Double]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Double].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5844,14 +5857,15 @@ public func _bjs_roundTripNestedBoolArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Bool]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Bool].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5866,14 +5880,15 @@ public func _bjs_roundTripNestedDataPointArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[DataPoint]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([DataPoint].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5888,14 +5903,15 @@ public func _bjs_roundTripNestedDirectionArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Direction]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Direction].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5910,14 +5926,15 @@ public func _bjs_roundTripNestedGreeterArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [[Greeter]] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append([Greeter].bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { - __bjs_elem_ret.bridgeJSLowerReturn()} + __bjs_elem_ret.bridgeJSLowerReturn() + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -5995,9 +6012,7 @@ public func _bjs_consumeDataProcessorArrayType() -> Int32 { public func _bjs_roundTripDataProcessorArrayType() -> Void { #if arch(wasm32) let ret = roundTripDataProcessorArrayType(_: [AnyDataProcessor].bridgeJSLiftParameter()) - ret.map { - $0 as! AnyDataProcessor - } .bridgeJSLowerReturn() + ret.map { $0 as! AnyDataProcessor }.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif @@ -6022,17 +6037,19 @@ public func _bjs_roundTripOptionalJSObjectArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Optional.bridgeJSLiftParameter()) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -6047,15 +6064,13 @@ public func _bjs_roundTripFooArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Foo] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { + for _ in 0..<__count { __result.append(Foo(unsafelyWrapping: JSObject.bridgeJSLiftParameter())) } __result.reverse() return __result - }()) - ret.map { - $0.jsObject - } .bridgeJSLowerReturn() + }()) + ret.map { $0.jsObject }.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif @@ -6069,19 +6084,19 @@ public func _bjs_roundTripOptionalFooArray() -> Void { let __count = Int(_swift_js_pop_i32()) var __result: [Optional] = [] __result.reserveCapacity(__count) - for _ in 0 ..< __count { - __result.append(Optional.bridgeJSLiftParameter().map { - Foo(unsafelyWrapping: $0) - }) + for _ in 0..<__count { + __result.append(Optional.bridgeJSLiftParameter().map { Foo(unsafelyWrapping: $0) }) } __result.reverse() return __result - }()) + }()) for __bjs_elem_ret in ret { let __bjs_isSome_ret_elem = __bjs_elem_ret != nil if let __bjs_unwrapped_ret_elem = __bjs_elem_ret { - __bjs_unwrapped_ret_elem.jsObject.bridgeJSLowerStackReturn()} - _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)} + __bjs_unwrapped_ret_elem.jsObject.bridgeJSLowerStackReturn() + } + _swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0) + } _swift_js_push_i32(Int32(ret.count)) #else fatalError("Only available on WebAssembly") @@ -8160,9 +8175,7 @@ public func _bjs_DataProcessorManager_processor_set(_ _self: UnsafeMutableRawPoi @_cdecl("bjs_DataProcessorManager_backupProcessor_get") public func _bjs_DataProcessorManager_backupProcessor_get(_ _self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = DataProcessorManager.bridgeJSLiftParameter(_self).backupProcessor.flatMap { - $0 as? AnyDataProcessor - } + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).backupProcessor.flatMap { $0 as? AnyDataProcessor } return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly")