diff --git a/Benchmarks/Sources/Generated/BridgeJS.ExportSwift.swift b/Benchmarks/Sources/Generated/BridgeJS.ExportSwift.swift index d02d0fae..ab0cbb19 100644 --- a/Benchmarks/Sources/Generated/BridgeJS.ExportSwift.swift +++ b/Benchmarks/Sources/Generated/BridgeJS.ExportSwift.swift @@ -7,7 +7,7 @@ @_spi(BridgeJS) import JavaScriptKit extension APIResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> APIResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -26,6 +26,43 @@ extension APIResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0): + _swift_js_push_int(Int32(param0)) + return Int32(1) + case .flag(let param0): + _swift_js_push_int(param0 ? 1 : 0) + return Int32(2) + case .rate(let param0): + _swift_js_push_f32(param0) + return Int32(3) + case .precise(let param0): + _swift_js_push_f64(param0) + return Int32(4) + case .info: + return Int32(5) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -53,7 +90,7 @@ extension APIResult: _BridgedSwiftAssociatedValueEnum { } extension ComplexResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> ComplexResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> ComplexResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -74,6 +111,79 @@ extension ComplexResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .error(let param0, let param1): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + _swift_js_push_int(Int32(param1)) + return Int32(1) + case .location(let param0, let param1, let param2): + _swift_js_push_f64(param0) + _swift_js_push_f64(param1) + var __bjs_param2 = param2 + __bjs_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(2) + case .status(let param0, let param1, let param2): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(Int32(param1)) + var __bjs_param2 = param2 + __bjs_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(3) + case .coordinates(let param0, let param1, let param2): + _swift_js_push_f64(param0) + _swift_js_push_f64(param1) + _swift_js_push_f64(param2) + return Int32(4) + case .comprehensive(let param0, let param1, let param2, let param3, let param4, let param5, let param6, let param7, let param8): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(param1 ? 1 : 0) + _swift_js_push_int(Int32(param2)) + _swift_js_push_int(Int32(param3)) + _swift_js_push_f64(param4) + _swift_js_push_f64(param5) + var __bjs_param6 = param6 + __bjs_param6.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + var __bjs_param7 = param7 + __bjs_param7.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + var __bjs_param8 = param8 + __bjs_param8.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(5) + case .info: + return Int32(6) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> ComplexResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> ComplexResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 666b62d2..9ca71ef1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -112,5 +112,9 @@ Run this script when you've made changes to: These changes require updating the pre-generated Swift bindings committed to the repository. +**Adding new BridgeJS intrinsics:** + +If you add new `@_extern(wasm, module: "bjs")` functions to [`BridgeJSInstrincics.swift`](Sources/JavaScriptKit/BridgeJSInstrincics.swift), also add corresponding stub entries to [`Plugins/PackageToJS/Templates/instantiate.js`](Plugins/PackageToJS/Templates/instantiate.js) in the `importObject["bjs"]` object. This allows packages without BridgeJS-generated code to instantiate successfully. + ## Support If you have any questions or need assistance, feel free to reach out via [GitHub Issues](https://github.com/swiftwasm/JavaScriptKit/issues) or [Discord](https://discord.gg/ashJW8T8yp). diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift index 9179a966..4cdc8a3b 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift @@ -588,7 +588,6 @@ public class ExportSwift { baseName: name, namespace: finalNamespace, staticContext: isStatic ? staticContext : nil, - operation: nil, className: classNameForABI ) @@ -749,9 +748,8 @@ public class ExportSwift { case .topLevel: diagnose(node: node, message: "@JS var must be inside a @JS class or enum") return .skipChildren - case .protocolBody(_, _): - diagnose(node: node, message: "Properties are not supported in protocols") - return .skipChildren + case .protocolBody(let protocolName, let protocolKey): + return visitProtocolProperty(node: node, protocolName: protocolName, protocolKey: protocolKey) } // Process each binding (variable declaration) @@ -775,23 +773,8 @@ public class ExportSwift { // Check if property is readonly let isLet = node.bindingSpecifier.tokenKind == .keyword(.let) - let isGetterOnly = node.bindings.contains(where: { - switch $0.accessorBlock?.accessors { - case .accessors(let accessors): - // Has accessors - check if it only has a getter (no setter, willSet, or didSet) - return !accessors.contains(where: { accessor in - let tokenKind = accessor.accessorSpecifier.tokenKind - return tokenKind == .keyword(.set) || tokenKind == .keyword(.willSet) - || tokenKind == .keyword(.didSet) - }) - case .getter: - // Has only a getter block - return true - case nil: - // No accessor block - this is a stored property, not readonly - return false - } - }) + let isGetterOnly = node.bindings.contains(where: { self.hasOnlyGetter($0.accessorBlock) }) + let isReadonly = isLet || isGetterOnly let exportedProperty = ExportedProperty( @@ -997,6 +980,17 @@ public class ExportSwift { message: "Protocol visibility must be at least internal" ) + let protocolUniqueKey = makeKey(name: name, namespace: namespaceResult.namespace) + + exportedProtocolByName[protocolUniqueKey] = ExportedProtocol( + name: name, + methods: [], + properties: [], + namespace: namespaceResult.namespace + ) + + stateStack.push(state: .protocolBody(name: name, key: protocolUniqueKey)) + var methods: [ExportedFunction] = [] for member in node.memberBlock.members { if let funcDecl = member.decl.as(FunctionDeclSyntax.self) { @@ -1007,19 +1001,23 @@ public class ExportSwift { ) { methods.append(exportedFunction) } + } else if let varDecl = member.decl.as(VariableDeclSyntax.self) { + _ = visitProtocolProperty(node: varDecl, protocolName: name, protocolKey: protocolUniqueKey) } } let exportedProtocol = ExportedProtocol( name: name, methods: methods, + properties: exportedProtocolByName[protocolUniqueKey]?.properties ?? [], namespace: namespaceResult.namespace ) - let protocolUniqueKey = makeKey(name: name, namespace: namespaceResult.namespace) exportedProtocolByName[protocolUniqueKey] = exportedProtocol exportedProtocolNames.append(protocolUniqueKey) + stateStack.pop() + parent.exportedProtocolNameByKey[protocolUniqueKey] = name return .skipChildren @@ -1055,8 +1053,6 @@ public class ExportSwift { let abiName = ABINameGenerator.generateABIName( baseName: name, namespace: namespace, - staticContext: nil, - operation: nil, className: protocolName ) @@ -1075,6 +1071,81 @@ public class ExportSwift { ) } + private func visitProtocolProperty( + node: VariableDeclSyntax, + protocolName: String, + protocolKey: String + ) -> SyntaxVisitorContinueKind { + for binding in node.bindings { + guard let pattern = binding.pattern.as(IdentifierPatternSyntax.self) else { + diagnose(node: binding.pattern, message: "Complex patterns not supported for protocol properties") + continue + } + + let propertyName = pattern.identifier.text + + guard let typeAnnotation = binding.typeAnnotation else { + diagnose(node: binding, message: "Protocol property must have explicit type annotation") + continue + } + + guard let propertyType = self.parent.lookupType(for: typeAnnotation.type) else { + diagnoseUnsupportedType(node: typeAnnotation.type, type: typeAnnotation.type.trimmedDescription) + continue + } + + guard let accessorBlock = binding.accessorBlock else { + diagnose( + node: binding, + message: "Protocol property must specify { get } or { get set }", + hint: "Add { get } for readonly or { get set } for readwrite property" + ) + continue + } + + let isReadonly = hasOnlyGetter(accessorBlock) + + let exportedProperty = ExportedProtocolProperty( + name: propertyName, + type: propertyType, + isReadonly: isReadonly + ) + + if var currentProtocol = exportedProtocolByName[protocolKey] { + var properties = currentProtocol.properties + properties.append(exportedProperty) + + currentProtocol = ExportedProtocol( + name: currentProtocol.name, + methods: currentProtocol.methods, + properties: properties, + namespace: currentProtocol.namespace + ) + exportedProtocolByName[protocolKey] = currentProtocol + } + } + + return .skipChildren + } + + private func hasOnlyGetter(_ accessorBlock: AccessorBlockSyntax?) -> Bool { + switch accessorBlock?.accessors { + case .accessors(let accessors): + // Has accessors - check if it only has a getter (no setter, willSet, or didSet) + return !accessors.contains(where: { accessor in + let tokenKind = accessor.accessorSpecifier.tokenKind + return tokenKind == .keyword(.set) || tokenKind == .keyword(.willSet) + || tokenKind == .keyword(.didSet) + }) + case .getter: + // Has only a getter block + return true + case nil: + // No accessor block - this is a stored property, not readonly + return false + } + } + override func visit(_ node: EnumCaseDeclSyntax) -> SyntaxVisitorContinueKind { guard case .enumBody(_, let enumKey) = stateStack.current else { return .visitChildren @@ -1093,6 +1164,8 @@ public class ExportSwift { } else { var numericExpr = element.rawValue?.value var isNegative = false + + // Check for prefix operator (for negative numbers) if let prefixExpr = numericExpr?.as(PrefixOperatorExprSyntax.self), prefixExpr.operator.text == "-" { @@ -1677,13 +1750,13 @@ public class ExportSwift { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> \(raw: typeName) { - return \(raw: typeName)(bridgeJSRawValue: value)! + 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 bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -1700,13 +1773,31 @@ public class ExportSwift { func renderAssociatedValueEnumHelpers(_ enumDef: ExportedEnum) -> DeclSyntax { let typeName = enumDef.swiftCallName return """ - extension \(raw: typeName): _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> \(raw: typeName) { + 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)") } } + + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + \(raw: generateLowerParameterSwitchCases(enumDef: enumDef).joined(separator: "\n")) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> \(raw: typeName) { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> \(raw: typeName) { + return _bridgeJSLiftFromCaseId(caseId) + } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { @@ -1775,6 +1866,78 @@ public class ExportSwift { return cases } + private func generatePayloadPushingCode( + associatedValues: [AssociatedValue] + ) -> [String] { + var bodyLines: [String] = [] + for (index, associatedValue) in associatedValues.enumerated() { + let paramName = associatedValue.label ?? "param\(index)" + switch associatedValue.type { + case .string: + bodyLines.append("var __bjs_\(paramName) = \(paramName)") + bodyLines.append("__bjs_\(paramName).withUTF8 { ptr in") + bodyLines.append("_swift_js_push_string(ptr.baseAddress, Int32(ptr.count))") + bodyLines.append("}") + case .int: + bodyLines.append("_swift_js_push_int(Int32(\(paramName)))") + case .bool: + bodyLines.append("_swift_js_push_int(\(paramName) ? 1 : 0)") + case .float: + bodyLines.append("_swift_js_push_f32(\(paramName))") + case .double: + bodyLines.append("_swift_js_push_f64(\(paramName))") + case .optional(let wrappedType): + bodyLines.append("let __bjs_isSome_\(paramName) = \(paramName) != nil") + bodyLines.append("if let __bjs_unwrapped_\(paramName) = \(paramName) {") + switch wrappedType { + case .string: + bodyLines.append("var __bjs_str_\(paramName) = __bjs_unwrapped_\(paramName)") + bodyLines.append("__bjs_str_\(paramName).withUTF8 { ptr in") + bodyLines.append("_swift_js_push_string(ptr.baseAddress, Int32(ptr.count))") + bodyLines.append("}") + case .int: + bodyLines.append("_swift_js_push_int(Int32(__bjs_unwrapped_\(paramName)))") + case .bool: + bodyLines.append("_swift_js_push_int(__bjs_unwrapped_\(paramName) ? 1 : 0)") + case .float: + bodyLines.append("_swift_js_push_f32(__bjs_unwrapped_\(paramName))") + case .double: + bodyLines.append("_swift_js_push_f64(__bjs_unwrapped_\(paramName))") + default: + bodyLines.append( + "preconditionFailure(\"BridgeJS: unsupported optional wrapped type in generated code\")" + ) + } + bodyLines.append("}") + bodyLines.append("_swift_js_push_int(__bjs_isSome_\(paramName) ? 1 : 0)") + default: + bodyLines.append( + "preconditionFailure(\"BridgeJS: unsupported associated value type in generated code\")" + ) + } + } + return bodyLines + } + + private func generateLowerParameterSwitchCases(enumDef: ExportedEnum) -> [String] { + var cases: [String] = [] + for (caseIndex, enumCase) in enumDef.cases.enumerated() { + if enumCase.associatedValues.isEmpty { + cases.append("case .\(enumCase.name):") + cases.append("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))") + } + } + return cases + } + private func generateReturnSwitchCases(enumDef: ExportedEnum) -> [String] { var cases: [String] = [] for (caseIndex, enumCase) in enumDef.cases.enumerated() { @@ -1782,59 +1945,13 @@ public class ExportSwift { cases.append("case .\(enumCase.name):") cases.append("_swift_js_push_tag(Int32(\(caseIndex)))") } else { - var bodyLines: [String] = [] - bodyLines.append("_swift_js_push_tag(Int32(\(caseIndex)))") - for (index, associatedValue) in enumCase.associatedValues.enumerated() { - let paramName = associatedValue.label ?? "param\(index)" - switch associatedValue.type { - case .string: - bodyLines.append("var __bjs_\(paramName) = \(paramName)") - bodyLines.append("__bjs_\(paramName).withUTF8 { ptr in") - bodyLines.append("_swift_js_push_string(ptr.baseAddress, Int32(ptr.count))") - bodyLines.append("}") - case .int: - bodyLines.append("_swift_js_push_int(Int32(\(paramName)))") - case .bool: - bodyLines.append("_swift_js_push_int(\(paramName) ? 1 : 0)") - case .float: - bodyLines.append("_swift_js_push_f32(\(paramName))") - case .double: - bodyLines.append("_swift_js_push_f64(\(paramName))") - case .optional(let wrappedType): - bodyLines.append("let __bjs_isSome_\(paramName) = \(paramName) != nil") - bodyLines.append("if let __bjs_unwrapped_\(paramName) = \(paramName) {") - switch wrappedType { - case .string: - bodyLines.append("var __bjs_str_\(paramName) = __bjs_unwrapped_\(paramName)") - bodyLines.append("__bjs_str_\(paramName).withUTF8 { ptr in") - bodyLines.append("_swift_js_push_string(ptr.baseAddress, Int32(ptr.count))") - bodyLines.append("}") - case .int: - bodyLines.append("_swift_js_push_int(Int32(__bjs_unwrapped_\(paramName)))") - case .bool: - bodyLines.append("_swift_js_push_int(__bjs_unwrapped_\(paramName) ? 1 : 0)") - case .float: - bodyLines.append("_swift_js_push_f32(__bjs_unwrapped_\(paramName))") - case .double: - bodyLines.append("_swift_js_push_f64(__bjs_unwrapped_\(paramName))") - default: - bodyLines.append( - "preconditionFailure(\"BridgeJS: unsupported optional wrapped type in generated code\")" - ) - } - bodyLines.append("}") - bodyLines.append("_swift_js_push_int(__bjs_isSome_\(paramName) ? 1 : 0)") - default: - bodyLines.append( - "preconditionFailure(\"BridgeJS: unsupported associated value type in generated code\")" - ) - } - } 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: bodyLines) + cases.append("_swift_js_push_tag(Int32(\(caseIndex)))") + let payloadCode = generatePayloadPushingCode(associatedValues: enumCase.associatedValues) + cases.append(contentsOf: payloadCode) } } return cases @@ -2108,10 +2225,8 @@ public class ExportSwift { """ } - /// Generates an AnyProtocol wrapper struct for a protocol - /// /// Creates a struct that wraps a JSObject and implements protocol methods - /// by calling `@_extern(wasm)` functions that forward to JavaScript + /// by calling `@_extern(wasm)` functions that forward to JavaScript via JSObject ID func renderProtocolWrapper(protocol proto: ExportedProtocol) throws -> DeclSyntax { let wrapperName = "Any\(proto.name)" let protocolName = proto.name @@ -2131,42 +2246,77 @@ public class ExportSwift { var externParams: [String] = ["this: Int32"] for param in method.parameters { - let loweringInfo = try param.type.loweringParameterInfo() - assert( - loweringInfo.loweredParameters.count == 1, - "Protocol parameters must lower to a single WASM type" - ) - let (_, wasmType) = loweringInfo.loweredParameters[0] - externParams.append("\(param.name): \(wasmType.swiftType)") + let loweringInfo = try param.type.loweringParameterInfo(context: .protocolExport) + for (paramName, wasmType) in loweringInfo.loweredParameters { + let fullParamName = + loweringInfo.loweredParameters.count > 1 + ? "\(param.name)\(paramName.capitalizedFirstLetter)" : param.name + externParams.append("\(fullParamName): \(wasmType.swiftType)") + } } + var preCallStatements: [String] = [] var callArgs: [String] = ["this: Int32(bitPattern: jsObject.id)"] for param in method.parameters { - callArgs.append("\(param.name): \(param.name).bridgeJSLowerParameter()") + let loweringInfo = try param.type.loweringParameterInfo(context: .protocolExport) + if case .optional = param.type, loweringInfo.loweredParameters.count > 1 { + let isSomeName = "\(param.name)\(loweringInfo.loweredParameters[0].name.capitalizedFirstLetter)" + let wrappedName = "\(param.name)\(loweringInfo.loweredParameters[1].name.capitalizedFirstLetter)" + preCallStatements.append( + "let (\(isSomeName), \(wrappedName)) = \(param.name).bridgeJSLowerParameterWithPresence()" + ) + callArgs.append("\(isSomeName): \(isSomeName)") + callArgs.append("\(wrappedName): \(wrappedName)") + } else { + callArgs.append("\(param.name): \(param.name).bridgeJSLowerParameter()") + } } let returnTypeStr: String let externReturnType: String let callCode: DeclSyntax + let preCallCode = preCallStatements.isEmpty ? "" : preCallStatements.joined(separator: "\n") + "\n" + if method.returnType == .void { returnTypeStr = "" externReturnType = "" callCode = """ - _extern_\(raw: method.name)(\(raw: callArgs.joined(separator: ", "))) + \(raw: preCallCode)_extern_\(raw: method.name)(\(raw: callArgs.joined(separator: ", "))) """ } else { returnTypeStr = " -> \(method.returnType.swiftType)" - let liftingInfo = try method.returnType.liftingReturnInfo() - if let abiType = liftingInfo.valueToLift { + let liftingInfo = try method.returnType.liftingReturnInfo( + context: .protocolExport + ) + + if case .optional = method.returnType { + if let abiType = liftingInfo.valueToLift { + externReturnType = " -> \(abiType.swiftType)" + callCode = """ + \(raw: preCallCode)let ret = _extern_\(raw: method.name)(\(raw: callArgs.joined(separator: ", "))) + return \(raw: method.returnType.swiftType).bridgeJSLiftReturn(ret) + """ + } else { + externReturnType = "" + callCode = """ + \(raw: preCallCode)_extern_\(raw: method.name)(\(raw: callArgs.joined(separator: ", "))) + return \(raw: method.returnType.swiftType).bridgeJSLiftReturn() + """ + } + } else if let abiType = liftingInfo.valueToLift { externReturnType = " -> \(abiType.swiftType)" + callCode = """ + \(raw: preCallCode)let ret = _extern_\(raw: method.name)(\(raw: callArgs.joined(separator: ", "))) + return \(raw: method.returnType.swiftType).bridgeJSLiftReturn(ret) + """ } else { externReturnType = "" + callCode = """ + \(raw: preCallCode)_extern_\(raw: method.name)(\(raw: callArgs.joined(separator: ", "))) + return \(raw: method.returnType.swiftType).bridgeJSLiftReturn() + """ } - callCode = """ - let ret = _extern_\(raw: method.name)(\(raw: callArgs.joined(separator: ", "))) - return \(raw: method.returnType.swiftType).bridgeJSLiftReturn(ret) - """ } let methodImplementation: DeclSyntax = """ func \(raw: method.name)(\(raw: swiftParams.joined(separator: ", ")))\(raw: returnTypeStr) { @@ -2179,11 +2329,24 @@ public class ExportSwift { methodDecls.append(methodImplementation) } + var propertyDecls: [DeclSyntax] = [] + + for property in proto.properties { + let propertyImpl = try renderProtocolProperty( + property: property, + protocolName: protocolName, + moduleName: moduleName + ) + propertyDecls.append(propertyImpl) + } + + let allDecls = (methodDecls + propertyDecls).map { $0.description }.joined(separator: "\n\n") + return """ struct \(raw: wrapperName): \(raw: protocolName), _BridgedSwiftProtocolWrapper { let jsObject: JSObject - \(raw: methodDecls.map { $0.description }.joined(separator: "\n\n")) + \(raw: allDecls) static func bridgeJSLiftParameter(_ value: Int32) -> Self { return \(raw: wrapperName)(jsObject: JSObject(id: UInt32(bitPattern: value))) @@ -2191,6 +2354,119 @@ public class ExportSwift { } """ } + + private func renderProtocolProperty( + property: ExportedProtocolProperty, + protocolName: String, + moduleName: String + ) throws -> DeclSyntax { + let getterAbiName = ABINameGenerator.generateABIName( + baseName: property.name, + operation: "get", + className: protocolName + ) + let setterAbiName = ABINameGenerator.generateABIName( + baseName: property.name, + operation: "set", + className: protocolName + ) + + let usesSideChannel: Bool + if case .optional(let wrappedType) = property.type { + switch wrappedType { + case .string: + usesSideChannel = true + case .rawValueEnum(_, let rawType): + switch rawType { + case .string: + usesSideChannel = true + default: + usesSideChannel = true + } + case .int, .float, .double: + usesSideChannel = true + case .bool, .caseEnum, .associatedValueEnum, .swiftHeapObject: + usesSideChannel = false + default: + usesSideChannel = false + } + } else { + usesSideChannel = false + } + + let liftingInfo = try property.type.liftingReturnInfo(context: .protocolExport) + let getterReturnType: String + let getterBody: String + + if usesSideChannel { + // Optional case/raw enums use side-channel reading (Void return) + getterReturnType = "" + getterBody = """ + _extern_get(this: Int32(bitPattern: jsObject.id)) + return \(property.type.swiftType).bridgeJSLiftReturnFromSideChannel() + """ + } else if let abiType = liftingInfo.valueToLift { + getterReturnType = " -> \(abiType.swiftType)" + getterBody = """ + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return \(property.type.swiftType).bridgeJSLiftReturn(ret) + """ + } else { + getterReturnType = "" + getterBody = """ + _extern_get(this: Int32(bitPattern: jsObject.id)) + return \(property.type.swiftType).bridgeJSLiftReturn() + """ + } + + if property.isReadonly { + return """ + var \(raw: property.name): \(raw: property.type.swiftType) { + get { + @_extern(wasm, module: "\(raw: moduleName)", name: "\(raw: getterAbiName)") + func _extern_get(this: Int32)\(raw: getterReturnType) + \(raw: getterBody) + } + } + """ + } else { + let loweringInfo = try property.type.loweringParameterInfo(context: .protocolExport) + + let setterParams = + (["this: Int32"] + loweringInfo.loweredParameters.map { "\($0.name): \($0.type.swiftType)" }).joined( + separator: ", " + ) + + let setterBody: String + if case .optional = property.type, loweringInfo.loweredParameters.count > 1 { + let isSomeParam = loweringInfo.loweredParameters[0].name + let wrappedParam = loweringInfo.loweredParameters[1].name + setterBody = """ + let (\(isSomeParam), \(wrappedParam)) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), \(isSomeParam): \(isSomeParam), \(wrappedParam): \(wrappedParam)) + """ + } else { + let paramName = loweringInfo.loweredParameters[0].name + setterBody = + "_extern_set(this: Int32(bitPattern: jsObject.id), \(paramName): newValue.bridgeJSLowerParameter())" + } + + return """ + var \(raw: property.name): \(raw: property.type.swiftType) { + get { + @_extern(wasm, module: "\(raw: moduleName)", name: "\(raw: getterAbiName)") + func _extern_get(this: Int32)\(raw: getterReturnType) + \(raw: getterBody) + } + set { + @_extern(wasm, module: "\(raw: moduleName)", name: "\(raw: setterAbiName)") + func _extern_set(\(raw: setterParams)) + \(raw: setterBody) + } + } + """ + } + } } fileprivate enum Constants { diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift index 64e89d66..4589817d 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift @@ -424,7 +424,7 @@ extension BridgeType { static let void = LoweringParameterInfo(loweredParameters: []) } - func loweringParameterInfo() throws -> LoweringParameterInfo { + func loweringParameterInfo(context: BridgeContext = .importTS) throws -> LoweringParameterInfo { switch self { case .bool: return .bool case .int: return .int @@ -433,14 +433,54 @@ extension BridgeType { case .string: return .string case .jsObject: return .jsObject case .void: return .void - case .swiftHeapObject: - throw BridgeJSCoreError("swiftHeapObject is not supported in imported signatures") + case .swiftHeapObject(let className): + switch context { + case .importTS: + throw BridgeJSCoreError( + """ + swiftHeapObject '\(className)' is not supported in TypeScript imports. + Swift classes can only be used in @JS protocols where Swift owns the instance. + """ + ) + case .protocolExport: + return LoweringParameterInfo(loweredParameters: [("pointer", .pointer)]) + } case .swiftProtocol: throw BridgeJSCoreError("swiftProtocol is not supported in imported signatures") - case .caseEnum, .rawValueEnum, .associatedValueEnum, .namespaceEnum: - throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") - case .optional: - throw BridgeJSCoreError("Optional types are not yet supported in TypeScript imports") + case .caseEnum: + switch context { + case .importTS: + throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") + case .protocolExport: + return LoweringParameterInfo(loweredParameters: [("value", .i32)]) + } + case .rawValueEnum(_, let rawType): + switch context { + case .importTS: + throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") + case .protocolExport: + // For protocol export we return .i32 for String raw value type instead of nil + return LoweringParameterInfo(loweredParameters: [("value", rawType.wasmCoreType ?? .i32)]) + } + case .associatedValueEnum: + switch context { + case .importTS: + throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") + case .protocolExport: + return LoweringParameterInfo(loweredParameters: [("caseId", .i32)]) + } + case .namespaceEnum: + throw BridgeJSCoreError("Namespace enums cannot be used as parameters") + case .optional(let wrappedType): + switch context { + case .importTS: + throw BridgeJSCoreError("Optional types are not yet supported in TypeScript imports") + case .protocolExport: + let wrappedInfo = try wrappedType.loweringParameterInfo(context: context) + var params = [("isSome", WasmCoreType.i32)] + params.append(contentsOf: wrappedInfo.loweredParameters) + return LoweringParameterInfo(loweredParameters: params) + } } } @@ -456,7 +496,9 @@ extension BridgeType { static let void = LiftingReturnInfo(valueToLift: nil) } - func liftingReturnInfo() throws -> LiftingReturnInfo { + func liftingReturnInfo( + context: BridgeContext = .importTS + ) throws -> LiftingReturnInfo { switch self { case .bool: return .bool case .int: return .int @@ -465,14 +507,52 @@ extension BridgeType { case .string: return .string case .jsObject: return .jsObject case .void: return .void - case .swiftHeapObject: - throw BridgeJSCoreError("swiftHeapObject is not supported in imported signatures") + case .swiftHeapObject(let className): + switch context { + case .importTS: + throw BridgeJSCoreError( + """ + swiftHeapObject '\(className)' cannot be returned from imported TypeScript functions. + JavaScript cannot create Swift heap objects. + """ + ) + case .protocolExport: + return LiftingReturnInfo(valueToLift: .pointer) + } case .swiftProtocol: throw BridgeJSCoreError("swiftProtocol is not supported in imported signatures") - case .caseEnum, .rawValueEnum, .associatedValueEnum, .namespaceEnum: - throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") - case .optional: - throw BridgeJSCoreError("Optional types are not yet supported in TypeScript imports") + case .caseEnum: + switch context { + case .importTS: + throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") + case .protocolExport: + return LiftingReturnInfo(valueToLift: .i32) + } + case .rawValueEnum(_, let rawType): + switch context { + case .importTS: + throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") + case .protocolExport: + // For protocol export we return .i32 for String raw value type instead of nil + return LiftingReturnInfo(valueToLift: rawType.wasmCoreType ?? .i32) + } + case .associatedValueEnum: + switch context { + case .importTS: + throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports") + case .protocolExport: + return LiftingReturnInfo(valueToLift: .i32) + } + case .namespaceEnum: + throw BridgeJSCoreError("Namespace enums cannot be used as return values") + case .optional(let wrappedType): + switch context { + case .importTS: + throw BridgeJSCoreError("Optional types are not yet supported in TypeScript imports") + case .protocolExport: + let wrappedInfo = try wrappedType.liftingReturnInfo(context: context) + return LiftingReturnInfo(valueToLift: wrappedInfo.valueToLift) + } } } } diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift index 88f2af3d..6abfd305 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift @@ -314,6 +314,13 @@ struct BridgeJSLink { } for proto in skeleton.protocols { + for property in proto.properties { + try renderProtocolProperty( + importObjectBuilder: importObjectBuilder, + protocol: proto, + property: property + ) + } for method in proto.methods { try renderProtocolMethod( importObjectBuilder: importObjectBuilder, @@ -582,6 +589,61 @@ struct BridgeJSLink { printer.write("}") } printer.write("}") + printer.write("bjs[\"swift_js_get_optional_int_presence\"] = function() {") + printer.indent { + printer.write("return \(JSGlueVariableScope.reservedStorageToReturnOptionalInt) != null ? 1 : 0;") + } + printer.write("}") + printer.write("bjs[\"swift_js_get_optional_int_value\"] = function() {") + printer.indent { + printer.write("const value = \(JSGlueVariableScope.reservedStorageToReturnOptionalInt);") + printer.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalInt) = undefined;") + printer.write("return value;") + } + printer.write("}") + printer.write("bjs[\"swift_js_get_optional_string\"] = function() {") + printer.indent { + printer.write("const str = \(JSGlueVariableScope.reservedStorageToReturnString);") + printer.write("\(JSGlueVariableScope.reservedStorageToReturnString) = undefined;") + printer.write("if (str == null) {") + printer.indent { + printer.write("return -1;") + } + printer.write("} else {") + printer.indent { + printer.write("const bytes = \(JSGlueVariableScope.reservedTextEncoder).encode(str);") + printer.write("\(JSGlueVariableScope.reservedStorageToReturnBytes) = bytes;") + printer.write("return bytes.length;") + } + printer.write("}") + } + printer.write("}") + printer.write("bjs[\"swift_js_get_optional_float_presence\"] = function() {") + printer.indent { + printer.write("return \(JSGlueVariableScope.reservedStorageToReturnOptionalFloat) != null ? 1 : 0;") + } + printer.write("}") + printer.write("bjs[\"swift_js_get_optional_float_value\"] = function() {") + printer.indent { + printer.write("const value = \(JSGlueVariableScope.reservedStorageToReturnOptionalFloat);") + printer.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalFloat) = undefined;") + printer.write("return value;") + } + printer.write("}") + printer.write("bjs[\"swift_js_get_optional_double_presence\"] = function() {") + printer.indent { + printer.write( + "return \(JSGlueVariableScope.reservedStorageToReturnOptionalDouble) != null ? 1 : 0;" + ) + } + printer.write("}") + printer.write("bjs[\"swift_js_get_optional_double_value\"] = function() {") + printer.indent { + printer.write("const value = \(JSGlueVariableScope.reservedStorageToReturnOptionalDouble);") + printer.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalDouble) = undefined;") + printer.write("return value;") + } + printer.write("}") } } return printer @@ -607,6 +669,13 @@ struct BridgeJSLink { "\(method.name)\(renderTSSignature(parameters: method.parameters, returnType: method.returnType, effects: method.effects));" ) } + for property in proto.properties { + let propertySignature = + property.isReadonly + ? "readonly \(property.name): \(property.type.tsType);" + : "\(property.name): \(property.type.tsType);" + printer.write(propertySignature) + } } printer.write("}") printer.nextLine() @@ -1842,13 +1911,15 @@ extension BridgeJSLink { let body: CodeFragmentPrinter let scope: JSGlueVariableScope let cleanupCode: CodeFragmentPrinter + let context: BridgeContext var parameterNames: [String] = [] var parameterForwardings: [String] = [] - init() { + init(context: BridgeContext = .importTS) { self.body = CodeFragmentPrinter() self.scope = JSGlueVariableScope() self.cleanupCode = CodeFragmentPrinter() + self.context = context } func liftSelf() { @@ -1856,7 +1927,7 @@ extension BridgeJSLink { } func liftParameter(param: Parameter) throws { - let liftingFragment = try IntrinsicJSFragment.liftParameter(type: param.type) + let liftingFragment = try IntrinsicJSFragment.liftParameter(type: param.type, context: context) assert( liftingFragment.parameters.count >= 1, "Lifting fragment should have at least one parameter to lift" @@ -1867,6 +1938,7 @@ extension BridgeJSLink { valuesToLift = [scope.variable(param.name)] } else { valuesToLift = liftingFragment.parameters.map { scope.variable(param.name + $0.capitalizedFirstLetter) } + parameterNames.append(contentsOf: valuesToLift) } let liftedValues = liftingFragment.printCode(valuesToLift, scope, body, cleanupCode) assert(liftedValues.count == 1, "Lifting fragment should produce exactly one value") @@ -1913,7 +1985,7 @@ extension BridgeJSLink { } private func call(callExpr: String, returnType: BridgeType) throws -> String? { - let loweringFragment = try IntrinsicJSFragment.lowerReturn(type: returnType) + let loweringFragment = try IntrinsicJSFragment.lowerReturn(type: returnType, context: context) let returnExpr: String? if loweringFragment.parameters.count == 0 { body.write("\(callExpr);") @@ -1933,7 +2005,7 @@ extension BridgeJSLink { func callConstructor(name: String) throws -> String? { let call = "new imports.\(name)(\(parameterForwardings.joined(separator: ", ")))" let type: BridgeType = .jsObject(name) - let loweringFragment = try IntrinsicJSFragment.lowerReturn(type: type) + let loweringFragment = try IntrinsicJSFragment.lowerReturn(type: type, context: context) return try lowerReturnValue(returnType: type, returnExpr: call, loweringFragment: loweringFragment) } @@ -1945,6 +2017,43 @@ extension BridgeJSLink { } func callPropertyGetter(name: String, returnType: BridgeType) throws -> String? { + if context == .protocolExport, case .optional(let wrappedType) = returnType { + let usesSideChannel: Bool + switch wrappedType { + case .string, .int, .float, .double, .jsObject, .swiftProtocol: + usesSideChannel = true + case .rawValueEnum: + usesSideChannel = true + case .bool, .caseEnum, .swiftHeapObject, .associatedValueEnum: + usesSideChannel = false + default: + usesSideChannel = false + } + + if usesSideChannel { + let resultVar = scope.variable("ret") + body.write( + "let \(resultVar) = \(JSGlueVariableScope.reservedSwift).memory.getObject(self).\(name);" + ) + + switch wrappedType { + case .string, .rawValueEnum(_, .string): + body.write("\(JSGlueVariableScope.reservedStorageToReturnString) = \(resultVar);") + case .int, .rawValueEnum(_, .int): + body.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalInt) = \(resultVar);") + case .float, .rawValueEnum(_, .float): + body.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalFloat) = \(resultVar);") + case .double, .rawValueEnum(_, .double): + body.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalDouble) = \(resultVar);") + case .jsObject, .swiftProtocol: + body.write("\(JSGlueVariableScope.reservedStorageToReturnString) = \(resultVar);") + default: + break + } + return nil // Side-channel types return nil (no direct return value) + } + } + return try call( callExpr: "\(JSGlueVariableScope.reservedSwift).memory.getObject(self).\(name)", returnType: returnType @@ -2417,12 +2526,58 @@ extension BridgeJSLink { return (funcLines, []) } + func renderProtocolProperty( + importObjectBuilder: ImportObjectBuilder, + protocol: ExportedProtocol, + property: ExportedProtocolProperty + ) throws { + let getterAbiName = ABINameGenerator.generateABIName( + baseName: property.name, + namespace: nil, + staticContext: nil, + operation: "get", + className: `protocol`.name + ) + + let getterThunkBuilder = ImportedThunkBuilder(context: .protocolExport) + getterThunkBuilder.liftSelf() + let returnExpr = try getterThunkBuilder.callPropertyGetter(name: property.name, returnType: property.type) + let getterLines = getterThunkBuilder.renderFunction( + name: getterAbiName, + returnExpr: returnExpr, + returnType: property.type + ) + importObjectBuilder.assignToImportObject(name: getterAbiName, function: getterLines) + + if !property.isReadonly { + let setterAbiName = ABINameGenerator.generateABIName( + baseName: property.name, + namespace: nil, + staticContext: nil, + operation: "set", + className: `protocol`.name + ) + let setterThunkBuilder = ImportedThunkBuilder(context: .protocolExport) + setterThunkBuilder.liftSelf() + try setterThunkBuilder.liftParameter( + param: Parameter(label: nil, name: "value", type: property.type) + ) + setterThunkBuilder.callPropertySetter(name: property.name, returnType: property.type) + let setterLines = setterThunkBuilder.renderFunction( + name: setterAbiName, + returnExpr: nil, + returnType: .void + ) + importObjectBuilder.assignToImportObject(name: setterAbiName, function: setterLines) + } + } + func renderProtocolMethod( importObjectBuilder: ImportObjectBuilder, protocol: ExportedProtocol, method: ExportedFunction ) throws { - let thunkBuilder = ImportedThunkBuilder() + let thunkBuilder = ImportedThunkBuilder(context: .protocolExport) thunkBuilder.liftSelf() for param in method.parameters { try thunkBuilder.liftParameter(param: param) diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift index ef431432..9cdf9d72 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift @@ -219,6 +219,20 @@ struct IntrinsicJSFragment: Sendable { } ) } + static func swiftHeapObjectLiftParameter(_ name: String) -> IntrinsicJSFragment { + return IntrinsicJSFragment( + parameters: ["pointer"], + printCode: { arguments, scope, printer, cleanupCode in + return ["\(name).__construct(\(arguments[0]))"] + } + ) + } + static let swiftHeapObjectLowerReturn = IntrinsicJSFragment( + parameters: ["value"], + printCode: { arguments, scope, printer, cleanupCode in + return ["\(arguments[0]).pointer"] + } + ) static func associatedEnumLowerParameter(enumBase: String) -> IntrinsicJSFragment { IntrinsicJSFragment( @@ -249,6 +263,76 @@ struct IntrinsicJSFragment: Sendable { ) } + static func optionalLiftParameter(wrappedType: BridgeType) throws -> IntrinsicJSFragment { + return IntrinsicJSFragment( + parameters: ["isSome", "wrappedValue"], + printCode: { arguments, scope, printer, cleanupCode in + let isSome = arguments[0] + let wrappedValue = arguments[1] + let resultExpr: String + + switch wrappedType { + case .int, .float, .double, .caseEnum: + resultExpr = "\(isSome) ? \(wrappedValue) : null" + case .bool: + resultExpr = "\(isSome) ? \(wrappedValue) !== 0 : null" + case .string: + let objectLabel = scope.variable("obj") + printer.write("let \(objectLabel);") + printer.write("if (\(isSome)) {") + printer.indent { + printer.write( + "\(objectLabel) = \(JSGlueVariableScope.reservedSwift).memory.getObject(\(wrappedValue));" + ) + printer.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(wrappedValue));") + } + printer.write("}") + resultExpr = "\(isSome) ? \(objectLabel) : null" + case .swiftHeapObject(let name): + resultExpr = "\(isSome) ? \(name).__construct(\(wrappedValue)) : null" + case .jsObject: + resultExpr = + "\(isSome) ? \(JSGlueVariableScope.reservedSwift).memory.getObject(\(wrappedValue)) : null" + case .rawValueEnum(_, let rawType): + switch rawType { + case .string: + let objectLabel = scope.variable("obj") + printer.write("let \(objectLabel);") + printer.write("if (\(isSome)) {") + printer.indent { + printer.write( + "\(objectLabel) = \(JSGlueVariableScope.reservedSwift).memory.getObject(\(wrappedValue));" + ) + printer.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(wrappedValue));") + } + printer.write("}") + resultExpr = "\(isSome) ? \(objectLabel) : null" + case .bool: + resultExpr = "\(isSome) ? \(wrappedValue) !== 0 : null" + default: + resultExpr = "\(isSome) ? \(wrappedValue) : null" + } + case .associatedValueEnum(let fullName): + let base = fullName.components(separatedBy: ".").last ?? fullName + let enumVar = scope.variable("enumValue") + printer.write("let \(enumVar);") + printer.write("if (\(isSome)) {") + printer.indent { + printer.write( + "\(enumVar) = enumHelpers.\(base).raise(\(wrappedValue), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s));" + ) + } + printer.write("}") + resultExpr = "\(isSome) ? \(enumVar) : null" + default: + resultExpr = "\(isSome) ? \(wrappedValue) : null" + } + + return [resultExpr] + } + ) + } + static func optionalLowerParameter(wrappedType: BridgeType) throws -> IntrinsicJSFragment { return IntrinsicJSFragment( parameters: ["value"], @@ -293,6 +377,9 @@ struct IntrinsicJSFragment: Sendable { cleanupCode.write("if (\(cleanupVar)) { \(cleanupVar)(); }") return ["+\(isSomeVar)", "\(isSomeVar) ? \(caseIdVar) : 0"] + case .rawValueEnum: + // Raw value enums with optional - falls through to handle based on raw type + return ["+\(isSomeVar)", "\(isSomeVar) ? \(value) : 0"] default: switch wrappedType { case .swiftHeapObject: @@ -409,6 +496,128 @@ struct IntrinsicJSFragment: Sendable { ) } + static func optionalLowerReturn(wrappedType: BridgeType) throws -> IntrinsicJSFragment { + switch wrappedType { + case .void, .optional, .namespaceEnum: + throw BridgeJSLinkError(message: "Unsupported optional wrapped type for protocol export: \(wrappedType)") + default: break + } + + return IntrinsicJSFragment( + parameters: ["value"], + printCode: { arguments, scope, printer, cleanupCode in + let value = arguments[0] + let isSomeVar = scope.variable("isSome") + printer.write("const \(isSomeVar) = \(value) != null;") + + switch wrappedType { + case .bool: + printer.write( + "bjs[\"swift_js_return_optional_bool\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? (\(value) ? 1 : 0) : 0);" + ) + case .int: + printer.write( + "bjs[\"swift_js_return_optional_int\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? (\(value) | 0) : 0);" + ) + case .caseEnum: + printer.write("return \(isSomeVar) ? (\(value) | 0) : -1;") + case .float: + printer.write( + "bjs[\"swift_js_return_optional_float\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? Math.fround(\(value)) : 0.0);" + ) + case .double: + printer.write( + "bjs[\"swift_js_return_optional_double\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? \(value) : 0.0);" + ) + case .string: + let bytesVar = scope.variable("bytes") + printer.write("if (\(isSomeVar)) {") + printer.indent { + printer.write( + "const \(bytesVar) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(value));" + ) + printer.write("bjs[\"swift_js_return_optional_string\"](1, \(bytesVar), \(bytesVar).length);") + printer.write("return \(bytesVar).length;") + } + printer.write("} else {") + printer.indent { + printer.write("bjs[\"swift_js_return_optional_string\"](0, 0, 0);") + printer.write("return -1;") + } + printer.write("}") + case .jsObject, .swiftProtocol: + let idVar = scope.variable("id") + printer.write("let \(idVar) = 0;") + printer.write("if (\(isSomeVar)) {") + printer.indent { + printer.write("\(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(value));") + } + printer.write("}") + printer.write("bjs[\"swift_js_return_optional_object\"](\(isSomeVar) ? 1 : 0, \(idVar));") + case .rawValueEnum(_, let rawType): + switch rawType { + case .string: + let bytesVar = scope.variable("bytes") + printer.write("if (\(isSomeVar)) {") + printer.indent { + printer.write( + "const \(bytesVar) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(value));" + ) + printer.write( + "bjs[\"swift_js_return_optional_string\"](1, \(bytesVar), \(bytesVar).length);" + ) + } + printer.write("} else {") + printer.indent { + printer.write("bjs[\"swift_js_return_optional_string\"](0, 0, 0);") + } + printer.write("}") + default: + switch rawType { + case .bool: + printer.write( + "bjs[\"swift_js_return_optional_bool\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? (\(value) ? 1 : 0) : 0);" + ) + case .float: + printer.write( + "bjs[\"swift_js_return_optional_float\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? Math.fround(\(value)) : 0.0);" + ) + case .double: + printer.write( + "bjs[\"swift_js_return_optional_double\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? \(value) : 0.0);" + ) + default: + printer.write( + "bjs[\"swift_js_return_optional_int\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? (\(value) | 0) : 0);" + ) + } + } + case .associatedValueEnum(let fullName): + let base = fullName.components(separatedBy: ".").last ?? fullName + let caseIdVar = scope.variable("caseId") + let cleanupVar = scope.variable("cleanup") + printer.write("if (\(isSomeVar)) {") + printer.indent { + printer.write( + "const { caseId: \(caseIdVar), cleanup: \(cleanupVar) } = enumHelpers.\(base).lower(\(value));" + ) + printer.write("return \(caseIdVar);") + } + printer.write("} else {") + printer.indent { + printer.write("return -1;") + } + printer.write("}") + cleanupCode.write("if (\(cleanupVar)) { \(cleanupVar)(); }") + default: + () + } + + return [] + } + ) + } + // MARK: - ExportSwift /// Returns a fragment that lowers a JS value to Wasm core values for parameters @@ -468,26 +677,35 @@ struct IntrinsicJSFragment: Sendable { // MARK: - ImportedJS /// Returns a fragment that lifts Wasm core values to JS values for parameters - static func liftParameter(type: BridgeType) throws -> IntrinsicJSFragment { + static func liftParameter(type: BridgeType, context: BridgeContext = .importTS) throws -> IntrinsicJSFragment { switch type { case .int, .float, .double: return .identity case .bool: return .boolLiftParameter case .string: return .stringLiftParameter case .jsObject: return .jsObjectLiftParameter case .swiftHeapObject(let name): - throw BridgeJSLinkError( - message: - "Swift heap objects are not supported to be passed as parameters to imported JS functions: \(name)" - ) + switch context { + case .importTS: + throw BridgeJSLinkError( + message: "swiftHeapObject '\(name)' can only be used in protocol exports, not in \(context)" + ) + case .protocolExport: + return .swiftHeapObjectLiftParameter(name) + } case .swiftProtocol: return .jsObjectLiftParameter case .void: throw BridgeJSLinkError( message: "Void can't appear in parameters of imported JS functions" ) case .optional(let wrappedType): - throw BridgeJSLinkError( - message: "Optional types are not supported for imported JS functions: \(wrappedType)" - ) + switch context { + case .importTS: + throw BridgeJSLinkError( + message: "Optional types are not supported for imported JS functions: \(wrappedType)" + ) + case .protocolExport: + return try .optionalLiftParameter(wrappedType: wrappedType) + } case .caseEnum: return .identity case .rawValueEnum(_, let rawType): switch rawType { @@ -495,11 +713,27 @@ struct IntrinsicJSFragment: Sendable { case .bool: return .boolLiftParameter default: return .identity } - case .associatedValueEnum(let string): - throw BridgeJSLinkError( - message: - "Associated value enums are not supported to be passed as parameters to imported JS functions: \(string)" - ) + case .associatedValueEnum(let fullName): + switch context { + case .importTS: + throw BridgeJSLinkError( + message: + "Associated value enums are not supported to be passed as parameters to imported JS functions: \(fullName)" + ) + case .protocolExport: + let base = fullName.components(separatedBy: ".").last ?? fullName + return IntrinsicJSFragment( + parameters: ["caseId"], + printCode: { arguments, scope, printer, cleanupCode in + let caseId = arguments[0] + let resultVar = scope.variable("enumValue") + printer.write( + "const \(resultVar) = enumHelpers.\(base).raise(\(caseId), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s));" + ) + return [resultVar] + } + ) + } case .namespaceEnum(let string): throw BridgeJSLinkError( message: @@ -509,22 +743,33 @@ struct IntrinsicJSFragment: Sendable { } /// Returns a fragment that lowers a JS value to Wasm core values for return values - static func lowerReturn(type: BridgeType) throws -> IntrinsicJSFragment { + static func lowerReturn(type: BridgeType, context: BridgeContext = .importTS) throws -> IntrinsicJSFragment { switch type { case .int, .float, .double: return .identity case .bool: return .boolLowerReturn case .string: return .stringLowerReturn case .jsObject: return .jsObjectLowerReturn - case .swiftHeapObject: - throw BridgeJSLinkError( - message: "Swift heap objects are not supported to be returned from imported JS functions" - ) + case .swiftHeapObject(let name): + switch context { + case .importTS: + throw BridgeJSLinkError( + message: "swiftHeapObject '\(name)' can only be used in protocol exports, not in \(context)" + ) + case .protocolExport: + return .swiftHeapObjectLowerReturn + } case .swiftProtocol: return .jsObjectLowerReturn case .void: return .void case .optional(let wrappedType): - throw BridgeJSLinkError( - message: "Optional types are not supported for imported JS functions: \(wrappedType)" - ) + switch context { + case .importTS: + throw BridgeJSLinkError( + message: "Optional types are not supported for imported JS functions: \(wrappedType)" + ) + case .protocolExport: + // Protocol exports support Optional - use side channel approach for return values + return try .optionalLowerReturn(wrappedType: wrappedType) + } case .caseEnum: return .identity case .rawValueEnum(_, let rawType): switch rawType { @@ -532,10 +777,16 @@ struct IntrinsicJSFragment: Sendable { case .bool: return .boolLowerReturn default: return .identity } - case .associatedValueEnum(let string): - throw BridgeJSLinkError( - message: "Associated value enums are not supported to be returned from imported JS functions: \(string)" - ) + case .associatedValueEnum(let fullName): + switch context { + case .importTS: + throw BridgeJSLinkError( + message: + "Associated value enums are not supported to be returned from imported JS functions: \(fullName)" + ) + case .protocolExport: + return associatedValueLowerReturn(fullName: fullName) + } case .namespaceEnum(let string): throw BridgeJSLinkError( message: "Namespace enums are not supported to be returned from imported JS functions: \(string)" @@ -545,6 +796,23 @@ struct IntrinsicJSFragment: Sendable { // MARK: - Enums Payload Fragments + static func associatedValueLowerReturn(fullName: String) -> IntrinsicJSFragment { + let base = fullName.components(separatedBy: ".").last ?? fullName + return IntrinsicJSFragment( + parameters: ["value"], + printCode: { arguments, scope, printer, cleanupCode in + let value = arguments[0] + let caseIdVar = scope.variable("caseId") + let cleanupVar = scope.variable("cleanup") + printer.write( + "const { caseId: \(caseIdVar), cleanup: \(cleanupVar) } = enumHelpers.\(base).lower(\(value));" + ) + cleanupCode.write("if (\(cleanupVar)) { \(cleanupVar)(); }") + printer.write("return \(caseIdVar);") + return [] + } + ) + } /// Fragment for generating an entire associated value enum helper static func associatedValueEnumHelper(enumDefinition: ExportedEnum) -> IntrinsicJSFragment { return IntrinsicJSFragment( @@ -854,26 +1122,27 @@ struct IntrinsicJSFragment: Sendable { cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));") } cleanup.write("}") + case .int: + printer.write( + "\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? (\(value) | 0) : 0);" + ) + printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);") + case .bool: + printer.write( + "\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? (\(value) ? 1 : 0) : 0);" + ) + printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);") + case .float: + printer.write( + "\(JSGlueVariableScope.reservedTmpParamF32s).push(\(isSomeVar) ? Math.fround(\(value)) : 0.0);" + ) + printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);") + case .double: + printer.write( + "\(JSGlueVariableScope.reservedTmpParamF64s).push(\(isSomeVar) ? \(value) : 0.0);" + ) + printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);") default: - switch wrappedType { - case .int: - printer.write( - "\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? (\(value) | 0) : 0);" - ) - case .bool: - printer.write( - "\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? (\(value) ? 1 : 0) : 0);" - ) - case .float: - printer.write( - "\(JSGlueVariableScope.reservedTmpParamF32s).push(\(isSomeVar) ? Math.fround(\(value)) : 0.0);" - ) - case .double: - printer.write( - "\(JSGlueVariableScope.reservedTmpParamF64s).push(\(isSomeVar) ? \(value) : 0.0);" - ) - default: () - } printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);") } diff --git a/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift b/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift index 1a414b95..c9a60275 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift @@ -9,8 +9,8 @@ public struct ABINameGenerator { /// Generates ABI name using standardized namespace + context pattern public static func generateABIName( baseName: String, - namespace: [String]?, - staticContext: StaticContext?, + namespace: [String]? = nil, + staticContext: StaticContext? = nil, operation: String? = nil, className: String? = nil ) -> String { @@ -67,6 +67,12 @@ public struct ABINameGenerator { // MARK: - Types +/// Context for bridge operations that determines which types are valid +public enum BridgeContext: Sendable { + case importTS + case protocolExport +} + public enum BridgeType: Codable, Equatable, Sendable { case int, float, double, string, bool, jsObject(String?), swiftHeapObject(String), void indirect case optional(BridgeType) @@ -270,14 +276,37 @@ public enum EnumType: String, Codable, Sendable { // MARK: - Exported Skeleton +public struct ExportedProtocolProperty: Codable, Equatable, Sendable { + public let name: String + public let type: BridgeType + public let isReadonly: Bool + + public init( + name: String, + type: BridgeType, + isReadonly: Bool + ) { + self.name = name + self.type = type + self.isReadonly = isReadonly + } +} + public struct ExportedProtocol: Codable, Equatable { public let name: String public let methods: [ExportedFunction] + public let properties: [ExportedProtocolProperty] public let namespace: [String]? - public init(name: String, methods: [ExportedFunction], namespace: [String]? = nil) { + public init( + name: String, + methods: [ExportedFunction], + properties: [ExportedProtocolProperty] = [], + namespace: [String]? = nil + ) { self.name = name self.methods = methods + self.properties = properties self.namespace = namespace } } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Protocol.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Protocol.swift index f0213369..689f4a35 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Protocol.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Protocol.swift @@ -1,11 +1,64 @@ import JavaScriptKit +@JS enum Direction { + case north + case south + case east + case west +} + +@JS enum ExampleEnum: String { + case test = "test" + case test2 = "test2" +} + +@JS enum Result { + case success(String) + case failure(Int) +} + +@JS enum Priority: Int { + case low = -1 + case medium = 0 + case high = 1 +} + +@JS class Helper { + @JS var value: Int + + @JS init(value: Int) { + self.value = value + } + + @JS func increment() { + value += 1 + } +} + @JS protocol MyViewControllerDelegate { + var eventCount: Int { get set } + var delegateName: String { get } + var optionalName: String? { get set } + var optionalRawEnum: ExampleEnum? { get set } + var rawStringEnum: ExampleEnum { get set } + var result: Result { get set } + var optionalResult: Result? { get set } + var direction: Direction { get set } + var directionOptional: Direction? { get set } + var priority: Priority { get set } + var priorityOptional: Priority? { get set } func onSomethingHappened() func onValueChanged(_ value: String) func onCountUpdated(count: Int) -> Bool func onLabelUpdated(_ prefix: String, _ suffix: String) func isCountEven() -> Bool + func onHelperUpdated(_ helper: Helper) + func createHelper() -> Helper + func onOptionalHelperUpdated(_ helper: Helper?) + func createOptionalHelper() -> Helper? + func createEnum() -> ExampleEnum + func handleResult(_ result: Result) + func getResult() -> Result } @JS class MyViewController { @@ -38,4 +91,8 @@ import JavaScriptKit @JS func checkEvenCount() -> Bool { return delegate.isCountEven() } + + @JS func sendHelper(_ helper: Helper) { + delegate.onHelperUpdated(helper) + } } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js index 7295699a..c9485c39 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayParameter.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_checkArray"] = function bjs_checkArray(a) { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Export.js index 92f10f94..a763e45f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Import.js index 74fb30c5..b5537ddc 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_asyncReturnVoid"] = function bjs_asyncReturnVoid() { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.Export.js index dac7bdfc..a2b07002 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.Export.js @@ -144,6 +144,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.Export.js index ea0aef15..b6518d71 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.Export.js @@ -622,6 +622,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.Export.js index 9cbed9a2..04eda063 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.Export.js @@ -162,6 +162,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Export.js index b934456f..9e645a67 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Export.js @@ -182,6 +182,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js index 6b56108a..15b03a4b 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js @@ -213,6 +213,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js index cead874b..51921ddc 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Interface.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_returnAnimatable"] = function bjs_returnAnimatable() { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.Import.js index f469072f..b6694c06 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_createArrayBuffer"] = function bjs_createArrayBuffer() { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MultipleImportedTypes.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MultipleImportedTypes.Import.js index 116933d7..3253d17d 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MultipleImportedTypes.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MultipleImportedTypes.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_createDatabaseConnection"] = function bjs_createDatabaseConnection(config) { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Export.js index 5044b4b1..89dd9023 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.Export.js index f67a06e8..319556b9 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js index eea05ba1..17e1a1e5 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js index 882e633e..e8d18b98 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_check"] = function bjs_check(a, b) { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js index 7c20fa15..c7efcf0d 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js index 481cc495..24fa28a0 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_checkNumber"] = function bjs_checkNumber() { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.Export.js index bbfbb0ac..bc9748d8 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.d.ts b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.d.ts index 0f5d0fb2..b7fddd42 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.d.ts +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.d.ts @@ -10,8 +10,65 @@ export interface MyViewControllerDelegate { onCountUpdated(count: number): boolean; onLabelUpdated(prefix: string, suffix: string): void; isCountEven(): boolean; + onHelperUpdated(helper: Helper): void; + createHelper(): Helper; + onOptionalHelperUpdated(helper: Helper | null): void; + createOptionalHelper(): Helper | null; + createEnum(): ExampleEnumTag; + handleResult(result: ResultTag): void; + getResult(): ResultTag; + eventCount: number; + readonly delegateName: string; + optionalName: string | null; + optionalRawEnum: ExampleEnumTag | null; + rawStringEnum: ExampleEnumTag; + result: ResultTag; + optionalResult: ResultTag | null; + direction: DirectionTag; + directionOptional: DirectionTag | null; + priority: PriorityTag; + priorityOptional: PriorityTag | null; } +export const DirectionValues: { + readonly North: 0; + readonly South: 1; + readonly East: 2; + readonly West: 3; +}; +export type DirectionTag = typeof DirectionValues[keyof typeof DirectionValues]; + +export const ExampleEnumValues: { + readonly Test: "test"; + readonly Test2: "test2"; +}; +export type ExampleEnumTag = typeof ExampleEnumValues[keyof typeof ExampleEnumValues]; + +export const ResultValues: { + readonly Tag: { + readonly Success: 0; + readonly Failure: 1; + }; +}; + +export type ResultTag = + { tag: typeof ResultValues.Tag.Success; param0: string } | { tag: typeof ResultValues.Tag.Failure; param0: number } + +export const PriorityValues: { + readonly Low: -1; + readonly Medium: 0; + readonly High: 1; +}; +export type PriorityTag = typeof PriorityValues[keyof typeof PriorityValues]; + +export type DirectionObject = typeof DirectionValues; + +export type ExampleEnumObject = typeof ExampleEnumValues; + +export type ResultObject = typeof ResultValues; + +export type PriorityObject = typeof PriorityValues; + /// Represents a Swift heap object like a class instance or an actor instance. export interface SwiftHeapObject { /// Release the heap object. @@ -19,19 +76,31 @@ export interface SwiftHeapObject { /// Note: Calling this method will release the heap object and it will no longer be accessible. release(): void; } +export interface Helper extends SwiftHeapObject { + increment(): void; + value: number; +} export interface MyViewController extends SwiftHeapObject { triggerEvent(): void; updateValue(value: string): void; updateCount(count: number): boolean; updateLabel(prefix: string, suffix: string): void; checkEvenCount(): boolean; + sendHelper(helper: Helper): void; delegate: MyViewControllerDelegate; secondDelegate: MyViewControllerDelegate | null; } export type Exports = { + Helper: { + new(value: number): Helper; + } MyViewController: { new(delegate: MyViewControllerDelegate): MyViewController; } + Direction: DirectionObject + ExampleEnum: ExampleEnumObject + Result: ResultObject + Priority: PriorityObject } export type Imports = { } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.js index 142c1cf0..2740c7c2 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.js @@ -4,6 +4,70 @@ // To update this file, just rebuild your project or run // `swift package bridge-js`. +export const DirectionValues = { + North: 0, + South: 1, + East: 2, + West: 3, +}; + +export const ExampleEnumValues = { + Test: "test", + Test2: "test2", +}; + +export const ResultValues = { + Tag: { + Success: 0, + Failure: 1, + }, +}; + +const __bjs_createResultValuesHelpers = () => { + return (tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift) => ({ + lower: (value) => { + const enumTag = value.tag; + switch (enumTag) { + case ResultValues.Tag.Success: { + const bytes = textEncoder.encode(value.param0); + const id = swift.memory.retain(bytes); + tmpParamInts.push(bytes.length); + tmpParamInts.push(id); + const cleanup = () => { + swift.memory.release(id); + }; + return { caseId: ResultValues.Tag.Success, cleanup }; + } + case ResultValues.Tag.Failure: { + tmpParamInts.push((value.param0 | 0)); + const cleanup = undefined; + return { caseId: ResultValues.Tag.Failure, cleanup }; + } + default: throw new Error("Unknown ResultValues tag: " + String(enumTag)); + } + }, + raise: (tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s) => { + const tag = tmpRetTag | 0; + switch (tag) { + case ResultValues.Tag.Success: { + const string = tmpRetStrings.pop(); + return { tag: ResultValues.Tag.Success, param0: string }; + } + case ResultValues.Tag.Failure: { + const int = tmpRetInts.pop(); + return { tag: ResultValues.Tag.Failure, param0: int }; + } + default: throw new Error("Unknown ResultValues tag returned from Swift: " + String(tag)); + } + } + }); +}; +export const PriorityValues = { + Low: -1, + Medium: 0, + High: 1, +}; + export async function createInstantiator(options, swift) { let instance; let memory; @@ -26,6 +90,7 @@ export async function createInstantiator(options, swift) { let tmpParamInts = []; let tmpParamF32s = []; let tmpParamF64s = []; + const enumHelpers = {}; return { /** @@ -138,15 +203,242 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; } + importObject["TestModule"]["bjs_Helper_wrap"] = function(pointer) { + const obj = Helper.__construct(pointer); + return swift.memory.retain(obj); + }; importObject["TestModule"]["bjs_MyViewController_wrap"] = function(pointer) { const obj = MyViewController.__construct(pointer); return swift.memory.retain(obj); }; const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; + TestModule["bjs_MyViewControllerDelegate_eventCount_get"] = function bjs_MyViewControllerDelegate_eventCount_get(self) { + try { + let ret = swift.memory.getObject(self).eventCount; + return ret; + } catch (error) { + setException(error); + return 0 + } + } + TestModule["bjs_MyViewControllerDelegate_eventCount_set"] = function bjs_MyViewControllerDelegate_eventCount_set(self, value) { + try { + swift.memory.getObject(self).eventCount = value; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_delegateName_get"] = function bjs_MyViewControllerDelegate_delegateName_get(self) { + try { + let ret = swift.memory.getObject(self).delegateName; + tmpRetBytes = textEncoder.encode(ret); + return tmpRetBytes.length; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_optionalName_get"] = function bjs_MyViewControllerDelegate_optionalName_get(self) { + try { + let ret = swift.memory.getObject(self).optionalName; + tmpRetString = ret; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_optionalName_set"] = function bjs_MyViewControllerDelegate_optionalName_set(self, valueIsSome, valueWrappedValue) { + try { + let obj; + if (valueIsSome) { + obj = swift.memory.getObject(valueWrappedValue); + swift.memory.release(valueWrappedValue); + } + swift.memory.getObject(self).optionalName = valueIsSome ? obj : null; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_optionalRawEnum_get"] = function bjs_MyViewControllerDelegate_optionalRawEnum_get(self) { + try { + let ret = swift.memory.getObject(self).optionalRawEnum; + tmpRetString = ret; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_optionalRawEnum_set"] = function bjs_MyViewControllerDelegate_optionalRawEnum_set(self, valueIsSome, valueWrappedValue) { + try { + let obj; + if (valueIsSome) { + obj = swift.memory.getObject(valueWrappedValue); + swift.memory.release(valueWrappedValue); + } + swift.memory.getObject(self).optionalRawEnum = valueIsSome ? obj : null; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_rawStringEnum_get"] = function bjs_MyViewControllerDelegate_rawStringEnum_get(self) { + try { + let ret = swift.memory.getObject(self).rawStringEnum; + tmpRetBytes = textEncoder.encode(ret); + return tmpRetBytes.length; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_rawStringEnum_set"] = function bjs_MyViewControllerDelegate_rawStringEnum_set(self, value) { + try { + const valueObject = swift.memory.getObject(value); + swift.memory.release(value); + swift.memory.getObject(self).rawStringEnum = valueObject; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_result_get"] = function bjs_MyViewControllerDelegate_result_get(self) { + try { + let ret = swift.memory.getObject(self).result; + const { caseId: caseId, cleanup: cleanup } = enumHelpers.Result.lower(ret); + return caseId; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_result_set"] = function bjs_MyViewControllerDelegate_result_set(self, value) { + try { + const enumValue = enumHelpers.Result.raise(value, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s); + swift.memory.getObject(self).result = enumValue; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_optionalResult_get"] = function bjs_MyViewControllerDelegate_optionalResult_get(self) { + try { + let ret = swift.memory.getObject(self).optionalResult; + const isSome = ret != null; + if (isSome) { + const { caseId: caseId, cleanup: cleanup } = enumHelpers.Result.lower(ret); + return caseId; + } else { + return -1; + } + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_optionalResult_set"] = function bjs_MyViewControllerDelegate_optionalResult_set(self, valueIsSome, valueWrappedValue) { + try { + let enumValue; + if (valueIsSome) { + enumValue = enumHelpers.Result.raise(valueWrappedValue, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s); + } + swift.memory.getObject(self).optionalResult = valueIsSome ? enumValue : null; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_direction_get"] = function bjs_MyViewControllerDelegate_direction_get(self) { + try { + let ret = swift.memory.getObject(self).direction; + return ret; + } catch (error) { + setException(error); + return 0 + } + } + TestModule["bjs_MyViewControllerDelegate_direction_set"] = function bjs_MyViewControllerDelegate_direction_set(self, value) { + try { + swift.memory.getObject(self).direction = value; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_directionOptional_get"] = function bjs_MyViewControllerDelegate_directionOptional_get(self) { + try { + let ret = swift.memory.getObject(self).directionOptional; + const isSome = ret != null; + return isSome ? (ret | 0) : -1; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_directionOptional_set"] = function bjs_MyViewControllerDelegate_directionOptional_set(self, valueIsSome, valueWrappedValue) { + try { + swift.memory.getObject(self).directionOptional = valueIsSome ? valueWrappedValue : null; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_priority_get"] = function bjs_MyViewControllerDelegate_priority_get(self) { + try { + let ret = swift.memory.getObject(self).priority; + return ret; + } catch (error) { + setException(error); + return 0 + } + } + TestModule["bjs_MyViewControllerDelegate_priority_set"] = function bjs_MyViewControllerDelegate_priority_set(self, value) { + try { + swift.memory.getObject(self).priority = value; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_priorityOptional_get"] = function bjs_MyViewControllerDelegate_priorityOptional_get(self) { + try { + let ret = swift.memory.getObject(self).priorityOptional; + tmpRetOptionalInt = ret; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_priorityOptional_set"] = function bjs_MyViewControllerDelegate_priorityOptional_set(self, valueIsSome, valueWrappedValue) { + try { + swift.memory.getObject(self).priorityOptional = valueIsSome ? valueWrappedValue : null; + } catch (error) { + setException(error); + } + } TestModule["bjs_MyViewControllerDelegate_onSomethingHappened"] = function bjs_MyViewControllerDelegate_onSomethingHappened(self) { try { swift.memory.getObject(self).onSomethingHappened(); @@ -192,11 +484,71 @@ export async function createInstantiator(options, swift) { return 0 } } + TestModule["bjs_MyViewControllerDelegate_onHelperUpdated"] = function bjs_MyViewControllerDelegate_onHelperUpdated(self, helper) { + try { + swift.memory.getObject(self).onHelperUpdated(Helper.__construct(helper)); + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_createHelper"] = function bjs_MyViewControllerDelegate_createHelper(self) { + try { + let ret = swift.memory.getObject(self).createHelper(); + return ret.pointer; + } catch (error) { + setException(error); + return 0 + } + } + TestModule["bjs_MyViewControllerDelegate_onOptionalHelperUpdated"] = function bjs_MyViewControllerDelegate_onOptionalHelperUpdated(self, helperIsSome, helperWrappedValue) { + try { + swift.memory.getObject(self).onOptionalHelperUpdated(helperIsSome ? Helper.__construct(helperWrappedValue) : null); + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_createOptionalHelper"] = function bjs_MyViewControllerDelegate_createOptionalHelper(self) { + try { + let ret = swift.memory.getObject(self).createOptionalHelper(); + const isSome = ret != null; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_createEnum"] = function bjs_MyViewControllerDelegate_createEnum(self) { + try { + let ret = swift.memory.getObject(self).createEnum(); + tmpRetBytes = textEncoder.encode(ret); + return tmpRetBytes.length; + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_handleResult"] = function bjs_MyViewControllerDelegate_handleResult(self, result) { + try { + const enumValue = enumHelpers.Result.raise(result, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s); + swift.memory.getObject(self).handleResult(enumValue); + } catch (error) { + setException(error); + } + } + TestModule["bjs_MyViewControllerDelegate_getResult"] = function bjs_MyViewControllerDelegate_getResult(self) { + try { + let ret = swift.memory.getObject(self).getResult(); + const { caseId: caseId, cleanup: cleanup } = enumHelpers.Result.lower(ret); + return caseId; + } catch (error) { + setException(error); + } + } }, setInstance: (i) => { instance = i; memory = instance.exports.memory; + const ResultHelpers = __bjs_createResultValuesHelpers()(tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift); + enumHelpers.Result = ResultHelpers; + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } @@ -223,6 +575,26 @@ export async function createInstantiator(options, swift) { this.deinit(this.pointer); } } + class Helper extends SwiftHeapObject { + static __construct(ptr) { + return SwiftHeapObject.__wrap(ptr, instance.exports.bjs_Helper_deinit, Helper.prototype); + } + + constructor(value) { + const ret = instance.exports.bjs_Helper_init(value); + return Helper.__construct(ret); + } + increment() { + instance.exports.bjs_Helper_increment(this.pointer); + } + get value() { + const ret = instance.exports.bjs_Helper_value_get(this.pointer); + return ret; + } + set value(value) { + instance.exports.bjs_Helper_value_set(this.pointer, value); + } + } class MyViewController extends SwiftHeapObject { static __construct(ptr) { return SwiftHeapObject.__wrap(ptr, instance.exports.bjs_MyViewController_deinit, MyViewController.prototype); @@ -258,6 +630,9 @@ export async function createInstantiator(options, swift) { const ret = instance.exports.bjs_MyViewController_checkEvenCount(this.pointer); return ret !== 0; } + sendHelper(helper) { + instance.exports.bjs_MyViewController_sendHelper(this.pointer, helper.pointer); + } get delegate() { const ret = instance.exports.bjs_MyViewController_delegate_get(this.pointer); const ret1 = swift.memory.getObject(ret); @@ -279,7 +654,12 @@ export async function createInstantiator(options, swift) { } } return { + Helper, MyViewController, + Direction: DirectionValues, + ExampleEnum: ExampleEnumValues, + Result: ResultValues, + Priority: PriorityValues, }; }, } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Export.js index 44ad0a93..6fa84a4f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Export.js @@ -190,6 +190,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Export.js index eb1c5ad1..29060b93 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Export.js @@ -143,6 +143,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js index b7d4b505..a9f4bcd7 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js index bc109fc9..4f6e1dae 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_checkString"] = function bjs_checkString(a) { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js index 08eef7e0..2d9a848f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js index 11d30446..80d4fcd9 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_checkString"] = function bjs_checkString() { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js index 0a545a0e..395057d8 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } // Wrapper functions for module: TestModule if (!importObject["TestModule"]) { importObject["TestModule"] = {}; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TS2SkeletonLike.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TS2SkeletonLike.Import.js index 453d5f76..a1d39cd2 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TS2SkeletonLike.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TS2SkeletonLike.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_createTS2Skeleton"] = function bjs_createTS2Skeleton() { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.js index af17ab14..f2e0bfc9 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js index 9ba7cf60..7c4438b0 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeAlias.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_checkSimple"] = function bjs_checkSimple(a) { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js index 98fb575d..49b0b331 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/TypeScriptClass.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_Greeter_init"] = function bjs_Greeter_init(name) { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js index d2716dfa..a0f6f0dd 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Export.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } }, setInstance: (i) => { instance = i; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js index 4b4c75ea..bd1d9bfb 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.Import.js @@ -138,6 +138,41 @@ export async function createInstantiator(options, swift) { tmpRetOptionalHeapObject = pointer; } } + bjs["swift_js_get_optional_int_presence"] = function() { + return tmpRetOptionalInt != null ? 1 : 0; + } + bjs["swift_js_get_optional_int_value"] = function() { + const value = tmpRetOptionalInt; + tmpRetOptionalInt = undefined; + return value; + } + bjs["swift_js_get_optional_string"] = function() { + const str = tmpRetString; + tmpRetString = undefined; + if (str == null) { + return -1; + } else { + const bytes = textEncoder.encode(str); + tmpRetBytes = bytes; + return bytes.length; + } + } + bjs["swift_js_get_optional_float_presence"] = function() { + return tmpRetOptionalFloat != null ? 1 : 0; + } + bjs["swift_js_get_optional_float_value"] = function() { + const value = tmpRetOptionalFloat; + tmpRetOptionalFloat = undefined; + return value; + } + bjs["swift_js_get_optional_double_presence"] = function() { + return tmpRetOptionalDouble != null ? 1 : 0; + } + bjs["swift_js_get_optional_double_value"] = function() { + const value = tmpRetOptionalDouble; + tmpRetOptionalDouble = undefined; + return value; + } const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_check"] = function bjs_check() { try { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/DefaultParameters.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/DefaultParameters.swift index fd382c0a..675f7bfb 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/DefaultParameters.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/DefaultParameters.swift @@ -11,13 +11,13 @@ extension Status: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Status { - return Status(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Status { return Status(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumAssociatedValue.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumAssociatedValue.swift index 008eeda0..0c70fe5c 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumAssociatedValue.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumAssociatedValue.swift @@ -7,7 +7,7 @@ @_spi(BridgeJS) import JavaScriptKit extension APIResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> APIResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -26,6 +26,43 @@ extension APIResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0): + _swift_js_push_int(Int32(param0)) + return Int32(1) + case .flag(let param0): + _swift_js_push_int(param0 ? 1 : 0) + return Int32(2) + case .rate(let param0): + _swift_js_push_f32(param0) + return Int32(3) + case .precise(let param0): + _swift_js_push_f64(param0) + return Int32(4) + case .info: + return Int32(5) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -53,7 +90,7 @@ extension APIResult: _BridgedSwiftAssociatedValueEnum { } extension ComplexResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> ComplexResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> ComplexResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -72,6 +109,71 @@ extension ComplexResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .error(let param0, let param1): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + _swift_js_push_int(Int32(param1)) + return Int32(1) + case .status(let param0, let param1, let param2): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(Int32(param1)) + var __bjs_param2 = param2 + __bjs_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(2) + case .coordinates(let param0, let param1, let param2): + _swift_js_push_f64(param0) + _swift_js_push_f64(param1) + _swift_js_push_f64(param2) + return Int32(3) + case .comprehensive(let param0, let param1, let param2, let param3, let param4, let param5, let param6, let param7, let param8): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(param1 ? 1 : 0) + _swift_js_push_int(Int32(param2)) + _swift_js_push_int(Int32(param3)) + _swift_js_push_f64(param4) + _swift_js_push_f64(param5) + var __bjs_param6 = param6 + __bjs_param6.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + var __bjs_param7 = param7 + __bjs_param7.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + var __bjs_param8 = param8 + __bjs_param8.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(4) + case .info: + return Int32(5) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> ComplexResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> ComplexResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -127,7 +229,7 @@ extension ComplexResult: _BridgedSwiftAssociatedValueEnum { } extension Utilities.Result: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> Utilities.Result { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> Utilities.Result { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -140,6 +242,44 @@ extension Utilities.Result: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0, let param1): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + _swift_js_push_int(Int32(param1)) + return Int32(1) + case .status(let param0, let param1, let param2): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(Int32(param1)) + var __bjs_param2 = param2 + __bjs_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(2) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> Utilities.Result { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> Utilities.Result { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -168,7 +308,7 @@ extension Utilities.Result: _BridgedSwiftAssociatedValueEnum { } extension NetworkingResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> NetworkingResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> NetworkingResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -179,6 +319,36 @@ extension NetworkingResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0, let param1): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + _swift_js_push_int(Int32(param1)) + return Int32(1) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> NetworkingResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> NetworkingResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -199,7 +369,7 @@ extension NetworkingResult: _BridgedSwiftAssociatedValueEnum { } extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIOptionalResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> APIOptionalResult { switch caseId { case 0: return .success(Optional.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -212,6 +382,65 @@ extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + let __bjs_isSome_param0 = param0 != nil + if let __bjs_unwrapped_param0 = param0 { + var __bjs_str_param0 = __bjs_unwrapped_param0 + __bjs_str_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + } + _swift_js_push_int(__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 { + _swift_js_push_int(Int32(__bjs_unwrapped_param0)) + } + _swift_js_push_int(__bjs_isSome_param0 ? 1 : 0) + let __bjs_isSome_param1 = param1 != nil + if let __bjs_unwrapped_param1 = param1 { + _swift_js_push_int(__bjs_unwrapped_param1 ? 1 : 0) + } + _swift_js_push_int(__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 { + _swift_js_push_int(__bjs_unwrapped_param0 ? 1 : 0) + } + _swift_js_push_int(__bjs_isSome_param0 ? 1 : 0) + let __bjs_isSome_param1 = param1 != nil + if let __bjs_unwrapped_param1 = param1 { + _swift_js_push_int(Int32(__bjs_unwrapped_param1)) + } + _swift_js_push_int(__bjs_isSome_param1 ? 1 : 0) + let __bjs_isSome_param2 = param2 != nil + if let __bjs_unwrapped_param2 = param2 { + var __bjs_str_param2 = __bjs_unwrapped_param2 + __bjs_str_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + } + _swift_js_push_int(__bjs_isSome_param2 ? 1 : 0) + return Int32(2) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> APIOptionalResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIOptionalResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumCase.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumCase.swift index 4a1be41e..06bfa86a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumCase.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumCase.swift @@ -11,13 +11,13 @@ extension Direction: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Direction { - return Direction(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Direction { return Direction(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -54,13 +54,13 @@ extension Status: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Status { - return Status(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Status { return Status(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -93,13 +93,13 @@ extension TSDirection: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> TSDirection { - return TSDirection(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> TSDirection { return TSDirection(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -136,13 +136,13 @@ extension PublicStatus: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> PublicStatus { - return PublicStatus(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> PublicStatus { return PublicStatus(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumNamespace.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumNamespace.swift index daa5577a..ca201f3d 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumNamespace.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumNamespace.swift @@ -11,13 +11,13 @@ extension Networking.API.Method: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Networking.API.Method { - return Networking.API.Method(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Networking.API.Method { return Networking.API.Method(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -60,13 +60,13 @@ extension Internal.SupportedMethod: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Internal.SupportedMethod { - return Internal.SupportedMethod(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Internal.SupportedMethod { return Internal.SupportedMethod(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.json index 77327c45..46b96429 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.json @@ -1,5 +1,59 @@ { "classes" : [ + { + "constructor" : { + "abiName" : "bjs_Helper_init", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "parameters" : [ + { + "label" : "value", + "name" : "value", + "type" : { + "int" : { + + } + } + } + ] + }, + "methods" : [ + { + "abiName" : "bjs_Helper_increment", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "increment", + "parameters" : [ + + ], + "returnType" : { + "void" : { + + } + } + } + ], + "name" : "Helper", + "properties" : [ + { + "isReadonly" : false, + "isStatic" : false, + "name" : "value", + "type" : { + "int" : { + + } + } + } + ], + "swiftCallName" : "Helper" + }, { "constructor" : { "abiName" : "bjs_MyViewController_init", @@ -136,6 +190,31 @@ "returnType" : { "bool" : { + } + } + }, + { + "abiName" : "bjs_MyViewController_sendHelper", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "sendHelper", + "parameters" : [ + { + "label" : "_", + "name" : "helper", + "type" : { + "swiftHeapObject" : { + "_0" : "Helper" + } + } + } + ], + "returnType" : { + "void" : { + } } } @@ -171,7 +250,143 @@ } ], "enums" : [ + { + "cases" : [ + { + "associatedValues" : [ + + ], + "name" : "north" + }, + { + "associatedValues" : [ + + ], + "name" : "south" + }, + { + "associatedValues" : [ + + ], + "name" : "east" + }, + { + "associatedValues" : [ + ], + "name" : "west" + } + ], + "emitStyle" : "const", + "name" : "Direction", + "staticMethods" : [ + + ], + "staticProperties" : [ + + ], + "swiftCallName" : "Direction" + }, + { + "cases" : [ + { + "associatedValues" : [ + + ], + "name" : "test", + "rawValue" : "test" + }, + { + "associatedValues" : [ + + ], + "name" : "test2", + "rawValue" : "test2" + } + ], + "emitStyle" : "const", + "name" : "ExampleEnum", + "rawType" : "String", + "staticMethods" : [ + + ], + "staticProperties" : [ + + ], + "swiftCallName" : "ExampleEnum" + }, + { + "cases" : [ + { + "associatedValues" : [ + { + "type" : { + "string" : { + + } + } + } + ], + "name" : "success" + }, + { + "associatedValues" : [ + { + "type" : { + "int" : { + + } + } + } + ], + "name" : "failure" + } + ], + "emitStyle" : "const", + "name" : "Result", + "staticMethods" : [ + + ], + "staticProperties" : [ + + ], + "swiftCallName" : "Result" + }, + { + "cases" : [ + { + "associatedValues" : [ + + ], + "name" : "low", + "rawValue" : "-1" + }, + { + "associatedValues" : [ + + ], + "name" : "medium", + "rawValue" : "0" + }, + { + "associatedValues" : [ + + ], + "name" : "high", + "rawValue" : "1" + } + ], + "emitStyle" : "const", + "name" : "Priority", + "rawType" : "Int", + "staticMethods" : [ + + ], + "staticProperties" : [ + + ], + "swiftCallName" : "Priority" + } ], "functions" : [ @@ -297,9 +512,286 @@ } } + }, + { + "abiName" : "bjs_MyViewControllerDelegate_onHelperUpdated", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "onHelperUpdated", + "parameters" : [ + { + "label" : "_", + "name" : "helper", + "type" : { + "swiftHeapObject" : { + "_0" : "Helper" + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_MyViewControllerDelegate_createHelper", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "createHelper", + "parameters" : [ + + ], + "returnType" : { + "swiftHeapObject" : { + "_0" : "Helper" + } + } + }, + { + "abiName" : "bjs_MyViewControllerDelegate_onOptionalHelperUpdated", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "onOptionalHelperUpdated", + "parameters" : [ + { + "label" : "_", + "name" : "helper", + "type" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Helper" + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_MyViewControllerDelegate_createOptionalHelper", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "createOptionalHelper", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Helper" + } + } + } + } + }, + { + "abiName" : "bjs_MyViewControllerDelegate_createEnum", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "createEnum", + "parameters" : [ + + ], + "returnType" : { + "rawValueEnum" : { + "_0" : "ExampleEnum", + "_1" : "String" + } + } + }, + { + "abiName" : "bjs_MyViewControllerDelegate_handleResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "handleResult", + "parameters" : [ + { + "label" : "_", + "name" : "result", + "type" : { + "associatedValueEnum" : { + "_0" : "Result" + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_MyViewControllerDelegate_getResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getResult", + "parameters" : [ + + ], + "returnType" : { + "associatedValueEnum" : { + "_0" : "Result" + } + } } ], - "name" : "MyViewControllerDelegate" + "name" : "MyViewControllerDelegate", + "properties" : [ + { + "isReadonly" : false, + "name" : "eventCount", + "type" : { + "int" : { + + } + } + }, + { + "isReadonly" : true, + "name" : "delegateName", + "type" : { + "string" : { + + } + } + }, + { + "isReadonly" : false, + "name" : "optionalName", + "type" : { + "optional" : { + "_0" : { + "string" : { + + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "optionalRawEnum", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "ExampleEnum", + "_1" : "String" + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "rawStringEnum", + "type" : { + "rawValueEnum" : { + "_0" : "ExampleEnum", + "_1" : "String" + } + } + }, + { + "isReadonly" : false, + "name" : "result", + "type" : { + "associatedValueEnum" : { + "_0" : "Result" + } + } + }, + { + "isReadonly" : false, + "name" : "optionalResult", + "type" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "Result" + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "direction", + "type" : { + "caseEnum" : { + "_0" : "Direction" + } + } + }, + { + "isReadonly" : false, + "name" : "directionOptional", + "type" : { + "optional" : { + "_0" : { + "caseEnum" : { + "_0" : "Direction" + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "priority", + "type" : { + "rawValueEnum" : { + "_0" : "Priority", + "_1" : "Int" + } + } + }, + { + "isReadonly" : false, + "name" : "priorityOptional", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "Priority", + "_1" : "Int" + } + } + } + } + } + ] } ] } \ No newline at end of file diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.swift index 6ac515c7..d68ef81d 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Protocol.swift @@ -41,11 +41,376 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto return Bool.bridgeJSLiftReturn(ret) } + func onHelperUpdated(_ helper: Helper) { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_onHelperUpdated") + func _extern_onHelperUpdated(this: Int32, helper: UnsafeMutableRawPointer) + _extern_onHelperUpdated(this: Int32(bitPattern: jsObject.id), helper: helper.bridgeJSLowerParameter()) + } + + func createHelper() -> Helper { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_createHelper") + func _extern_createHelper(this: Int32) -> UnsafeMutableRawPointer + let ret = _extern_createHelper(this: Int32(bitPattern: jsObject.id)) + return Helper.bridgeJSLiftReturn(ret) + } + + func onOptionalHelperUpdated(_ helper: Optional) { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_onOptionalHelperUpdated") + func _extern_onOptionalHelperUpdated(this: Int32, helperIsSome: Int32, helperPointer: UnsafeMutableRawPointer) + let (helperIsSome, helperPointer) = helper.bridgeJSLowerParameterWithPresence() + _extern_onOptionalHelperUpdated(this: Int32(bitPattern: jsObject.id), helperIsSome: helperIsSome, helperPointer: helperPointer) + } + + func createOptionalHelper() -> Optional { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_createOptionalHelper") + func _extern_createOptionalHelper(this: Int32) -> UnsafeMutableRawPointer + let ret = _extern_createOptionalHelper(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + + func createEnum() -> ExampleEnum { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_createEnum") + func _extern_createEnum(this: Int32) -> Int32 + let ret = _extern_createEnum(this: Int32(bitPattern: jsObject.id)) + return ExampleEnum.bridgeJSLiftReturn(ret) + } + + func handleResult(_ result: Result) { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_handleResult") + func _extern_handleResult(this: Int32, result: Int32) + _extern_handleResult(this: Int32(bitPattern: jsObject.id), result: result.bridgeJSLowerParameter()) + } + + func getResult() -> Result { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_getResult") + func _extern_getResult(this: Int32) -> Int32 + let ret = _extern_getResult(this: Int32(bitPattern: jsObject.id)) + return Result.bridgeJSLiftReturn(ret) + } + + var eventCount: Int { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_eventCount_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Int.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_eventCount_set") + func _extern_set(this: Int32, value: Int32) + _extern_set(this: Int32(bitPattern: jsObject.id), value: newValue.bridgeJSLowerParameter()) + } + } + + var delegateName: String { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_delegateName_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return String.bridgeJSLiftReturn(ret) + } + } + + var optionalName: Optional { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalName_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturnFromSideChannel() + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalName_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var optionalRawEnum: Optional { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalRawEnum_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturnFromSideChannel() + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalRawEnum_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var rawStringEnum: ExampleEnum { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_rawStringEnum_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return ExampleEnum.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_rawStringEnum_set") + func _extern_set(this: Int32, value: Int32) + _extern_set(this: Int32(bitPattern: jsObject.id), value: newValue.bridgeJSLowerParameter()) + } + } + + var result: Result { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_result_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Result.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_result_set") + func _extern_set(this: Int32, caseId: Int32) + _extern_set(this: Int32(bitPattern: jsObject.id), caseId: newValue.bridgeJSLowerParameter()) + } + } + + var optionalResult: Optional { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalResult_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalResult_set") + func _extern_set(this: Int32, isSome: Int32, caseId: Int32) + let (isSome, caseId) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, caseId: caseId) + } + } + + var direction: Direction { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_direction_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Direction.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_direction_set") + func _extern_set(this: Int32, value: Int32) + _extern_set(this: Int32(bitPattern: jsObject.id), value: newValue.bridgeJSLowerParameter()) + } + } + + var directionOptional: Optional { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_directionOptional_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_directionOptional_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var priority: Priority { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_priority_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Priority.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_priority_set") + func _extern_set(this: Int32, value: Int32) + _extern_set(this: Int32(bitPattern: jsObject.id), value: newValue.bridgeJSLowerParameter()) + } + } + + var priorityOptional: Optional { + get { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_priorityOptional_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturnFromSideChannel() + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_priorityOptional_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + static func bridgeJSLiftParameter(_ value: Int32) -> Self { return AnyMyViewControllerDelegate(jsObject: JSObject(id: UInt32(bitPattern: value))) } } +extension Direction: _BridgedSwiftCaseEnum { + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + return bridgeJSRawValue + } + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Direction { + return bridgeJSLiftParameter(value) + } + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Direction { + return Direction(bridgeJSRawValue: value)! + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { + return bridgeJSLowerParameter() + } + + private init?(bridgeJSRawValue: Int32) { + switch bridgeJSRawValue { + case 0: + self = .north + case 1: + self = .south + case 2: + self = .east + case 3: + self = .west + default: + return nil + } + } + + private var bridgeJSRawValue: Int32 { + switch self { + case .north: + return 0 + case .south: + return 1 + case .east: + return 2 + case .west: + return 3 + } + } +} + +extension ExampleEnum: _BridgedSwiftEnumNoPayload { +} + +extension Result: _BridgedSwiftAssociatedValueEnum { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> Result { + switch caseId { + case 0: + return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) + case 1: + return .failure(Int.bridgeJSLiftParameter(_swift_js_pop_param_int32())) + default: + fatalError("Unknown Result case ID: \(caseId)") + } + } + + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0): + _swift_js_push_int(Int32(param0)) + return Int32(1) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> Result { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> Result { + return _bridgeJSLiftFromCaseId(caseId) + } + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { + switch self { + case .success(let param0): + _swift_js_push_tag(Int32(0)) + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + case .failure(let param0): + _swift_js_push_tag(Int32(1)) + _swift_js_push_int(Int32(param0)) + } + } +} + +extension Priority: _BridgedSwiftEnumNoPayload { +} + +@_expose(wasm, "bjs_Helper_init") +@_cdecl("bjs_Helper_init") +public func _bjs_Helper_init(value: Int32) -> UnsafeMutableRawPointer { + #if arch(wasm32) + let ret = Helper(value: Int.bridgeJSLiftParameter(value)) + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_Helper_increment") +@_cdecl("bjs_Helper_increment") +public func _bjs_Helper_increment(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + Helper.bridgeJSLiftParameter(_self).increment() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_Helper_value_get") +@_cdecl("bjs_Helper_value_get") +public func _bjs_Helper_value_get(_self: UnsafeMutableRawPointer) -> Int32 { + #if arch(wasm32) + let ret = Helper.bridgeJSLiftParameter(_self).value + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_Helper_value_set") +@_cdecl("bjs_Helper_value_set") +public func _bjs_Helper_value_set(_self: UnsafeMutableRawPointer, value: Int32) -> Void { + #if arch(wasm32) + Helper.bridgeJSLiftParameter(_self).value = Int.bridgeJSLiftParameter(value) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_Helper_deinit") +@_cdecl("bjs_Helper_deinit") +public func _bjs_Helper_deinit(pointer: UnsafeMutableRawPointer) { + Unmanaged.fromOpaque(pointer).release() +} + +extension Helper: ConvertibleToJSValue, _BridgedSwiftHeapObject { + var jsValue: JSValue { + #if arch(wasm32) + @_extern(wasm, module: "TestModule", name: "bjs_Helper_wrap") + func _bjs_Helper_wrap(_: UnsafeMutableRawPointer) -> Int32 + #else + func _bjs_Helper_wrap(_: UnsafeMutableRawPointer) -> Int32 { + fatalError("Only available on WebAssembly") + } + #endif + return .object(JSObject(id: UInt32(bitPattern: _bjs_Helper_wrap(Unmanaged.passRetained(self).toOpaque())))) + } +} + @_expose(wasm, "bjs_MyViewController_init") @_cdecl("bjs_MyViewController_init") public func _bjs_MyViewController_init(delegate: Int32) -> UnsafeMutableRawPointer { @@ -109,6 +474,16 @@ public func _bjs_MyViewController_checkEvenCount(_self: UnsafeMutableRawPointer) #endif } +@_expose(wasm, "bjs_MyViewController_sendHelper") +@_cdecl("bjs_MyViewController_sendHelper") +public func _bjs_MyViewController_sendHelper(_self: UnsafeMutableRawPointer, helper: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + MyViewController.bridgeJSLiftParameter(_self).sendHelper(_: Helper.bridgeJSLiftParameter(helper)) + #else + fatalError("Only available on WebAssembly") + #endif +} + @_expose(wasm, "bjs_MyViewController_delegate_get") @_cdecl("bjs_MyViewController_delegate_get") public func _bjs_MyViewController_delegate_get(_self: UnsafeMutableRawPointer) -> Int32 { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticFunctions.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticFunctions.swift index 4e301151..7aa91b91 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticFunctions.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticFunctions.swift @@ -11,13 +11,13 @@ extension Calculator: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Calculator { - return Calculator(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Calculator { return Calculator(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -53,7 +53,7 @@ public func _bjs_Calculator_static_square(value: Int32) -> Int32 { } extension APIResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> APIResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -64,6 +64,32 @@ extension APIResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0): + _swift_js_push_int(Int32(param0)) + return Int32(1) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticProperties.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticProperties.swift index 6edd0c8f..72d561a0 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticProperties.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StaticProperties.swift @@ -11,13 +11,13 @@ extension PropertyEnum: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> PropertyEnum { - return PropertyEnum(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> PropertyEnum { return PropertyEnum(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { diff --git a/Plugins/PackageToJS/Templates/instantiate.js b/Plugins/PackageToJS/Templates/instantiate.js index 236b7020..5d74cc7e 100644 --- a/Plugins/PackageToJS/Templates/instantiate.js +++ b/Plugins/PackageToJS/Templates/instantiate.js @@ -54,6 +54,13 @@ async function createInstantiator(options, swift) { swift_js_return_optional_float: unexpectedBjsCall, swift_js_return_optional_heap_object: unexpectedBjsCall, swift_js_return_optional_object: unexpectedBjsCall, + swift_js_get_optional_int_presence: unexpectedBjsCall, + swift_js_get_optional_int_value: unexpectedBjsCall, + swift_js_get_optional_string: unexpectedBjsCall, + swift_js_get_optional_float_presence: unexpectedBjsCall, + swift_js_get_optional_float_value: unexpectedBjsCall, + swift_js_get_optional_double_presence: unexpectedBjsCall, + swift_js_get_optional_double_value: unexpectedBjsCall, } }, /** @param {WebAssembly.Instance} instance */ diff --git a/Sources/JavaScriptKit/BridgeJSInstrincics.swift b/Sources/JavaScriptKit/BridgeJSInstrincics.swift index 4742aa62..9c97583c 100644 --- a/Sources/JavaScriptKit/BridgeJSInstrincics.swift +++ b/Sources/JavaScriptKit/BridgeJSInstrincics.swift @@ -77,6 +77,10 @@ import _CJavaScriptKit // - `func bridgeJSLiftParameter(_ ...) -> <#TargetType#>`: lift the given Wasm core type parameters to a higher-level type // - `func bridgeJSLowerReturn() -> <#WasmCoreType#>`: lower the given higher-level return value to a Wasm core type // +// Optional types (ExportSwift only) additionally define: +// - `func bridgeJSLowerParameterWithPresence()`: lower optional as (isSome, value) tuple for protocol setters/parameters +// - `func bridgeJSLiftReturnFromSideChannel()`: lift optional from side-channel storage for protocol property getters +// // See JSGlueGen.swift in BridgeJSLink for JS-side lowering/lifting implementation. /// A protocol that Swift types that can appear as parameters or return values on @@ -268,14 +272,14 @@ public protocol _BridgedSwiftHeapObject: AnyObject {} extension _BridgedSwiftHeapObject { // MARK: ImportTS - @available(*, unavailable, message: "Swift heap objects are not supported to be passed to imported JS functions") - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Void {} - @available( - *, - unavailable, - message: "Swift heap objects are not supported to be returned from imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ pointer: UnsafeMutableRawPointer) -> Void {} + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> UnsafeMutableRawPointer { + // For protocol parameters, we pass the unretained pointer since JS already has a reference + return Unmanaged.passUnretained(self).toOpaque() + } + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ pointer: UnsafeMutableRawPointer) -> Self { + // For protocol returns, take an unretained value since JS manages the lifetime + return Unmanaged.fromOpaque(pointer).takeUnretainedValue() + } // MARK: ExportSwift @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ pointer: UnsafeMutableRawPointer) -> Self { @@ -325,6 +329,11 @@ public protocol _BridgedSwiftEnumNoPayload {} /// /// The conformance is automatically synthesized by the BridgeJS code generator. public protocol _BridgedSwiftCaseEnum { + // MARK: ImportTS + @_spi(BridgeJS) consuming func bridgeJSLowerParameter() -> Int32 + @_spi(BridgeJS) static func bridgeJSLiftReturn(_ value: Int32) -> Self + + // MARK: ExportSwift @_spi(BridgeJS) static func bridgeJSLiftParameter(_ value: Int32) -> Self @_spi(BridgeJS) consuming func bridgeJSLowerReturn() -> Int32 } @@ -332,7 +341,12 @@ public protocol _BridgedSwiftCaseEnum { /// A protocol that Swift associated value enum types conform to. /// /// The conformance is automatically synthesized by the BridgeJS code generator. -public protocol _BridgedSwiftAssociatedValueEnum { +public protocol _BridgedSwiftAssociatedValueEnum: _BridgedSwiftTypeLoweredIntoVoidType { + // MARK: ImportTS + @_spi(BridgeJS) consuming func bridgeJSLowerParameter() -> Int32 + @_spi(BridgeJS) static func bridgeJSLiftReturn(_ caseId: Int32) -> Self + + // MARK: ExportSwift @_spi(BridgeJS) static func bridgeJSLiftParameter(_ caseId: Int32) -> Self @_spi(BridgeJS) consuming func bridgeJSLowerReturn() -> Void } @@ -340,8 +354,9 @@ public protocol _BridgedSwiftAssociatedValueEnum { extension _BridgedSwiftEnumNoPayload where Self: RawRepresentable, RawValue == String { // MARK: ImportTS @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Int32 { rawValue.bridgeJSLowerParameter() } - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ bytesCount: Int32) -> Self { - Self(rawValue: .bridgeJSLiftReturn(bytesCount))! + + @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ id: Int32) -> Self { + Self(rawValue: .bridgeJSLiftReturn(id))! } // MARK: ExportSwift @@ -453,21 +468,25 @@ where Self: RawRepresentable, RawValue: _BridgedSwiftTypeLoweredIntoSingleWasmCo } #endif -// MARK: Optional Type Support - extension Optional where Wrapped == Bool { // MARK: ImportTS @available(*, unavailable, message: "Optional Bool type is not supported to be passed to imported JS functions") @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} - @available(*, unavailable, message: "Optional Bool type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Int32) -> Bool? { - return nil - } - // MARK: ExportSwift + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, value: Int32 + ) { + switch consume self { + case .none: + return (isSome: 0, value: 0) + case .some(let wrapped): + return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + } + } + @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Int32) -> Bool? { if isSome == 0 { return nil @@ -476,6 +495,19 @@ extension Optional where Wrapped == Bool { } } + @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ value: Int32) -> Bool? { + switch value { + case -1: + return nil + case 0: + return false + case 1: + return true + default: + return nil // Treat invalid values as null + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { #if arch(wasm32) @_extern(wasm, module: "bjs", name: "swift_js_return_optional_bool") @@ -496,18 +528,25 @@ extension Optional where Wrapped == Bool { } } -/// Optional support for Int extension Optional where Wrapped == Int { // MARK: ImportTS @available(*, unavailable, message: "Optional Int type is not supported to be passed to imported JS functions") @_spi(BridgeJS) public func bridgeJSLowerParameter() -> Void {} - @available(*, unavailable, message: "Optional Int type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Int32) -> Int? { return nil } - // MARK: ExportSwift + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, value: Int32 + ) { + switch consume self { + case .none: + return (isSome: 0, value: 0) + case .some(let wrapped): + return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + } + } + @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Int32) -> Int? { if isSome == 0 { return nil @@ -516,6 +555,29 @@ extension Optional where Wrapped == Int { } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> Int? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_int_presence") + func _swift_js_get_optional_int_presence() -> Int32 + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_int_value") + func _swift_js_get_optional_int_value() -> Int32 + #else + func _swift_js_get_optional_int_presence() -> Int32 { + _onlyAvailableOnWasm() + } + func _swift_js_get_optional_int_value() -> Int32 { + _onlyAvailableOnWasm() + } + #endif + + let isSome = _swift_js_get_optional_int_presence() + if isSome == 0 { + return nil + } else { + return Int.bridgeJSLiftReturn(_swift_js_get_optional_int_value()) + } + } + @_spi(BridgeJS) public func bridgeJSLowerReturn() -> Void { #if arch(wasm32) @_extern(wasm, module: "bjs", name: "swift_js_return_optional_int") @@ -536,16 +598,19 @@ extension Optional where Wrapped == Int { } } extension Optional where Wrapped == String { - // MARK: ImportTS - - @available(*, unavailable, message: "Optional String type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public func bridgeJSLowerParameter() -> Void {} - - @available(*, unavailable, message: "Optional String type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32) -> String? { return nil } - // MARK: ExportSwift + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, value: Int32 + ) { + switch consume self { + case .none: + return (isSome: 0, value: 0) + case .some(let wrapped): + return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + } + } + @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ bytes: Int32, _ count: Int32) -> String? { if isSome == 0 { @@ -555,6 +620,24 @@ extension Optional where Wrapped == String { } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> String? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_string") + func _swift_js_get_optional_string() -> Int32 + #else + func _swift_js_get_optional_string() -> Int32 { + _onlyAvailableOnWasm() + } + #endif + + let length = _swift_js_get_optional_string() + if length < 0 { + return nil + } else { + return String.bridgeJSLiftReturn(length) + } + } + @_spi(BridgeJS) public func bridgeJSLowerReturn() -> Void { #if arch(wasm32) @_extern(wasm, module: "bjs", name: "swift_js_return_optional_string") @@ -577,18 +660,19 @@ extension Optional where Wrapped == String { } } extension Optional where Wrapped == JSObject { - // MARK: ImportTS - - @available(*, unavailable, message: "Optional JSObject type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public func bridgeJSLowerParameter() -> Void {} + // MARK: ExportSwift - @available(*, unavailable, message: "Optional JSObject type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ objectId: Int32) -> JSObject? { - return nil + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, value: Int32 + ) { + switch consume self { + case .none: + return (isSome: 0, value: 0) + case .some(let wrapped): + return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + } } - // MARK: ExportSwift - @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ objectId: Int32) -> JSObject? { if isSome == 0 { return nil @@ -619,24 +703,6 @@ extension Optional where Wrapped == JSObject { } extension Optional where Wrapped: _BridgedSwiftProtocolWrapper { - // MARK: ImportTS - - @available( - *, - unavailable, - message: "Optional protocol types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public func bridgeJSLowerParameter() -> Void {} - - @available( - *, - unavailable, - message: "Optional protocol types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ objectId: Int32) -> Wrapped? { - return nil - } - // MARK: ExportSwift @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ objectId: Int32) -> Wrapped? { @@ -671,19 +737,35 @@ extension Optional where Wrapped: _BridgedSwiftProtocolWrapper { /// Optional support for Swift heap objects extension Optional where Wrapped: _BridgedSwiftHeapObject { // MARK: ImportTS - @available( - *, - unavailable, - message: "Optional Swift heap objects are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Void {} + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> UnsafeMutableRawPointer { + switch consume self { + case .none: + // Return null pointer for nil + return UnsafeMutableRawPointer(bitPattern: 0).unsafelyUnwrapped + case .some(let value): + // Pass unretained pointer since JS will manage the lifetime + return Unmanaged.passUnretained(value).toOpaque() + } + } + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, pointer: UnsafeMutableRawPointer + ) { + switch consume self { + case .none: + return (isSome: 0, pointer: UnsafeMutableRawPointer(bitPattern: 0)!) + case .some(let value): + return (isSome: 1, pointer: value.bridgeJSLowerParameter()) + } + } - @available( - *, - unavailable, - message: "Optional Swift heap objects are not supported to be returned from imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ pointer: UnsafeMutableRawPointer) -> Void { + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ pointer: UnsafeMutableRawPointer) -> Wrapped? + { + if pointer == UnsafeMutableRawPointer(bitPattern: 0) { + return nil + } else { + return Wrapped.bridgeJSLiftReturn(pointer) + } } // MARK: ExportSwift @@ -725,11 +807,6 @@ extension Optional where Wrapped == Float { @available(*, unavailable, message: "Optional Float type is not supported to be passed to imported JS functions") @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} - @available(*, unavailable, message: "Optional Float type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Float32) -> Float? { - return nil - } - // MARK: ExportSwift @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Float32) -> Float? { @@ -740,6 +817,29 @@ extension Optional where Wrapped == Float { } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> Float? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_float_presence") + func _swift_js_get_optional_float_presence() -> Int32 + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_float_value") + func _swift_js_get_optional_float_value() -> Float32 + #else + func _swift_js_get_optional_float_presence() -> Int32 { + _onlyAvailableOnWasm() + } + func _swift_js_get_optional_float_value() -> Float32 { + _onlyAvailableOnWasm() + } + #endif + + let isSome = _swift_js_get_optional_float_presence() + if isSome == 0 { + return nil + } else { + return Float.bridgeJSLiftReturn(_swift_js_get_optional_float_value()) + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { #if arch(wasm32) @_extern(wasm, module: "bjs", name: "swift_js_return_optional_float") @@ -767,11 +867,6 @@ extension Optional where Wrapped == Double { @available(*, unavailable, message: "Optional Double type is not supported to be passed to imported JS functions") @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} - @available(*, unavailable, message: "Optional Double type is not supported to be passed to imported JS functions") - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Float64) -> Double? { - return nil - } - // MARK: ExportSwift @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Float64) -> Double? { @@ -782,6 +877,29 @@ extension Optional where Wrapped == Double { } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> Double? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_double_presence") + func _swift_js_get_optional_double_presence() -> Int32 + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_double_value") + func _swift_js_get_optional_double_value() -> Float64 + #else + func _swift_js_get_optional_double_presence() -> Int32 { + _onlyAvailableOnWasm() + } + func _swift_js_get_optional_double_value() -> Float64 { + _onlyAvailableOnWasm() + } + #endif + + let isSome = _swift_js_get_optional_double_presence() + if isSome == 0 { + return nil + } else { + return Double.bridgeJSLiftReturn(_swift_js_get_optional_double_value()) + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { #if arch(wasm32) @_extern(wasm, module: "bjs", name: "swift_js_return_optional_double") @@ -804,26 +922,17 @@ extension Optional where Wrapped == Double { /// Optional support for case enums extension Optional where Wrapped: _BridgedSwiftCaseEnum { - // MARK: ImportTS - - @available( - *, - unavailable, - message: "Optional case enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} + // MARK: ExportSwift - @available( - *, - unavailable, - message: "Optional case enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Int32) -> Wrapped? { - return nil + @_spi(BridgeJS) public consuming func bridgeJSLowerParameterWithPresence() -> (isSome: Int32, value: Int32) { + switch consume self { + case .none: + return (isSome: 0, value: 0) + case .some(let value): + return (isSome: 1, value: value.bridgeJSLowerParameter()) + } } - // MARK: ExportSwift - @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Int32) -> Wrapped? { if isSome == 0 { return nil @@ -832,6 +941,14 @@ extension Optional where Wrapped: _BridgedSwiftCaseEnum { } } + @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ value: Int32) -> Wrapped? { + if value == -1 { + return nil + } else { + return Wrapped.bridgeJSLiftReturn(value) + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { #if arch(wasm32) @_extern(wasm, module: "bjs", name: "swift_js_return_optional_int") @@ -871,24 +988,19 @@ extension Optional where Wrapped: _BridgedSwiftTypeLoweredIntoVoidType { // MARK: Optional Raw Value Enum Support extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepresentable, Wrapped.RawValue == String { - // MARK: ImportTS - - @available( - *, - unavailable, - message: "Optional String raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} - - @available( - *, - unavailable, - message: "Optional String raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32) -> Wrapped? { return nil } - // MARK: ExportSwift + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, value: Int32 + ) { + switch consume self { + case .none: + return (isSome: 0, value: 0) + case .some(let wrapped): + return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + } + } + @_spi(BridgeJS) public static func bridgeJSLiftParameter( _ isSome: Int32, _ bytes: Int32, @@ -898,6 +1010,25 @@ extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepres return optionalRawValue.flatMap { Wrapped(rawValue: $0) } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> Wrapped? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_string") + func _swift_js_get_optional_string() -> Int32 + #else + func _swift_js_get_optional_string() -> Int32 { + _onlyAvailableOnWasm() + } + #endif + + let length = _swift_js_get_optional_string() + if length < 0 { + return nil + } else { + let rawValue = String.bridgeJSLiftReturn(length) + return Wrapped(rawValue: rawValue) + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { let optionalRawValue: String? = self?.rawValue optionalRawValue.bridgeJSLowerReturn() @@ -905,31 +1036,45 @@ extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepres } extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepresentable, Wrapped.RawValue == Int { - // MARK: ImportTS - - @available( - *, - unavailable, - message: "Optional Int raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} - - @available( - *, - unavailable, - message: "Optional Int raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Int32) -> Wrapped? { - return nil - } - // MARK: ExportSwift + @_spi(BridgeJS) public consuming func bridgeJSLowerParameterWithPresence() -> (isSome: Int32, value: Int32) { + switch consume self { + case .none: + return (isSome: 0, value: 0) + case .some(let value): + return (isSome: 1, value: value.bridgeJSLowerParameter()) + } + } @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Int32) -> Wrapped? { let optionalRawValue = Int?.bridgeJSLiftParameter(isSome, wrappedValue) return optionalRawValue.flatMap { Wrapped(rawValue: $0) } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> Wrapped? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_int_presence") + func _swift_js_get_optional_int_presence() -> Int32 + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_int_value") + func _swift_js_get_optional_int_value() -> Int32 + #else + func _swift_js_get_optional_int_presence() -> Int32 { + _onlyAvailableOnWasm() + } + func _swift_js_get_optional_int_value() -> Int32 { + _onlyAvailableOnWasm() + } + #endif + + let isSome = _swift_js_get_optional_int_presence() + if isSome == 0 { + return nil + } else { + let rawValue = _swift_js_get_optional_int_value() + return Wrapped(rawValue: Int(rawValue)) + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { let optionalRawValue: Int? = self?.rawValue optionalRawValue.bridgeJSLowerReturn() @@ -937,24 +1082,6 @@ extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepres } extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepresentable, Wrapped.RawValue == Bool { - // MARK: ImportTS - - @available( - *, - unavailable, - message: "Optional Bool raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} - - @available( - *, - unavailable, - message: "Optional Bool raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Int32) -> Wrapped? { - return nil - } - // MARK: ExportSwift @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Int32) -> Wrapped? { @@ -969,30 +1096,46 @@ extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepres } extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepresentable, Wrapped.RawValue == Float { - // MARK: ImportTS - - @available( - *, - unavailable, - message: "Optional Float raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} + // MARK: ExportSwift - @available( - *, - unavailable, - message: "Optional Float raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Float32) -> Wrapped? { - return nil + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, value: Float32 + ) { + switch consume self { + case .none: + return (isSome: 0, value: 0.0) + case .some(let wrapped): + return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + } } - // MARK: ExportSwift - @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Float32) -> Wrapped? { let optionalRawValue = Float?.bridgeJSLiftParameter(isSome, wrappedValue) return optionalRawValue.flatMap { Wrapped(rawValue: $0) } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> Wrapped? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_float_presence") + func _swift_js_get_optional_float_presence() -> Int32 + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_float_value") + func _swift_js_get_optional_float_value() -> Float32 + #else + func _swift_js_get_optional_float_presence() -> Int32 { + _onlyAvailableOnWasm() + } + func _swift_js_get_optional_float_value() -> Float32 { + _onlyAvailableOnWasm() + } + #endif + + let isSome = _swift_js_get_optional_float_presence() + if isSome == 0 { + return nil + } else { + let rawValue = _swift_js_get_optional_float_value() + return Wrapped(rawValue: Float(rawValue)) + } + } @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { let optionalRawValue: Float? = self?.rawValue @@ -1001,31 +1144,48 @@ extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepres } extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepresentable, Wrapped.RawValue == Double { - // MARK: ImportTS - - @available( - *, - unavailable, - message: "Optional Double raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void {} + // MARK: ExportSwift - @available( - *, - unavailable, - message: "Optional Double raw value enum types are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ wrappedValue: Float64) -> Wrapped? { - return nil + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameterWithPresence() -> ( + isSome: Int32, value: Float64 + ) { + switch consume self { + case .none: + return (isSome: 0, value: 0.0) + case .some(let wrapped): + return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + } } - // MARK: ExportSwift - @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ wrappedValue: Float64) -> Wrapped? { let optionalRawValue = Double?.bridgeJSLiftParameter(isSome, wrappedValue) return optionalRawValue.flatMap { Wrapped(rawValue: $0) } } + @_spi(BridgeJS) public static func bridgeJSLiftReturnFromSideChannel() -> Wrapped? { + #if arch(wasm32) + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_double_presence") + func _swift_js_get_optional_double_presence() -> Int32 + @_extern(wasm, module: "bjs", name: "swift_js_get_optional_double_value") + func _swift_js_get_optional_double_value() -> Float64 + #else + func _swift_js_get_optional_double_presence() -> Int32 { + _onlyAvailableOnWasm() + } + func _swift_js_get_optional_double_value() -> Float64 { + _onlyAvailableOnWasm() + } + #endif + + let isSome = _swift_js_get_optional_double_presence() + if isSome == 0 { + return nil + } else { + let rawValue = _swift_js_get_optional_double_value() + return Wrapped(rawValue: Double(rawValue)) + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { let optionalRawValue: Double? = self?.rawValue optionalRawValue.bridgeJSLowerReturn() @@ -1035,25 +1195,17 @@ extension Optional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepres // MARK: Optional Associated Value Enum Support extension Optional where Wrapped: _BridgedSwiftAssociatedValueEnum { - // MARK: ImportTS - @available( - *, - unavailable, - message: "Optional associated value enums are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Void { - fatalError("Optional associated value enum bridgeJSLowerParameter is not supported for ImportTS") - } - - @available( - *, - unavailable, - message: "Optional associated value enums are not supported to be passed to imported JS functions" - ) - @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ isSome: Int32, _ caseId: Int32) -> Wrapped? { return nil } - // MARK: ExportSwift + @_spi(BridgeJS) public consuming func bridgeJSLowerParameterWithPresence() -> (isSome: Int32, caseId: Int32) { + switch consume self { + case .none: + return (isSome: 0, caseId: 0) + case .some(let value): + return (isSome: 1, caseId: value.bridgeJSLowerParameter()) + } + } + @_spi(BridgeJS) public static func bridgeJSLiftParameter(_ isSome: Int32, _ caseId: Int32) -> Wrapped? { if isSome == 0 { return nil @@ -1062,6 +1214,14 @@ extension Optional where Wrapped: _BridgedSwiftAssociatedValueEnum { } } + @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ caseId: Int32) -> Wrapped? { + if caseId == -1 { + return nil + } else { + return Wrapped.bridgeJSLiftReturn(caseId) + } + } + @_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Void { #if arch(wasm32) @_extern(wasm, module: "bjs", name: "swift_js_push_tag") diff --git a/Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Protocols.md b/Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Protocols.md index 802ff3ae..4dba78c3 100644 --- a/Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Protocols.md +++ b/Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Protocols.md @@ -21,6 +21,9 @@ Mark a Swift protocol with `@JS` to expose it: import JavaScriptKit @JS protocol Counter { + var count: Int { get set } + var name: String { get } + var label: String? { get set } func increment(by amount: Int) func reset() func getValue() -> Int @@ -41,6 +44,18 @@ import JavaScriptKit @JS func getCurrentValue() -> Int { return delegate.getValue() } + + @JS func getCounterName() -> String { + return delegate.name + } + + @JS func setCountValue(_ value: Int) { + delegate.count = value + } + + @JS func updateLabel(_ newLabel: String?) { + delegate.label = newLabel + } } ``` @@ -53,6 +68,8 @@ const { exports } = await init({}); // Implement the Counter protocol const counterImpl = { count: 0, + name: "JSCounter", + label: "Default Label", increment(amount) { this.count += amount; }, @@ -68,12 +85,23 @@ const counterImpl = { const manager = new exports.CounterManager(counterImpl); manager.incrementTwice(); console.log(manager.getCurrentValue()); // 2 +console.log(manager.getCounterName()); // "JSCounter" +manager.setCountValue(10); +console.log(counterImpl.count); // 10 + +manager.updateLabel("Custom Label"); +console.log(counterImpl.label); // "Custom Label" +manager.updateLabel(null); +console.log(counterImpl.label); // null ``` The generated TypeScript interface: ```typescript export interface Counter { + count: number; + readonly name: string; + label: string | null; increment(amount: number): void; reset(): void; getValue(): number; @@ -94,6 +122,27 @@ BridgeJS generates a Swift wrapper struct for each `@JS` protocol. This wrapper struct AnyCounter: Counter, _BridgedSwiftProtocolWrapper { let jsObject: JSObject + var count: Int { + get { + @_extern(wasm, module: "TestModule", name: "bjs_Counter_count_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Int.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "TestModule", name: "bjs_Counter_count_set") + func _extern_set(this: Int32, value: Int32) + _extern_set(this: Int32(bitPattern: jsObject.id), value: newValue.bridgeJSLowerParameter()) + } + } + + var name: String { + @_extern(wasm, module: "TestModule", name: "bjs_Counter_name_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return String.bridgeJSLiftReturn() + } + func increment(by amount: Int) { @_extern(wasm, module: "TestModule", name: "bjs_Counter_increment") func _extern_increment(this: Int32, amount: Int32) @@ -128,13 +177,16 @@ You can also implement protocols in Swift and use them from JavaScript: ```swift @JS protocol Counter { + var count: Int { get set } + var name: String { get } func increment(by amount: Int) func reset() func getValue() -> Int } final class SwiftCounter: Counter { - private var count = 0 + var count = 0 + let name = "SwiftCounter" func increment(by amount: Int) { count += amount @@ -158,9 +210,13 @@ From JavaScript: ```javascript const counter = exports.createCounter(); +console.log(counter.name); // "SwiftCounter" counter.increment(5); counter.increment(3); console.log(counter.getValue()); // 8 +console.log(counter.count); // 8 +counter.count = 100; +console.log(counter.getValue()); // 100 counter.reset(); console.log(counter.getValue()); // 0 ``` @@ -178,32 +234,14 @@ When you pass a JavaScript object implementing a protocol to Swift: | Swift Feature | Status | |:--------------|:-------| -| Method requirements: `func method()` | ✅ | -| Method requirements with parameters | ✅ | -| Method requirements with return values | ✅ | -| Throwing method requirements: `func method() throws(JSException)` | ✅ | -| Async method requirements: `func method() async` | ✅ | +| Method requirements: `func foo(_ param: String?) -> FooClass?` | ✅ | +| Property requirements: `var property: Type { get }` / `var property: Type { get set }` | ✅ | +| Optional parameters / return values in methods | ✅ | +| Optional properties | ✅ | | Optional protocol methods | ❌ | -| Property requirements: `var property: Type { get }` | ❌ | -| Property requirements: `var property: Type { get set }` | ❌ | | Associated types | ❌ | | Protocol inheritance | ❌ | | Protocol composition: `Protocol1 & Protocol2` | ❌ | | Generics | ❌ | -### Type Support for Protocol Method Parameters and Return Types - -Protocol method parameters and return values have more limited type support compared to regular exported Swift functions and classes. - -**Supported Types:** -- Primitives: `Bool`, `Int`, `Float`, `Double` -- `String` -- `JSObject` - -**Not Supported:** -- `@JS class` types -- `@JS enum` types (case, raw value, or associated value) -- `@JS protocol` types -- Optional types: `Int?`, `String?`, etc. - -> Note: For regular `@JS func` and `@JS class` exports (not within protocols), all these types including optionals, enums, and classes are fully supported. See and for more information. +> Note: Protocol type support matches that of regular `@JS func` and `@JS class` exports. See , , and for more information. diff --git a/Tests/BridgeJSRuntimeTests/ExportAPITests.swift b/Tests/BridgeJSRuntimeTests/ExportAPITests.swift index b6255a4e..97ebcfd2 100644 --- a/Tests/BridgeJSRuntimeTests/ExportAPITests.swift +++ b/Tests/BridgeJSRuntimeTests/ExportAPITests.swift @@ -859,70 +859,143 @@ enum APIOptionalResult { // MARK: - Protocol Tests -@JS protocol Counter { +@JS protocol DataProcessor { + var count: Int { get set } + var name: String { get } + var optionalTag: String? { get set } + var optionalCount: Int? { get set } + var direction: Direction? { get set } + var optionalTheme: Theme? { get set } + var httpStatus: HttpStatus? { get set } + var apiResult: APIResult? { get set } + var helper: Greeter { get set } + var optionalHelper: Greeter? { get set } func increment(by amount: Int) func getValue() -> Int func setLabelElements(_ labelPrefix: String, _ labelSuffix: String) func getLabel() -> String func isEven() -> Bool + func processGreeter(_ greeter: Greeter) -> String + func createGreeter() -> Greeter + func processOptionalGreeter(_ greeter: Greeter?) -> String + func createOptionalGreeter() -> Greeter? + func handleAPIResult(_ result: APIResult?) + func getAPIResult() -> APIResult? } -@JS class CounterManager { +@JS class DataProcessorManager { - @JS var counter: Counter - @JS var backupCounter: Counter? + @JS var processor: DataProcessor + @JS var backupProcessor: DataProcessor? - @JS init(counter: Counter) { - self.counter = counter - self.backupCounter = nil + @JS init(processor: DataProcessor) { + self.processor = processor + self.backupProcessor = nil } @JS func incrementByAmount(_ amount: Int) { - counter.increment(by: amount) + processor.increment(by: amount) } - @JS func setCounterLabel(_ prefix: String, _ suffix: String) { - counter.setLabelElements(prefix, suffix) + @JS func setProcessorLabel(_ prefix: String, _ suffix: String) { + processor.setLabelElements(prefix, suffix) } - @JS func isCounterEven() -> Bool { - return counter.isEven() + @JS func isProcessorEven() -> Bool { + return processor.isEven() } - @JS func getCounterLabel() -> String { - return counter.getLabel() + @JS func getProcessorLabel() -> String { + return processor.getLabel() } @JS func getCurrentValue() -> Int { - return counter.getValue() + return processor.getValue() } @JS func incrementBoth() { - counter.increment(by: 1) - backupCounter?.increment(by: 1) + processor.increment(by: 1) + backupProcessor?.increment(by: 1) } @JS func getBackupValue() -> Int? { - return backupCounter?.getValue() + return backupProcessor?.getValue() } @JS func hasBackup() -> Bool { - return backupCounter != nil + return backupProcessor != nil + } + + @JS func getProcessorOptionalTag() -> String? { + return processor.optionalTag + } + + @JS func setProcessorOptionalTag(_ tag: String?) { + processor.optionalTag = tag + } + + @JS func getProcessorOptionalCount() -> Int? { + return processor.optionalCount + } + + @JS func setProcessorOptionalCount(_ count: Int?) { + processor.optionalCount = count + } + + @JS func getProcessorDirection() -> Direction? { + return processor.direction + } + + @JS func setProcessorDirection(_ direction: Direction?) { + processor.direction = direction + } + + @JS func getProcessorTheme() -> Theme? { + return processor.optionalTheme + } + + @JS func setProcessorTheme(_ theme: Theme?) { + processor.optionalTheme = theme + } + + @JS func getProcessorHttpStatus() -> HttpStatus? { + return processor.httpStatus + } + + @JS func setProcessorHttpStatus(_ status: HttpStatus?) { + processor.httpStatus = status + } + + @JS func getProcessorAPIResult() -> APIResult? { + return processor.getAPIResult() + } + + @JS func setProcessorAPIResult(_ apiResult: APIResult?) { + processor.handleAPIResult(apiResult) } } -@JS class SwiftCounter: Counter { - private var value: Int = 0 +@JS class SwiftDataProcessor: DataProcessor { + @JS var count: Int = 0 + @JS let name: String = "SwiftDataProcessor" + @JS var optionalTag: String? = nil + @JS var optionalCount: Int? = nil + @JS var direction: Direction? = nil + @JS var optionalTheme: Theme? = nil + @JS var httpStatus: HttpStatus? = nil + @JS var apiResult: APIResult? = nil + @JS var helper: Greeter = Greeter(name: "DefaultHelper") + @JS var optionalHelper: Greeter? = nil private var label: String = "" @JS init() {} @JS func increment(by amount: Int) { - value += amount + count += amount } @JS func getValue() -> Int { - return value + return count } @JS func setLabelElements(_ labelPrefix: String, _ labelSuffix: String) { @@ -934,7 +1007,35 @@ enum APIOptionalResult { } @JS func isEven() -> Bool { - return value % 2 == 0 + return count % 2 == 0 + } + + @JS func processGreeter(_ greeter: Greeter) -> String { + return "SwiftProcessor processed: \(greeter.greet())" + } + + @JS func createGreeter() -> Greeter { + return Greeter(name: "ProcessorGreeter") + } + + @JS func processOptionalGreeter(_ greeter: Greeter?) -> String { + if let greeter = greeter { + return "SwiftProcessor processed optional: \(greeter.greet())" + } else { + return "SwiftProcessor received nil" + } + } + + @JS func createOptionalGreeter() -> Greeter? { + return Greeter(name: "OptionalProcessorGreeter") + } + + @JS func handleAPIResult(_ result: APIResult?) { + self.apiResult = result + } + + @JS func getAPIResult() -> APIResult? { + return apiResult } } diff --git a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.ExportSwift.swift b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.ExportSwift.swift index 1e22c2b0..350ef7ae 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.ExportSwift.swift +++ b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.ExportSwift.swift @@ -6,44 +6,229 @@ @_spi(BridgeJS) import JavaScriptKit -struct AnyCounter: Counter, _BridgedSwiftProtocolWrapper { +struct AnyDataProcessor: DataProcessor, _BridgedSwiftProtocolWrapper { let jsObject: JSObject func increment(by amount: Int) { - @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Counter_increment") + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_increment") func _extern_increment(this: Int32, amount: Int32) _extern_increment(this: Int32(bitPattern: jsObject.id), amount: amount.bridgeJSLowerParameter()) } func getValue() -> Int { - @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Counter_getValue") + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_getValue") func _extern_getValue(this: Int32) -> Int32 let ret = _extern_getValue(this: Int32(bitPattern: jsObject.id)) return Int.bridgeJSLiftReturn(ret) } func setLabelElements(_ labelPrefix: String, _ labelSuffix: String) { - @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Counter_setLabelElements") + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_setLabelElements") func _extern_setLabelElements(this: Int32, labelPrefix: Int32, labelSuffix: Int32) _extern_setLabelElements(this: Int32(bitPattern: jsObject.id), labelPrefix: labelPrefix.bridgeJSLowerParameter(), labelSuffix: labelSuffix.bridgeJSLowerParameter()) } func getLabel() -> String { - @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Counter_getLabel") + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_getLabel") func _extern_getLabel(this: Int32) -> Int32 let ret = _extern_getLabel(this: Int32(bitPattern: jsObject.id)) return String.bridgeJSLiftReturn(ret) } func isEven() -> Bool { - @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Counter_isEven") + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_isEven") func _extern_isEven(this: Int32) -> Int32 let ret = _extern_isEven(this: Int32(bitPattern: jsObject.id)) return Bool.bridgeJSLiftReturn(ret) } + func processGreeter(_ greeter: Greeter) -> String { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_processGreeter") + func _extern_processGreeter(this: Int32, greeter: UnsafeMutableRawPointer) -> Int32 + let ret = _extern_processGreeter(this: Int32(bitPattern: jsObject.id), greeter: greeter.bridgeJSLowerParameter()) + return String.bridgeJSLiftReturn(ret) + } + + func createGreeter() -> Greeter { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_createGreeter") + func _extern_createGreeter(this: Int32) -> UnsafeMutableRawPointer + let ret = _extern_createGreeter(this: Int32(bitPattern: jsObject.id)) + return Greeter.bridgeJSLiftReturn(ret) + } + + func processOptionalGreeter(_ greeter: Optional) -> String { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_processOptionalGreeter") + func _extern_processOptionalGreeter(this: Int32, greeterIsSome: Int32, greeterPointer: UnsafeMutableRawPointer) -> Int32 + let (greeterIsSome, greeterPointer) = greeter.bridgeJSLowerParameterWithPresence() + let ret = _extern_processOptionalGreeter(this: Int32(bitPattern: jsObject.id), greeterIsSome: greeterIsSome, greeterPointer: greeterPointer) + return String.bridgeJSLiftReturn(ret) + } + + func createOptionalGreeter() -> Optional { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_createOptionalGreeter") + func _extern_createOptionalGreeter(this: Int32) -> UnsafeMutableRawPointer + let ret = _extern_createOptionalGreeter(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + + func handleAPIResult(_ result: Optional) { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_handleAPIResult") + func _extern_handleAPIResult(this: Int32, resultIsSome: Int32, resultCaseId: Int32) + let (resultIsSome, resultCaseId) = result.bridgeJSLowerParameterWithPresence() + _extern_handleAPIResult(this: Int32(bitPattern: jsObject.id), resultIsSome: resultIsSome, resultCaseId: resultCaseId) + } + + func getAPIResult() -> Optional { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_getAPIResult") + func _extern_getAPIResult(this: Int32) -> Int32 + let ret = _extern_getAPIResult(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + + var count: Int { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_count_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Int.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_count_set") + func _extern_set(this: Int32, value: Int32) + _extern_set(this: Int32(bitPattern: jsObject.id), value: newValue.bridgeJSLowerParameter()) + } + } + + var name: String { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_name_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return String.bridgeJSLiftReturn(ret) + } + } + + var optionalTag: Optional { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalTag_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturnFromSideChannel() + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalTag_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var optionalCount: Optional { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalCount_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturnFromSideChannel() + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalCount_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var direction: Optional { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_direction_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_direction_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var optionalTheme: Optional { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalTheme_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturnFromSideChannel() + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalTheme_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var httpStatus: Optional { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_httpStatus_get") + func _extern_get(this: Int32) + _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturnFromSideChannel() + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_httpStatus_set") + func _extern_set(this: Int32, isSome: Int32, value: Int32) + let (isSome, value) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, value: value) + } + } + + var apiResult: Optional { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_apiResult_get") + func _extern_get(this: Int32) -> Int32 + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_apiResult_set") + func _extern_set(this: Int32, isSome: Int32, caseId: Int32) + let (isSome, caseId) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, caseId: caseId) + } + } + + var helper: Greeter { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_helper_get") + func _extern_get(this: Int32) -> UnsafeMutableRawPointer + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Greeter.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_helper_set") + func _extern_set(this: Int32, pointer: UnsafeMutableRawPointer) + _extern_set(this: Int32(bitPattern: jsObject.id), pointer: newValue.bridgeJSLowerParameter()) + } + } + + var optionalHelper: Optional { + get { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalHelper_get") + func _extern_get(this: Int32) -> UnsafeMutableRawPointer + let ret = _extern_get(this: Int32(bitPattern: jsObject.id)) + return Optional.bridgeJSLiftReturn(ret) + } + set { + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalHelper_set") + func _extern_set(this: Int32, isSome: Int32, pointer: UnsafeMutableRawPointer) + let (isSome, pointer) = newValue.bridgeJSLowerParameterWithPresence() + _extern_set(this: Int32(bitPattern: jsObject.id), isSome: isSome, pointer: pointer) + } + } + static func bridgeJSLiftParameter(_ value: Int32) -> Self { - return AnyCounter(jsObject: JSObject(id: UInt32(bitPattern: value))) + return AnyDataProcessor(jsObject: JSObject(id: UInt32(bitPattern: value))) } } @@ -52,13 +237,13 @@ extension Direction: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Direction { - return Direction(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Direction { return Direction(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -95,13 +280,13 @@ extension Status: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Status { - return Status(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Status { return Status(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -140,13 +325,13 @@ extension TSDirection: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> TSDirection { - return TSDirection(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> TSDirection { return TSDirection(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -186,13 +371,13 @@ extension Networking.API.Method: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Networking.API.Method { - return Networking.API.Method(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Networking.API.Method { return Networking.API.Method(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -235,13 +420,13 @@ extension Internal.SupportedMethod: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> Internal.SupportedMethod { - return Internal.SupportedMethod(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> Internal.SupportedMethod { return Internal.SupportedMethod(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -266,7 +451,7 @@ extension Internal.SupportedMethod: _BridgedSwiftCaseEnum { } extension APIResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> APIResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -285,6 +470,43 @@ extension APIResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0): + _swift_js_push_int(Int32(param0)) + return Int32(1) + case .flag(let param0): + _swift_js_push_int(param0 ? 1 : 0) + return Int32(2) + case .rate(let param0): + _swift_js_push_f32(param0) + return Int32(3) + case .precise(let param0): + _swift_js_push_f64(param0) + return Int32(4) + case .info: + return Int32(5) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -312,7 +534,7 @@ extension APIResult: _BridgedSwiftAssociatedValueEnum { } extension ComplexResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> ComplexResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> ComplexResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -333,6 +555,79 @@ extension ComplexResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .error(let param0, let param1): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + _swift_js_push_int(Int32(param1)) + return Int32(1) + case .location(let param0, let param1, let param2): + _swift_js_push_f64(param0) + _swift_js_push_f64(param1) + var __bjs_param2 = param2 + __bjs_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(2) + case .status(let param0, let param1, let param2): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(Int32(param1)) + var __bjs_param2 = param2 + __bjs_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(3) + case .coordinates(let param0, let param1, let param2): + _swift_js_push_f64(param0) + _swift_js_push_f64(param1) + _swift_js_push_f64(param2) + return Int32(4) + case .comprehensive(let param0, let param1, let param2, let param3, let param4, let param5, let param6, let param7, let param8): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(param1 ? 1 : 0) + _swift_js_push_int(Int32(param2)) + _swift_js_push_int(Int32(param3)) + _swift_js_push_f64(param4) + _swift_js_push_f64(param5) + var __bjs_param6 = param6 + __bjs_param6.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + var __bjs_param7 = param7 + __bjs_param7.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + var __bjs_param8 = param8 + __bjs_param8.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(5) + case .info: + return Int32(6) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> ComplexResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> ComplexResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -396,7 +691,7 @@ extension ComplexResult: _BridgedSwiftAssociatedValueEnum { } extension Utilities.Result: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> Utilities.Result { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> Utilities.Result { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -409,6 +704,44 @@ extension Utilities.Result: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0, let param1): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + _swift_js_push_int(Int32(param1)) + return Int32(1) + case .status(let param0, let param1, let param2): + _swift_js_push_int(param0 ? 1 : 0) + _swift_js_push_int(Int32(param1)) + var __bjs_param2 = param2 + __bjs_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(2) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> Utilities.Result { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> Utilities.Result { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -437,7 +770,7 @@ extension Utilities.Result: _BridgedSwiftAssociatedValueEnum { } extension API.NetworkingResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> API.NetworkingResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> API.NetworkingResult { switch caseId { case 0: return .success(String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -448,6 +781,36 @@ extension API.NetworkingResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + return Int32(0) + case .failure(let param0, let param1): + var __bjs_param0 = param0 + __bjs_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + _swift_js_push_int(Int32(param1)) + return Int32(1) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> API.NetworkingResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> API.NetworkingResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -468,7 +831,7 @@ extension API.NetworkingResult: _BridgedSwiftAssociatedValueEnum { } extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { - @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIOptionalResult { + private static func _bridgeJSLiftFromCaseId(_ caseId: Int32) -> APIOptionalResult { switch caseId { case 0: return .success(Optional.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32(), _swift_js_pop_param_int32())) @@ -481,6 +844,65 @@ extension APIOptionalResult: _BridgedSwiftAssociatedValueEnum { } } + // MARK: Protocol Export + + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 { + switch self { + case .success(let param0): + let __bjs_isSome_param0 = param0 != nil + if let __bjs_unwrapped_param0 = param0 { + var __bjs_str_param0 = __bjs_unwrapped_param0 + __bjs_str_param0.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + } + _swift_js_push_int(__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 { + _swift_js_push_int(Int32(__bjs_unwrapped_param0)) + } + _swift_js_push_int(__bjs_isSome_param0 ? 1 : 0) + let __bjs_isSome_param1 = param1 != nil + if let __bjs_unwrapped_param1 = param1 { + _swift_js_push_int(__bjs_unwrapped_param1 ? 1 : 0) + } + _swift_js_push_int(__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 { + _swift_js_push_int(__bjs_unwrapped_param0 ? 1 : 0) + } + _swift_js_push_int(__bjs_isSome_param0 ? 1 : 0) + let __bjs_isSome_param1 = param1 != nil + if let __bjs_unwrapped_param1 = param1 { + _swift_js_push_int(Int32(__bjs_unwrapped_param1)) + } + _swift_js_push_int(__bjs_isSome_param1 ? 1 : 0) + let __bjs_isSome_param2 = param2 != nil + if let __bjs_unwrapped_param2 = param2 { + var __bjs_str_param2 = __bjs_unwrapped_param2 + __bjs_str_param2.withUTF8 { ptr in + _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) + } + } + _swift_js_push_int(__bjs_isSome_param2 ? 1 : 0) + return Int32(2) + } + } + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ caseId: Int32) -> APIOptionalResult { + return _bridgeJSLiftFromCaseId(caseId) + } + + // MARK: ExportSwift + + @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> APIOptionalResult { + return _bridgeJSLiftFromCaseId(caseId) + } + @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() { switch self { case .success(let param0): @@ -534,13 +956,13 @@ extension StaticCalculator: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> StaticCalculator { - return StaticCalculator(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> StaticCalculator { return StaticCalculator(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -591,13 +1013,13 @@ extension StaticPropertyEnum: _BridgedSwiftCaseEnum { return bridgeJSRawValue } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftReturn(_ value: Int32) -> StaticPropertyEnum { - return StaticPropertyEnum(bridgeJSRawValue: value)! + return bridgeJSLiftParameter(value) } @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ value: Int32) -> StaticPropertyEnum { return StaticPropertyEnum(bridgeJSRawValue: value)! } @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() -> Int32 { - return bridgeJSRawValue + return bridgeJSLowerParameter() } private init?(bridgeJSRawValue: Int32) { @@ -3285,129 +3707,255 @@ extension StaticPropertyHolder: ConvertibleToJSValue, _BridgedSwiftHeapObject { } } -@_expose(wasm, "bjs_CounterManager_init") -@_cdecl("bjs_CounterManager_init") -public func _bjs_CounterManager_init(counter: Int32) -> UnsafeMutableRawPointer { +@_expose(wasm, "bjs_DataProcessorManager_init") +@_cdecl("bjs_DataProcessorManager_init") +public func _bjs_DataProcessorManager_init(processor: Int32) -> UnsafeMutableRawPointer { + #if arch(wasm32) + let ret = DataProcessorManager(processor: AnyDataProcessor.bridgeJSLiftParameter(processor)) + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_incrementByAmount") +@_cdecl("bjs_DataProcessorManager_incrementByAmount") +public func _bjs_DataProcessorManager_incrementByAmount(_self: UnsafeMutableRawPointer, amount: Int32) -> Void { + #if arch(wasm32) + DataProcessorManager.bridgeJSLiftParameter(_self).incrementByAmount(_: Int.bridgeJSLiftParameter(amount)) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_setProcessorLabel") +@_cdecl("bjs_DataProcessorManager_setProcessorLabel") +public func _bjs_DataProcessorManager_setProcessorLabel(_self: UnsafeMutableRawPointer, prefixBytes: Int32, prefixLength: Int32, suffixBytes: Int32, suffixLength: Int32) -> Void { + #if arch(wasm32) + DataProcessorManager.bridgeJSLiftParameter(_self).setProcessorLabel(_: String.bridgeJSLiftParameter(prefixBytes, prefixLength), _: String.bridgeJSLiftParameter(suffixBytes, suffixLength)) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_isProcessorEven") +@_cdecl("bjs_DataProcessorManager_isProcessorEven") +public func _bjs_DataProcessorManager_isProcessorEven(_self: UnsafeMutableRawPointer) -> Int32 { + #if arch(wasm32) + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).isProcessorEven() + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_getProcessorLabel") +@_cdecl("bjs_DataProcessorManager_getProcessorLabel") +public func _bjs_DataProcessorManager_getProcessorLabel(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getProcessorLabel() + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_getCurrentValue") +@_cdecl("bjs_DataProcessorManager_getCurrentValue") +public func _bjs_DataProcessorManager_getCurrentValue(_self: UnsafeMutableRawPointer) -> Int32 { #if arch(wasm32) - let ret = CounterManager(counter: AnyCounter.bridgeJSLiftParameter(counter)) + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getCurrentValue() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_incrementByAmount") -@_cdecl("bjs_CounterManager_incrementByAmount") -public func _bjs_CounterManager_incrementByAmount(_self: UnsafeMutableRawPointer, amount: Int32) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_incrementBoth") +@_cdecl("bjs_DataProcessorManager_incrementBoth") +public func _bjs_DataProcessorManager_incrementBoth(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - CounterManager.bridgeJSLiftParameter(_self).incrementByAmount(_: Int.bridgeJSLiftParameter(amount)) + DataProcessorManager.bridgeJSLiftParameter(_self).incrementBoth() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_setCounterLabel") -@_cdecl("bjs_CounterManager_setCounterLabel") -public func _bjs_CounterManager_setCounterLabel(_self: UnsafeMutableRawPointer, prefixBytes: Int32, prefixLength: Int32, suffixBytes: Int32, suffixLength: Int32) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_getBackupValue") +@_cdecl("bjs_DataProcessorManager_getBackupValue") +public func _bjs_DataProcessorManager_getBackupValue(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - CounterManager.bridgeJSLiftParameter(_self).setCounterLabel(_: String.bridgeJSLiftParameter(prefixBytes, prefixLength), _: String.bridgeJSLiftParameter(suffixBytes, suffixLength)) + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getBackupValue() + return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_isCounterEven") -@_cdecl("bjs_CounterManager_isCounterEven") -public func _bjs_CounterManager_isCounterEven(_self: UnsafeMutableRawPointer) -> Int32 { +@_expose(wasm, "bjs_DataProcessorManager_hasBackup") +@_cdecl("bjs_DataProcessorManager_hasBackup") +public func _bjs_DataProcessorManager_hasBackup(_self: UnsafeMutableRawPointer) -> Int32 { #if arch(wasm32) - let ret = CounterManager.bridgeJSLiftParameter(_self).isCounterEven() + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).hasBackup() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_getCounterLabel") -@_cdecl("bjs_CounterManager_getCounterLabel") -public func _bjs_CounterManager_getCounterLabel(_self: UnsafeMutableRawPointer) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_getProcessorOptionalTag") +@_cdecl("bjs_DataProcessorManager_getProcessorOptionalTag") +public func _bjs_DataProcessorManager_getProcessorOptionalTag(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = CounterManager.bridgeJSLiftParameter(_self).getCounterLabel() + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getProcessorOptionalTag() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_getCurrentValue") -@_cdecl("bjs_CounterManager_getCurrentValue") -public func _bjs_CounterManager_getCurrentValue(_self: UnsafeMutableRawPointer) -> Int32 { +@_expose(wasm, "bjs_DataProcessorManager_setProcessorOptionalTag") +@_cdecl("bjs_DataProcessorManager_setProcessorOptionalTag") +public func _bjs_DataProcessorManager_setProcessorOptionalTag(_self: UnsafeMutableRawPointer, tagIsSome: Int32, tagBytes: Int32, tagLength: Int32) -> Void { + #if arch(wasm32) + DataProcessorManager.bridgeJSLiftParameter(_self).setProcessorOptionalTag(_: Optional.bridgeJSLiftParameter(tagIsSome, tagBytes, tagLength)) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_getProcessorOptionalCount") +@_cdecl("bjs_DataProcessorManager_getProcessorOptionalCount") +public func _bjs_DataProcessorManager_getProcessorOptionalCount(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = CounterManager.bridgeJSLiftParameter(_self).getCurrentValue() + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getProcessorOptionalCount() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_incrementBoth") -@_cdecl("bjs_CounterManager_incrementBoth") -public func _bjs_CounterManager_incrementBoth(_self: UnsafeMutableRawPointer) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_setProcessorOptionalCount") +@_cdecl("bjs_DataProcessorManager_setProcessorOptionalCount") +public func _bjs_DataProcessorManager_setProcessorOptionalCount(_self: UnsafeMutableRawPointer, countIsSome: Int32, countValue: Int32) -> Void { #if arch(wasm32) - CounterManager.bridgeJSLiftParameter(_self).incrementBoth() + DataProcessorManager.bridgeJSLiftParameter(_self).setProcessorOptionalCount(_: Optional.bridgeJSLiftParameter(countIsSome, countValue)) #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_getBackupValue") -@_cdecl("bjs_CounterManager_getBackupValue") -public func _bjs_CounterManager_getBackupValue(_self: UnsafeMutableRawPointer) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_getProcessorDirection") +@_cdecl("bjs_DataProcessorManager_getProcessorDirection") +public func _bjs_DataProcessorManager_getProcessorDirection(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = CounterManager.bridgeJSLiftParameter(_self).getBackupValue() + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getProcessorDirection() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_hasBackup") -@_cdecl("bjs_CounterManager_hasBackup") -public func _bjs_CounterManager_hasBackup(_self: UnsafeMutableRawPointer) -> Int32 { +@_expose(wasm, "bjs_DataProcessorManager_setProcessorDirection") +@_cdecl("bjs_DataProcessorManager_setProcessorDirection") +public func _bjs_DataProcessorManager_setProcessorDirection(_self: UnsafeMutableRawPointer, directionIsSome: Int32, directionValue: Int32) -> Void { + #if arch(wasm32) + DataProcessorManager.bridgeJSLiftParameter(_self).setProcessorDirection(_: Optional.bridgeJSLiftParameter(directionIsSome, directionValue)) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_getProcessorTheme") +@_cdecl("bjs_DataProcessorManager_getProcessorTheme") +public func _bjs_DataProcessorManager_getProcessorTheme(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = CounterManager.bridgeJSLiftParameter(_self).hasBackup() + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getProcessorTheme() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_counter_get") -@_cdecl("bjs_CounterManager_counter_get") -public func _bjs_CounterManager_counter_get(_self: UnsafeMutableRawPointer) -> Int32 { +@_expose(wasm, "bjs_DataProcessorManager_setProcessorTheme") +@_cdecl("bjs_DataProcessorManager_setProcessorTheme") +public func _bjs_DataProcessorManager_setProcessorTheme(_self: UnsafeMutableRawPointer, themeIsSome: Int32, themeBytes: Int32, themeLength: Int32) -> Void { #if arch(wasm32) - let ret = CounterManager.bridgeJSLiftParameter(_self).counter as! AnyCounter + DataProcessorManager.bridgeJSLiftParameter(_self).setProcessorTheme(_: Optional.bridgeJSLiftParameter(themeIsSome, themeBytes, themeLength)) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_getProcessorHttpStatus") +@_cdecl("bjs_DataProcessorManager_getProcessorHttpStatus") +public func _bjs_DataProcessorManager_getProcessorHttpStatus(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getProcessorHttpStatus() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_counter_set") -@_cdecl("bjs_CounterManager_counter_set") -public func _bjs_CounterManager_counter_set(_self: UnsafeMutableRawPointer, value: Int32) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_setProcessorHttpStatus") +@_cdecl("bjs_DataProcessorManager_setProcessorHttpStatus") +public func _bjs_DataProcessorManager_setProcessorHttpStatus(_self: UnsafeMutableRawPointer, statusIsSome: Int32, statusValue: Int32) -> Void { #if arch(wasm32) - CounterManager.bridgeJSLiftParameter(_self).counter = AnyCounter.bridgeJSLiftParameter(value) + DataProcessorManager.bridgeJSLiftParameter(_self).setProcessorHttpStatus(_: Optional.bridgeJSLiftParameter(statusIsSome, statusValue)) #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_backupCounter_get") -@_cdecl("bjs_CounterManager_backupCounter_get") -public func _bjs_CounterManager_backupCounter_get(_self: UnsafeMutableRawPointer) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_getProcessorAPIResult") +@_cdecl("bjs_DataProcessorManager_getProcessorAPIResult") +public func _bjs_DataProcessorManager_getProcessorAPIResult(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = CounterManager.bridgeJSLiftParameter(_self).backupCounter.flatMap { - $0 as? AnyCounter + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).getProcessorAPIResult() + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_setProcessorAPIResult") +@_cdecl("bjs_DataProcessorManager_setProcessorAPIResult") +public func _bjs_DataProcessorManager_setProcessorAPIResult(_self: UnsafeMutableRawPointer, apiResultIsSome: Int32, apiResultCaseId: Int32) -> Void { + #if arch(wasm32) + DataProcessorManager.bridgeJSLiftParameter(_self).setProcessorAPIResult(_: Optional.bridgeJSLiftParameter(apiResultIsSome, apiResultCaseId)) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_processor_get") +@_cdecl("bjs_DataProcessorManager_processor_get") +public func _bjs_DataProcessorManager_processor_get(_self: UnsafeMutableRawPointer) -> Int32 { + #if arch(wasm32) + let ret = DataProcessorManager.bridgeJSLiftParameter(_self).processor as! AnyDataProcessor + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_processor_set") +@_cdecl("bjs_DataProcessorManager_processor_set") +public func _bjs_DataProcessorManager_processor_set(_self: UnsafeMutableRawPointer, value: Int32) -> Void { + #if arch(wasm32) + DataProcessorManager.bridgeJSLiftParameter(_self).processor = AnyDataProcessor.bridgeJSLiftParameter(value) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_DataProcessorManager_backupProcessor_get") +@_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 } return ret.bridgeJSLowerReturn() #else @@ -3415,116 +3963,381 @@ public func _bjs_CounterManager_backupCounter_get(_self: UnsafeMutableRawPointer #endif } -@_expose(wasm, "bjs_CounterManager_backupCounter_set") -@_cdecl("bjs_CounterManager_backupCounter_set") -public func _bjs_CounterManager_backupCounter_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueValue: Int32) -> Void { +@_expose(wasm, "bjs_DataProcessorManager_backupProcessor_set") +@_cdecl("bjs_DataProcessorManager_backupProcessor_set") +public func _bjs_DataProcessorManager_backupProcessor_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueValue: Int32) -> Void { #if arch(wasm32) - CounterManager.bridgeJSLiftParameter(_self).backupCounter = Optional.bridgeJSLiftParameter(valueIsSome, valueValue) + DataProcessorManager.bridgeJSLiftParameter(_self).backupProcessor = Optional.bridgeJSLiftParameter(valueIsSome, valueValue) #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_CounterManager_deinit") -@_cdecl("bjs_CounterManager_deinit") -public func _bjs_CounterManager_deinit(pointer: UnsafeMutableRawPointer) { - Unmanaged.fromOpaque(pointer).release() +@_expose(wasm, "bjs_DataProcessorManager_deinit") +@_cdecl("bjs_DataProcessorManager_deinit") +public func _bjs_DataProcessorManager_deinit(pointer: UnsafeMutableRawPointer) { + Unmanaged.fromOpaque(pointer).release() } -extension CounterManager: ConvertibleToJSValue, _BridgedSwiftHeapObject { +extension DataProcessorManager: ConvertibleToJSValue, _BridgedSwiftHeapObject { var jsValue: JSValue { #if arch(wasm32) - @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_CounterManager_wrap") - func _bjs_CounterManager_wrap(_: UnsafeMutableRawPointer) -> Int32 + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessorManager_wrap") + func _bjs_DataProcessorManager_wrap(_: UnsafeMutableRawPointer) -> Int32 #else - func _bjs_CounterManager_wrap(_: UnsafeMutableRawPointer) -> Int32 { + func _bjs_DataProcessorManager_wrap(_: UnsafeMutableRawPointer) -> Int32 { fatalError("Only available on WebAssembly") } #endif - return .object(JSObject(id: UInt32(bitPattern: _bjs_CounterManager_wrap(Unmanaged.passRetained(self).toOpaque())))) + return .object(JSObject(id: UInt32(bitPattern: _bjs_DataProcessorManager_wrap(Unmanaged.passRetained(self).toOpaque())))) } } -@_expose(wasm, "bjs_SwiftCounter_init") -@_cdecl("bjs_SwiftCounter_init") -public func _bjs_SwiftCounter_init() -> UnsafeMutableRawPointer { +@_expose(wasm, "bjs_SwiftDataProcessor_init") +@_cdecl("bjs_SwiftDataProcessor_init") +public func _bjs_SwiftDataProcessor_init() -> UnsafeMutableRawPointer { #if arch(wasm32) - let ret = SwiftCounter() + let ret = SwiftDataProcessor() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_SwiftCounter_increment") -@_cdecl("bjs_SwiftCounter_increment") -public func _bjs_SwiftCounter_increment(_self: UnsafeMutableRawPointer, amount: Int32) -> Void { +@_expose(wasm, "bjs_SwiftDataProcessor_increment") +@_cdecl("bjs_SwiftDataProcessor_increment") +public func _bjs_SwiftDataProcessor_increment(_self: UnsafeMutableRawPointer, amount: Int32) -> Void { #if arch(wasm32) - SwiftCounter.bridgeJSLiftParameter(_self).increment(by: Int.bridgeJSLiftParameter(amount)) + SwiftDataProcessor.bridgeJSLiftParameter(_self).increment(by: Int.bridgeJSLiftParameter(amount)) #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_SwiftCounter_getValue") -@_cdecl("bjs_SwiftCounter_getValue") -public func _bjs_SwiftCounter_getValue(_self: UnsafeMutableRawPointer) -> Int32 { +@_expose(wasm, "bjs_SwiftDataProcessor_getValue") +@_cdecl("bjs_SwiftDataProcessor_getValue") +public func _bjs_SwiftDataProcessor_getValue(_self: UnsafeMutableRawPointer) -> Int32 { #if arch(wasm32) - let ret = SwiftCounter.bridgeJSLiftParameter(_self).getValue() + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).getValue() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_SwiftCounter_setLabelElements") -@_cdecl("bjs_SwiftCounter_setLabelElements") -public func _bjs_SwiftCounter_setLabelElements(_self: UnsafeMutableRawPointer, labelPrefixBytes: Int32, labelPrefixLength: Int32, labelSuffixBytes: Int32, labelSuffixLength: Int32) -> Void { +@_expose(wasm, "bjs_SwiftDataProcessor_setLabelElements") +@_cdecl("bjs_SwiftDataProcessor_setLabelElements") +public func _bjs_SwiftDataProcessor_setLabelElements(_self: UnsafeMutableRawPointer, labelPrefixBytes: Int32, labelPrefixLength: Int32, labelSuffixBytes: Int32, labelSuffixLength: Int32) -> Void { #if arch(wasm32) - SwiftCounter.bridgeJSLiftParameter(_self).setLabelElements(_: String.bridgeJSLiftParameter(labelPrefixBytes, labelPrefixLength), _: String.bridgeJSLiftParameter(labelSuffixBytes, labelSuffixLength)) + SwiftDataProcessor.bridgeJSLiftParameter(_self).setLabelElements(_: String.bridgeJSLiftParameter(labelPrefixBytes, labelPrefixLength), _: String.bridgeJSLiftParameter(labelSuffixBytes, labelSuffixLength)) #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_SwiftCounter_getLabel") -@_cdecl("bjs_SwiftCounter_getLabel") -public func _bjs_SwiftCounter_getLabel(_self: UnsafeMutableRawPointer) -> Void { +@_expose(wasm, "bjs_SwiftDataProcessor_getLabel") +@_cdecl("bjs_SwiftDataProcessor_getLabel") +public func _bjs_SwiftDataProcessor_getLabel(_self: UnsafeMutableRawPointer) -> Void { #if arch(wasm32) - let ret = SwiftCounter.bridgeJSLiftParameter(_self).getLabel() + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).getLabel() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_SwiftCounter_isEven") -@_cdecl("bjs_SwiftCounter_isEven") -public func _bjs_SwiftCounter_isEven(_self: UnsafeMutableRawPointer) -> Int32 { +@_expose(wasm, "bjs_SwiftDataProcessor_isEven") +@_cdecl("bjs_SwiftDataProcessor_isEven") +public func _bjs_SwiftDataProcessor_isEven(_self: UnsafeMutableRawPointer) -> Int32 { #if arch(wasm32) - let ret = SwiftCounter.bridgeJSLiftParameter(_self).isEven() + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).isEven() return ret.bridgeJSLowerReturn() #else fatalError("Only available on WebAssembly") #endif } -@_expose(wasm, "bjs_SwiftCounter_deinit") -@_cdecl("bjs_SwiftCounter_deinit") -public func _bjs_SwiftCounter_deinit(pointer: UnsafeMutableRawPointer) { - Unmanaged.fromOpaque(pointer).release() +@_expose(wasm, "bjs_SwiftDataProcessor_processGreeter") +@_cdecl("bjs_SwiftDataProcessor_processGreeter") +public func _bjs_SwiftDataProcessor_processGreeter(_self: UnsafeMutableRawPointer, greeter: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).processGreeter(_: Greeter.bridgeJSLiftParameter(greeter)) + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_createGreeter") +@_cdecl("bjs_SwiftDataProcessor_createGreeter") +public func _bjs_SwiftDataProcessor_createGreeter(_self: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).createGreeter() + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_processOptionalGreeter") +@_cdecl("bjs_SwiftDataProcessor_processOptionalGreeter") +public func _bjs_SwiftDataProcessor_processOptionalGreeter(_self: UnsafeMutableRawPointer, greeterIsSome: Int32, greeterValue: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).processOptionalGreeter(_: Optional.bridgeJSLiftParameter(greeterIsSome, greeterValue)) + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_createOptionalGreeter") +@_cdecl("bjs_SwiftDataProcessor_createOptionalGreeter") +public func _bjs_SwiftDataProcessor_createOptionalGreeter(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).createOptionalGreeter() + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_handleAPIResult") +@_cdecl("bjs_SwiftDataProcessor_handleAPIResult") +public func _bjs_SwiftDataProcessor_handleAPIResult(_self: UnsafeMutableRawPointer, resultIsSome: Int32, resultCaseId: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).handleAPIResult(_: Optional.bridgeJSLiftParameter(resultIsSome, resultCaseId)) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_getAPIResult") +@_cdecl("bjs_SwiftDataProcessor_getAPIResult") +public func _bjs_SwiftDataProcessor_getAPIResult(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).getAPIResult() + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_count_get") +@_cdecl("bjs_SwiftDataProcessor_count_get") +public func _bjs_SwiftDataProcessor_count_get(_self: UnsafeMutableRawPointer) -> Int32 { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).count + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_count_set") +@_cdecl("bjs_SwiftDataProcessor_count_set") +public func _bjs_SwiftDataProcessor_count_set(_self: UnsafeMutableRawPointer, value: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).count = Int.bridgeJSLiftParameter(value) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_name_get") +@_cdecl("bjs_SwiftDataProcessor_name_get") +public func _bjs_SwiftDataProcessor_name_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).name + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalTag_get") +@_cdecl("bjs_SwiftDataProcessor_optionalTag_get") +public func _bjs_SwiftDataProcessor_optionalTag_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalTag + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalTag_set") +@_cdecl("bjs_SwiftDataProcessor_optionalTag_set") +public func _bjs_SwiftDataProcessor_optionalTag_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueBytes: Int32, valueLength: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalTag = Optional.bridgeJSLiftParameter(valueIsSome, valueBytes, valueLength) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalCount_get") +@_cdecl("bjs_SwiftDataProcessor_optionalCount_get") +public func _bjs_SwiftDataProcessor_optionalCount_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalCount + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalCount_set") +@_cdecl("bjs_SwiftDataProcessor_optionalCount_set") +public func _bjs_SwiftDataProcessor_optionalCount_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueValue: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalCount = Optional.bridgeJSLiftParameter(valueIsSome, valueValue) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_direction_get") +@_cdecl("bjs_SwiftDataProcessor_direction_get") +public func _bjs_SwiftDataProcessor_direction_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).direction + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_direction_set") +@_cdecl("bjs_SwiftDataProcessor_direction_set") +public func _bjs_SwiftDataProcessor_direction_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueValue: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).direction = Optional.bridgeJSLiftParameter(valueIsSome, valueValue) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalTheme_get") +@_cdecl("bjs_SwiftDataProcessor_optionalTheme_get") +public func _bjs_SwiftDataProcessor_optionalTheme_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalTheme + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalTheme_set") +@_cdecl("bjs_SwiftDataProcessor_optionalTheme_set") +public func _bjs_SwiftDataProcessor_optionalTheme_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueBytes: Int32, valueLength: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalTheme = Optional.bridgeJSLiftParameter(valueIsSome, valueBytes, valueLength) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_httpStatus_get") +@_cdecl("bjs_SwiftDataProcessor_httpStatus_get") +public func _bjs_SwiftDataProcessor_httpStatus_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).httpStatus + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_httpStatus_set") +@_cdecl("bjs_SwiftDataProcessor_httpStatus_set") +public func _bjs_SwiftDataProcessor_httpStatus_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueValue: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).httpStatus = Optional.bridgeJSLiftParameter(valueIsSome, valueValue) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_apiResult_get") +@_cdecl("bjs_SwiftDataProcessor_apiResult_get") +public func _bjs_SwiftDataProcessor_apiResult_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).apiResult + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_apiResult_set") +@_cdecl("bjs_SwiftDataProcessor_apiResult_set") +public func _bjs_SwiftDataProcessor_apiResult_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueCaseId: Int32) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).apiResult = Optional.bridgeJSLiftParameter(valueIsSome, valueCaseId) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_helper_get") +@_cdecl("bjs_SwiftDataProcessor_helper_get") +public func _bjs_SwiftDataProcessor_helper_get(_self: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).helper + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_helper_set") +@_cdecl("bjs_SwiftDataProcessor_helper_set") +public func _bjs_SwiftDataProcessor_helper_set(_self: UnsafeMutableRawPointer, value: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).helper = Greeter.bridgeJSLiftParameter(value) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalHelper_get") +@_cdecl("bjs_SwiftDataProcessor_optionalHelper_get") +public func _bjs_SwiftDataProcessor_optionalHelper_get(_self: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + let ret = SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalHelper + return ret.bridgeJSLowerReturn() + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_optionalHelper_set") +@_cdecl("bjs_SwiftDataProcessor_optionalHelper_set") +public func _bjs_SwiftDataProcessor_optionalHelper_set(_self: UnsafeMutableRawPointer, valueIsSome: Int32, valueValue: UnsafeMutableRawPointer) -> Void { + #if arch(wasm32) + SwiftDataProcessor.bridgeJSLiftParameter(_self).optionalHelper = Optional.bridgeJSLiftParameter(valueIsSome, valueValue) + #else + fatalError("Only available on WebAssembly") + #endif +} + +@_expose(wasm, "bjs_SwiftDataProcessor_deinit") +@_cdecl("bjs_SwiftDataProcessor_deinit") +public func _bjs_SwiftDataProcessor_deinit(pointer: UnsafeMutableRawPointer) { + Unmanaged.fromOpaque(pointer).release() } -extension SwiftCounter: ConvertibleToJSValue, _BridgedSwiftHeapObject { +extension SwiftDataProcessor: ConvertibleToJSValue, _BridgedSwiftHeapObject { var jsValue: JSValue { #if arch(wasm32) - @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_SwiftCounter_wrap") - func _bjs_SwiftCounter_wrap(_: UnsafeMutableRawPointer) -> Int32 + @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_SwiftDataProcessor_wrap") + func _bjs_SwiftDataProcessor_wrap(_: UnsafeMutableRawPointer) -> Int32 #else - func _bjs_SwiftCounter_wrap(_: UnsafeMutableRawPointer) -> Int32 { + func _bjs_SwiftDataProcessor_wrap(_: UnsafeMutableRawPointer) -> Int32 { fatalError("Only available on WebAssembly") } #endif - return .object(JSObject(id: UInt32(bitPattern: _bjs_SwiftCounter_wrap(Unmanaged.passRetained(self).toOpaque())))) + return .object(JSObject(id: UInt32(bitPattern: _bjs_SwiftDataProcessor_wrap(Unmanaged.passRetained(self).toOpaque())))) } } \ No newline at end of file diff --git a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json index a6be6754..c0e0a4a8 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json +++ b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json @@ -1165,7 +1165,7 @@ }, { "constructor" : { - "abiName" : "bjs_CounterManager_init", + "abiName" : "bjs_DataProcessorManager_init", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1173,11 +1173,11 @@ }, "parameters" : [ { - "label" : "counter", - "name" : "counter", + "label" : "processor", + "name" : "processor", "type" : { "swiftProtocol" : { - "_0" : "Counter" + "_0" : "DataProcessor" } } } @@ -1185,7 +1185,7 @@ }, "methods" : [ { - "abiName" : "bjs_CounterManager_incrementByAmount", + "abiName" : "bjs_DataProcessorManager_incrementByAmount", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1210,13 +1210,13 @@ } }, { - "abiName" : "bjs_CounterManager_setCounterLabel", + "abiName" : "bjs_DataProcessorManager_setProcessorLabel", "effects" : { "isAsync" : false, "isStatic" : false, "isThrows" : false }, - "name" : "setCounterLabel", + "name" : "setProcessorLabel", "parameters" : [ { "label" : "_", @@ -1244,13 +1244,13 @@ } }, { - "abiName" : "bjs_CounterManager_isCounterEven", + "abiName" : "bjs_DataProcessorManager_isProcessorEven", "effects" : { "isAsync" : false, "isStatic" : false, "isThrows" : false }, - "name" : "isCounterEven", + "name" : "isProcessorEven", "parameters" : [ ], @@ -1261,13 +1261,13 @@ } }, { - "abiName" : "bjs_CounterManager_getCounterLabel", + "abiName" : "bjs_DataProcessorManager_getProcessorLabel", "effects" : { "isAsync" : false, "isStatic" : false, "isThrows" : false }, - "name" : "getCounterLabel", + "name" : "getProcessorLabel", "parameters" : [ ], @@ -1278,7 +1278,7 @@ } }, { - "abiName" : "bjs_CounterManager_getCurrentValue", + "abiName" : "bjs_DataProcessorManager_getCurrentValue", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1295,7 +1295,7 @@ } }, { - "abiName" : "bjs_CounterManager_incrementBoth", + "abiName" : "bjs_DataProcessorManager_incrementBoth", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1312,7 +1312,7 @@ } }, { - "abiName" : "bjs_CounterManager_getBackupValue", + "abiName" : "bjs_DataProcessorManager_getBackupValue", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1333,7 +1333,7 @@ } }, { - "abiName" : "bjs_CounterManager_hasBackup", + "abiName" : "bjs_DataProcessorManager_hasBackup", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1346,42 +1346,346 @@ "returnType" : { "bool" : { + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_getProcessorOptionalTag", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getProcessorOptionalTag", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "string" : { + + } + } + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_setProcessorOptionalTag", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "setProcessorOptionalTag", + "parameters" : [ + { + "label" : "_", + "name" : "tag", + "type" : { + "optional" : { + "_0" : { + "string" : { + + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_getProcessorOptionalCount", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getProcessorOptionalCount", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "int" : { + + } + } + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_setProcessorOptionalCount", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "setProcessorOptionalCount", + "parameters" : [ + { + "label" : "_", + "name" : "count", + "type" : { + "optional" : { + "_0" : { + "int" : { + + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_getProcessorDirection", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getProcessorDirection", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "caseEnum" : { + "_0" : "Direction" + } + } + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_setProcessorDirection", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "setProcessorDirection", + "parameters" : [ + { + "label" : "_", + "name" : "direction", + "type" : { + "optional" : { + "_0" : { + "caseEnum" : { + "_0" : "Direction" + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_getProcessorTheme", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getProcessorTheme", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "Theme", + "_1" : "String" + } + } + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_setProcessorTheme", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "setProcessorTheme", + "parameters" : [ + { + "label" : "_", + "name" : "theme", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "Theme", + "_1" : "String" + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_getProcessorHttpStatus", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getProcessorHttpStatus", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "HttpStatus", + "_1" : "Int" + } + } + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_setProcessorHttpStatus", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "setProcessorHttpStatus", + "parameters" : [ + { + "label" : "_", + "name" : "status", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "HttpStatus", + "_1" : "Int" + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_getProcessorAPIResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getProcessorAPIResult", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } + }, + { + "abiName" : "bjs_DataProcessorManager_setProcessorAPIResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "setProcessorAPIResult", + "parameters" : [ + { + "label" : "_", + "name" : "apiResult", + "type" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } + } + ], + "returnType" : { + "void" : { + } } } ], - "name" : "CounterManager", + "name" : "DataProcessorManager", "properties" : [ { "isReadonly" : false, "isStatic" : false, - "name" : "counter", + "name" : "processor", "type" : { "swiftProtocol" : { - "_0" : "Counter" + "_0" : "DataProcessor" } } }, { "isReadonly" : false, "isStatic" : false, - "name" : "backupCounter", + "name" : "backupProcessor", "type" : { "optional" : { "_0" : { "swiftProtocol" : { - "_0" : "Counter" + "_0" : "DataProcessor" } } } } } ], - "swiftCallName" : "CounterManager" + "swiftCallName" : "DataProcessorManager" }, { "constructor" : { - "abiName" : "bjs_SwiftCounter_init", + "abiName" : "bjs_SwiftDataProcessor_init", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1393,7 +1697,7 @@ }, "methods" : [ { - "abiName" : "bjs_SwiftCounter_increment", + "abiName" : "bjs_SwiftDataProcessor_increment", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1418,7 +1722,7 @@ } }, { - "abiName" : "bjs_SwiftCounter_getValue", + "abiName" : "bjs_SwiftDataProcessor_getValue", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1435,7 +1739,7 @@ } }, { - "abiName" : "bjs_SwiftCounter_setLabelElements", + "abiName" : "bjs_SwiftDataProcessor_setLabelElements", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1469,7 +1773,7 @@ } }, { - "abiName" : "bjs_SwiftCounter_getLabel", + "abiName" : "bjs_SwiftDataProcessor_getLabel", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1486,7 +1790,7 @@ } }, { - "abiName" : "bjs_SwiftCounter_isEven", + "abiName" : "bjs_SwiftDataProcessor_isEven", "effects" : { "isAsync" : false, "isStatic" : false, @@ -1501,13 +1805,284 @@ } } + }, + { + "abiName" : "bjs_SwiftDataProcessor_processGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "processGreeter", + "parameters" : [ + { + "label" : "_", + "name" : "greeter", + "type" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + ], + "returnType" : { + "string" : { + + } + } + }, + { + "abiName" : "bjs_SwiftDataProcessor_createGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "createGreeter", + "parameters" : [ + + ], + "returnType" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + }, + { + "abiName" : "bjs_SwiftDataProcessor_processOptionalGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "processOptionalGreeter", + "parameters" : [ + { + "label" : "_", + "name" : "greeter", + "type" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + } + } + ], + "returnType" : { + "string" : { + + } + } + }, + { + "abiName" : "bjs_SwiftDataProcessor_createOptionalGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "createOptionalGreeter", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + } + }, + { + "abiName" : "bjs_SwiftDataProcessor_handleAPIResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "handleAPIResult", + "parameters" : [ + { + "label" : "_", + "name" : "result", + "type" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_SwiftDataProcessor_getAPIResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getAPIResult", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } + } + ], + "name" : "SwiftDataProcessor", + "properties" : [ + { + "isReadonly" : false, + "isStatic" : false, + "name" : "count", + "type" : { + "int" : { + + } + } + }, + { + "isReadonly" : true, + "isStatic" : false, + "name" : "name", + "type" : { + "string" : { + + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "optionalTag", + "type" : { + "optional" : { + "_0" : { + "string" : { + + } + } + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "optionalCount", + "type" : { + "optional" : { + "_0" : { + "int" : { + + } + } + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "direction", + "type" : { + "optional" : { + "_0" : { + "caseEnum" : { + "_0" : "Direction" + } + } + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "optionalTheme", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "Theme", + "_1" : "String" + } + } + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "httpStatus", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "HttpStatus", + "_1" : "Int" + } + } + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "apiResult", + "type" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "helper", + "type" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + }, + { + "isReadonly" : false, + "isStatic" : false, + "name" : "optionalHelper", + "type" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + } } ], - "name" : "SwiftCounter", - "properties" : [ - - ], - "swiftCallName" : "SwiftCounter" + "swiftCallName" : "SwiftDataProcessor" } ], "enums" : [ @@ -5786,7 +6361,7 @@ { "methods" : [ { - "abiName" : "bjs_Counter_increment", + "abiName" : "bjs_DataProcessor_increment", "effects" : { "isAsync" : false, "isStatic" : false, @@ -5811,7 +6386,7 @@ } }, { - "abiName" : "bjs_Counter_getValue", + "abiName" : "bjs_DataProcessor_getValue", "effects" : { "isAsync" : false, "isStatic" : false, @@ -5828,7 +6403,7 @@ } }, { - "abiName" : "bjs_Counter_setLabelElements", + "abiName" : "bjs_DataProcessor_setLabelElements", "effects" : { "isAsync" : false, "isStatic" : false, @@ -5862,7 +6437,7 @@ } }, { - "abiName" : "bjs_Counter_getLabel", + "abiName" : "bjs_DataProcessor_getLabel", "effects" : { "isAsync" : false, "isStatic" : false, @@ -5879,7 +6454,7 @@ } }, { - "abiName" : "bjs_Counter_isEven", + "abiName" : "bjs_DataProcessor_isEven", "effects" : { "isAsync" : false, "isStatic" : false, @@ -5894,9 +6469,273 @@ } } + }, + { + "abiName" : "bjs_DataProcessor_processGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "processGreeter", + "parameters" : [ + { + "label" : "_", + "name" : "greeter", + "type" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + ], + "returnType" : { + "string" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessor_createGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "createGreeter", + "parameters" : [ + + ], + "returnType" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + }, + { + "abiName" : "bjs_DataProcessor_processOptionalGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "processOptionalGreeter", + "parameters" : [ + { + "label" : "_", + "name" : "greeter", + "type" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + } + } + ], + "returnType" : { + "string" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessor_createOptionalGreeter", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "createOptionalGreeter", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + } + }, + { + "abiName" : "bjs_DataProcessor_handleAPIResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "handleAPIResult", + "parameters" : [ + { + "label" : "_", + "name" : "result", + "type" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } + } + ], + "returnType" : { + "void" : { + + } + } + }, + { + "abiName" : "bjs_DataProcessor_getAPIResult", + "effects" : { + "isAsync" : false, + "isStatic" : false, + "isThrows" : false + }, + "name" : "getAPIResult", + "parameters" : [ + + ], + "returnType" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } } ], - "name" : "Counter" + "name" : "DataProcessor", + "properties" : [ + { + "isReadonly" : false, + "name" : "count", + "type" : { + "int" : { + + } + } + }, + { + "isReadonly" : true, + "name" : "name", + "type" : { + "string" : { + + } + } + }, + { + "isReadonly" : false, + "name" : "optionalTag", + "type" : { + "optional" : { + "_0" : { + "string" : { + + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "optionalCount", + "type" : { + "optional" : { + "_0" : { + "int" : { + + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "direction", + "type" : { + "optional" : { + "_0" : { + "caseEnum" : { + "_0" : "Direction" + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "optionalTheme", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "Theme", + "_1" : "String" + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "httpStatus", + "type" : { + "optional" : { + "_0" : { + "rawValueEnum" : { + "_0" : "HttpStatus", + "_1" : "Int" + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "apiResult", + "type" : { + "optional" : { + "_0" : { + "associatedValueEnum" : { + "_0" : "APIResult" + } + } + } + } + }, + { + "isReadonly" : false, + "name" : "helper", + "type" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + }, + { + "isReadonly" : false, + "name" : "optionalHelper", + "type" : { + "optional" : { + "_0" : { + "swiftHeapObject" : { + "_0" : "Greeter" + } + } + } + } + } + ] } ] } \ No newline at end of file diff --git a/Tests/prelude.mjs b/Tests/prelude.mjs index e2d0c980..10c390af 100644 --- a/Tests/prelude.mjs +++ b/Tests/prelude.mjs @@ -809,75 +809,251 @@ function setupTestGlobals(global) { /** @param {import('./../.build/plugins/PackageToJS/outputs/PackageTests/bridge-js.d.ts').Exports} exports */ function testProtocolSupport(exports) { - let counterValue = 0; - let counterLabel = ""; - const jsCounter = { - increment(amount) { counterValue += amount; }, - getValue() { return counterValue; }, - setLabelElements(labelPrefix, labelSuffix) { counterLabel = labelPrefix + labelSuffix; }, - getLabel() { return counterLabel; }, - isEven() { return counterValue % 2 === 0; } + let processorValue = 0; + let processorLabel = ""; + let lastAPIResult = null; + const jsProcessor = { + count: 0, + name: "JSProcessor", + optionalTag: null, + direction: null, + optionalTheme: null, + httpStatus: null, + get apiResult() { return lastAPIResult; }, + set apiResult(value) { lastAPIResult = value; }, + helper: new exports.Greeter("JSHelper"), + optionalHelper: null, + optionalCount: null, + increment(amount) { processorValue += amount; this.count += amount; }, + getValue() { return processorValue; }, + setLabelElements(labelPrefix, labelSuffix) { processorLabel = labelPrefix + labelSuffix; }, + getLabel() { return processorLabel; }, + isEven() { return processorValue % 2 === 0; }, + processGreeter(greeter) { return `JSProcessor processed: ${greeter.greet()}`; }, + createGreeter() { return new exports.Greeter("JSProcessorGreeter"); }, + processOptionalGreeter(greeter) { + return greeter ? `JSProcessor processed optional: ${greeter.greet()}` : "JSProcessor received null"; + }, + createOptionalGreeter() { return new exports.Greeter("JSOptionalGreeter"); }, + handleAPIResult(result) { lastAPIResult = result; }, + getAPIResult() { return lastAPIResult; } }; - const manager = new exports.CounterManager(jsCounter); - manager.incrementByAmount(4); - assert.equal(manager.getCurrentValue(), 4); - - manager.setCounterLabel("Test", "Label"); - assert.equal(manager.getCounterLabel(), "TestLabel"); - assert.equal(jsCounter.getLabel(), "TestLabel"); - - assert.equal(manager.isCounterEven(), true); - manager.incrementByAmount(1); - assert.equal(manager.isCounterEven(), false); - assert.equal(jsCounter.isEven(), false); - - jsCounter.increment(3); - assert.equal(jsCounter.getValue(), 8); - manager.release(); - - const swiftCounter = new exports.SwiftCounter(); - const swiftManager = new exports.CounterManager(swiftCounter); + const successResult = { tag: exports.Result.Tag.Success, param0: "Operation completed" }; + const failureResult = { tag: exports.Result.Tag.Failure, param0: 500 }; + + const jsManager = new exports.DataProcessorManager(jsProcessor); + + jsManager.incrementByAmount(4); + assert.equal(jsManager.getCurrentValue(), 4); + assert.equal(jsProcessor.count, 4); + + jsManager.setProcessorLabel("Test", "Label"); + assert.equal(jsManager.getProcessorLabel(), "TestLabel"); + + assert.equal(jsManager.isProcessorEven(), true); + jsManager.incrementByAmount(1); + assert.equal(jsManager.isProcessorEven(), false); + + assert.equal(jsManager.getProcessorOptionalTag(), null); + jsManager.setProcessorOptionalTag("test-tag"); + assert.equal(jsManager.getProcessorOptionalTag(), "test-tag"); + jsManager.setProcessorOptionalTag("another-tag"); + assert.equal(jsManager.getProcessorOptionalTag(), "another-tag"); + jsManager.setProcessorOptionalTag(null); + assert.equal(jsManager.getProcessorOptionalTag(), null); + + // Test direct property access for optionalTag + jsProcessor.optionalTag = "direct-tag"; + assert.equal(jsManager.getProcessorOptionalTag(), "direct-tag"); + assert.equal(jsProcessor.optionalTag, "direct-tag"); + jsProcessor.optionalTag = null; + assert.equal(jsManager.getProcessorOptionalTag(), null); + assert.equal(jsProcessor.optionalTag, null); + + assert.equal(jsManager.getProcessorDirection(), null); + jsManager.setProcessorDirection(exports.Direction.North); + assert.equal(jsManager.getProcessorDirection(), exports.Direction.North); + jsManager.setProcessorDirection(exports.Direction.East); + assert.equal(jsManager.getProcessorDirection(), exports.Direction.East); + jsManager.setProcessorDirection(null); + assert.equal(jsManager.getProcessorDirection(), null); + + assert.equal(jsManager.getProcessorTheme(), null); + jsManager.setProcessorTheme(exports.Theme.Light); + assert.equal(jsManager.getProcessorTheme(), exports.Theme.Light); + jsManager.setProcessorTheme(exports.Theme.Dark); + assert.equal(jsManager.getProcessorTheme(), exports.Theme.Dark); + jsManager.setProcessorTheme(null); + assert.equal(jsManager.getProcessorTheme(), null); + + assert.equal(jsManager.getProcessorHttpStatus(), null); + jsManager.setProcessorHttpStatus(exports.HttpStatus.Ok); + assert.equal(jsManager.getProcessorHttpStatus(), exports.HttpStatus.Ok); + jsManager.setProcessorHttpStatus(exports.HttpStatus.NotFound); + assert.equal(jsManager.getProcessorHttpStatus(), exports.HttpStatus.NotFound); + jsManager.setProcessorHttpStatus(null); + assert.equal(jsManager.getProcessorHttpStatus(), null); + + jsProcessor.handleAPIResult(successResult); + assert.deepEqual(jsProcessor.getAPIResult(), successResult); + + jsProcessor.handleAPIResult(failureResult); + assert.deepEqual(jsProcessor.getAPIResult(), failureResult); + + jsProcessor.apiResult = successResult; + assert.deepEqual(jsProcessor.apiResult, successResult); + jsProcessor.apiResult = null; + assert.equal(jsProcessor.apiResult, null); + assert.equal(jsManager.getProcessorAPIResult(), null); + + assert.equal(jsProcessor.helper.name, "JSHelper"); + const newHelper = new exports.Greeter("UpdatedHelper"); + jsProcessor.helper = newHelper; + assert.equal(jsProcessor.helper.name, "UpdatedHelper"); + assert.equal(jsProcessor.helper.greet(), "Hello, UpdatedHelper!"); + + assert.equal(jsProcessor.optionalHelper, null); + const optHelper = new exports.Greeter("OptHelper"); + jsProcessor.optionalHelper = optHelper; + assert.equal(jsProcessor.optionalHelper.name, "OptHelper"); + assert.equal(jsProcessor.optionalHelper.greet(), "Hello, OptHelper!"); + jsProcessor.optionalHelper = null; + assert.equal(jsProcessor.optionalHelper, null); + + assert.equal(jsManager.getProcessorOptionalCount(), null); + jsManager.setProcessorOptionalCount(42); + assert.equal(jsManager.getProcessorOptionalCount(), 42); + jsManager.setProcessorOptionalCount(0); + assert.equal(jsManager.getProcessorOptionalCount(), 0); + jsManager.setProcessorOptionalCount(-100); + assert.equal(jsManager.getProcessorOptionalCount(), -100); + jsManager.setProcessorOptionalCount(null); + assert.equal(jsManager.getProcessorOptionalCount(), null); + + assert.equal(jsProcessor.optionalCount, null); + jsProcessor.optionalCount = 42; + assert.equal(jsProcessor.optionalCount, 42); + assert.equal(jsManager.getProcessorOptionalCount(), 42); + jsProcessor.optionalCount = 0; + assert.equal(jsProcessor.optionalCount, 0); + jsProcessor.optionalCount = null; + assert.equal(jsProcessor.optionalCount, null); + + newHelper.release(); + optHelper.release(); + jsManager.release(); + + const swiftProcessor = new exports.SwiftDataProcessor(); + const swiftManager = new exports.DataProcessorManager(swiftProcessor); swiftManager.incrementByAmount(10); assert.equal(swiftManager.getCurrentValue(), 10); - swiftManager.setCounterLabel("Swift", "Label"); - assert.equal(swiftManager.getCounterLabel(), "SwiftLabel"); + swiftManager.setProcessorLabel("Swift", "Label"); + assert.equal(swiftManager.getProcessorLabel(), "SwiftLabel"); + + assert.equal(swiftManager.isProcessorEven(), true); + swiftManager.incrementByAmount(1); + assert.equal(swiftManager.isProcessorEven(), false); + + assert.equal(swiftManager.getProcessorDirection(), null); + swiftManager.setProcessorDirection(exports.Direction.South); + assert.equal(swiftManager.getProcessorDirection(), exports.Direction.South); + swiftManager.setProcessorDirection(exports.Direction.West); + assert.equal(swiftManager.getProcessorDirection(), exports.Direction.West); + swiftManager.setProcessorDirection(null); + assert.equal(swiftManager.getProcessorDirection(), null); + + assert.equal(swiftManager.getProcessorTheme(), null); + swiftProcessor.optionalTheme = exports.Theme.Light; + assert.equal(swiftManager.getProcessorTheme(), exports.Theme.Light); + swiftManager.setProcessorTheme(exports.Theme.Auto); + assert.equal(swiftManager.getProcessorTheme(), exports.Theme.Auto); + swiftManager.setProcessorTheme(exports.Theme.Light); + assert.equal(swiftManager.getProcessorTheme(), exports.Theme.Light); + swiftManager.setProcessorTheme(null); + assert.equal(swiftManager.getProcessorTheme(), null); + + assert.equal(swiftManager.getProcessorHttpStatus(), null); + swiftManager.setProcessorHttpStatus(exports.HttpStatus.ServerError); + assert.equal(swiftManager.getProcessorHttpStatus(), exports.HttpStatus.ServerError); + swiftManager.setProcessorHttpStatus(exports.HttpStatus.Ok); + assert.equal(swiftManager.getProcessorHttpStatus(), exports.HttpStatus.Ok); + swiftManager.setProcessorHttpStatus(null); + assert.equal(swiftManager.getProcessorHttpStatus(), null); + + swiftProcessor.handleAPIResult(successResult); + assert.deepEqual(swiftProcessor.getAPIResult(), successResult); + assert.deepEqual(swiftManager.getProcessorAPIResult(), successResult); + + swiftProcessor.handleAPIResult(failureResult); + assert.deepEqual(swiftProcessor.getAPIResult(), failureResult); + assert.deepEqual(swiftManager.getProcessorAPIResult(), failureResult); + swiftManager.setProcessorAPIResult(successResult); + assert.deepEqual(swiftProcessor.getAPIResult(), successResult); - swiftCounter.increment(5); - assert.equal(swiftCounter.getValue(), 15); swiftManager.release(); - swiftCounter.release(); - - let optionalCounterValue = 100; - let optionalCounterLabel = "optional"; - const optionalCounter = { - increment(amount) { optionalCounterValue += amount; }, - getValue() { return optionalCounterValue; }, - setLabelElements(labelPrefix, labelSuffix) { optionalCounterLabel = labelPrefix + labelSuffix; }, - getLabel() { return optionalCounterLabel; }, - isEven() { return optionalCounterValue % 2 === 0; } + swiftProcessor.release(); + + let backupValue = 100; + const backupProcessor = { + count: 100, + name: "BackupProcessor", + optionalTag: null, + direction: null, + optionalTheme: null, + httpStatus: null, + apiResult: null, + helper: new exports.Greeter("BackupHelper"), + optionalHelper: null, + optionalCount: null, + increment(amount) { backupValue += amount; this.count += amount; }, + getValue() { return backupValue; }, + setLabelElements(labelPrefix, labelSuffix) { }, + getLabel() { return "backup"; }, + isEven() { return backupValue % 2 === 0; }, + processGreeter(greeter) { return ""; }, + createGreeter() { return new exports.Greeter("BackupGreeter"); }, + processOptionalGreeter(greeter) { return ""; }, + createOptionalGreeter() { return null; }, + handleAPIResult(result) { }, + getAPIResult() { return { tag: exports.APIResult.Tag.Info }; } }; - let mainCounterValue = 0; - let mainCounterLabel = "main"; - const mainCounter = { - increment(amount) { mainCounterValue += amount; }, - getValue() { return mainCounterValue; }, - setLabelElements(labelPrefix, labelSuffix) { mainCounterLabel = labelPrefix + labelSuffix; }, - getLabel() { return mainCounterLabel; }, - isEven() { return mainCounterValue % 2 === 0; } + let mainValue = 0; + const mainProcessor = { + count: 0, + name: "MainProcessor", + optionalTag: null, + direction: null, + optionalTheme: null, + httpStatus: null, + apiResult: null, + helper: new exports.Greeter("MainHelper"), + optionalHelper: null, + optionalCount: null, + increment(amount) { mainValue += amount; this.count += amount; }, + getValue() { return mainValue; }, + setLabelElements(labelPrefix, labelSuffix) { }, + getLabel() { return "main"; }, + isEven() { return mainValue % 2 === 0; }, + processGreeter(greeter) { return ""; }, + createGreeter() { return new exports.Greeter("MainGreeter"); }, + processOptionalGreeter(greeter) { return ""; }, + createOptionalGreeter() { return null; }, + handleAPIResult(result) { }, + getAPIResult() { return { tag: exports.APIResult.Tag.Info }; } }; - const managerWithOptional = new exports.CounterManager(mainCounter); + const managerWithOptional = new exports.DataProcessorManager(mainProcessor); - assert.equal(managerWithOptional.backupCounter, null); + assert.equal(managerWithOptional.backupProcessor, null); assert.equal(managerWithOptional.hasBackup(), false); assert.equal(managerWithOptional.getBackupValue(), null); - managerWithOptional.backupCounter = optionalCounter; - assert.notEqual(managerWithOptional.backupCounter, null); + managerWithOptional.backupProcessor = backupProcessor; + assert.notEqual(managerWithOptional.backupProcessor, null); assert.equal(managerWithOptional.hasBackup(), true); managerWithOptional.incrementBoth(); @@ -888,21 +1064,24 @@ function testProtocolSupport(exports) { assert.equal(managerWithOptional.getCurrentValue(), 2); assert.equal(managerWithOptional.getBackupValue(), 102); - managerWithOptional.backupCounter = null; - assert.equal(managerWithOptional.backupCounter, null); + managerWithOptional.backupProcessor = null; + assert.equal(managerWithOptional.backupProcessor, null); assert.equal(managerWithOptional.hasBackup(), false); managerWithOptional.incrementBoth(); assert.equal(managerWithOptional.getCurrentValue(), 3); assert.equal(managerWithOptional.getBackupValue(), null); - const swiftBackupCounter = new exports.SwiftCounter(); - swiftBackupCounter.increment(1); - managerWithOptional.backupCounter = swiftBackupCounter; + const swiftBackup = new exports.SwiftDataProcessor(); + managerWithOptional.backupProcessor = swiftBackup; assert.equal(managerWithOptional.hasBackup(), true); + assert.equal(managerWithOptional.getBackupValue(), 0); + + managerWithOptional.incrementBoth(); + assert.equal(managerWithOptional.getCurrentValue(), 4); assert.equal(managerWithOptional.getBackupValue(), 1); managerWithOptional.release(); - swiftBackupCounter.release(); + swiftBackup.release(); }