From 95e259cdc4e1370815d484f6b6b60841c462f9c5 Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Tue, 3 Mar 2026 08:55:31 +0100 Subject: [PATCH 1/5] pass unretained strings as tuple --- Benchmarks/Sources/Generated/BridgeJS.swift | 13 +- .../PlayBridgeJS/Generated/BridgeJS.swift | 13 +- .../Sources/BridgeJSCore/ImportTS.swift | 96 +++++- .../Sources/BridgeJSLink/JSGlueGen.swift | 21 +- .../BridgeJSCodegenTests/EnumRawType.swift | 13 +- .../BridgeJSCodegenTests/GlobalGetter.swift | 13 +- .../GlobalThisImports.swift | 39 ++- .../InvalidPropertyNames.swift | 65 ++-- .../BridgeJSCodegenTests/JSClass.swift | 39 ++- .../BridgeJSCodegenTests/Optionals.swift | 68 ++-- .../BridgeJSCodegenTests/Protocol.swift | 56 ++-- .../StringParameter.swift | 26 +- .../BridgeJSCodegenTests/SwiftClosure.swift | 52 +-- .../BridgeJSLinkTests/EnumRawType.js | 9 +- .../BridgeJSLinkTests/GlobalGetter.js | 9 +- .../BridgeJSLinkTests/GlobalThisImports.js | 27 +- .../BridgeJSLinkTests/InvalidPropertyNames.js | 45 +-- .../BridgeJSLinkTests/JSClass.js | 27 +- .../BridgeJSLinkTests/Optionals.js | 52 +-- .../BridgeJSLinkTests/Protocol.js | 50 +-- .../BridgeJSLinkTests/StringParameter.js | 18 +- .../BridgeJSLinkTests/SwiftClosure.js | 36 +- .../JavaScriptKit/BridgeJSIntrinsics.swift | 32 ++ .../Generated/BridgeJS.swift | 310 +++++++++--------- 24 files changed, 657 insertions(+), 472 deletions(-) diff --git a/Benchmarks/Sources/Generated/BridgeJS.swift b/Benchmarks/Sources/Generated/BridgeJS.swift index bf033ae21..59d4ab8de 100644 --- a/Benchmarks/Sources/Generated/BridgeJS.swift +++ b/Benchmarks/Sources/Generated/BridgeJS.swift @@ -1750,20 +1750,21 @@ func _$benchmarkHelperNoopWithNumber(_ n: Double) throws(JSException) -> Void { #if arch(wasm32) @_extern(wasm, module: "Benchmarks", name: "bjs_benchmarkRunner") -fileprivate func bjs_benchmarkRunner_extern(_ name: Int32, _ body: Int32) -> Void +fileprivate func bjs_benchmarkRunner_extern(_ nameBytes: Int32, _ nameLength: Int32, _ body: Int32) -> Void #else -fileprivate func bjs_benchmarkRunner_extern(_ name: Int32, _ body: Int32) -> Void { +fileprivate func bjs_benchmarkRunner_extern(_ nameBytes: Int32, _ nameLength: Int32, _ body: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_benchmarkRunner(_ name: Int32, _ body: Int32) -> Void { - return bjs_benchmarkRunner_extern(name, body) +@inline(never) fileprivate func bjs_benchmarkRunner(_ nameBytes: Int32, _ nameLength: Int32, _ body: Int32) -> Void { + return bjs_benchmarkRunner_extern(nameBytes, nameLength, body) } func _$benchmarkRunner(_ name: String, _ body: JSObject) throws(JSException) -> Void { - let nameValue = name.bridgeJSLowerParameter() let bodyValue = body.bridgeJSLowerParameter() - bjs_benchmarkRunner(nameValue, bodyValue) + _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + bjs_benchmarkRunner(nameBytes, nameLength, bodyValue) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift b/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift index 88cdf900e..d4dd8fff0 100644 --- a/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift +++ b/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift @@ -249,20 +249,21 @@ func _$createTS2Swift() throws(JSException) -> TS2Swift { #if arch(wasm32) @_extern(wasm, module: "PlayBridgeJS", name: "bjs_TS2Swift_convert") -fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ ts: Int32) -> Int32 +fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ tsBytes: Int32, _ tsLength: Int32) -> Int32 #else -fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ ts: Int32) -> Int32 { +fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ tsBytes: Int32, _ tsLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_TS2Swift_convert(_ self: Int32, _ ts: Int32) -> Int32 { - return bjs_TS2Swift_convert_extern(self, ts) +@inline(never) fileprivate func bjs_TS2Swift_convert(_ self: Int32, _ tsBytes: Int32, _ tsLength: Int32) -> Int32 { + return bjs_TS2Swift_convert_extern(self, tsBytes, tsLength) } func _$TS2Swift_convert(_ self: JSObject, _ ts: String) throws(JSException) -> String { let selfValue = self.bridgeJSLowerParameter() - let tsValue = ts.bridgeJSLowerParameter() - let ret = bjs_TS2Swift_convert(selfValue, tsValue) + let ret = _swift_js_with_borrowed_utf8(ts) { tsBytes, tsLength in + bjs_TS2Swift_convert(selfValue, tsBytes, tsLength) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift index 8f5ac511a..91b68d2ca 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift @@ -81,6 +81,12 @@ public struct ImportTS { } class CallJSEmission { + private struct BorrowedStringArgument { + let expression: String + let forwardedParameterNames: [String] + let isOptional: Bool + } + let abiName: String let moduleName: String let context: BridgeContext @@ -95,6 +101,8 @@ public struct ImportTS { var stackLoweringStmts: [String] = [] // Values to extend lifetime during call var valuesToExtendLifetimeDuringCall: [String] = [] + // String-like parameters lowered through borrowed UTF-8 wrappers at call site. + private var borrowedStringArguments: [BorrowedStringArgument] = [] init(moduleName: String, abiName: String, context: BridgeContext = .importTS) { self.moduleName = moduleName @@ -102,6 +110,26 @@ public struct ImportTS { self.context = context } + private func borrowedStringInfo(for param: Parameter) -> (expression: String, isOptional: Bool)? { + switch param.type { + case .string: + return (param.name, false) + case .rawValueEnum(_, .string): + return ("\(param.name).rawValue", false) + case .nullable(let wrappedType, _): + switch wrappedType { + case .string: + return ("\(param.name).asOptional", true) + case .rawValueEnum(_, .string): + return ("\(param.name).asOptional?.rawValue", true) + default: + return nil + } + default: + return nil + } + } + func lowerParameter(param: Parameter) throws { let loweringInfo = try param.type.loweringParameterInfo(context: context) @@ -115,6 +143,35 @@ public struct ImportTS { default: break } + + if let borrowed = borrowedStringInfo(for: param) { + let destructuredNames = loweringInfo.loweredParameters.map { + "\(param.name)\($0.name.capitalizedFirstLetter)" + } + if loweringInfo.loweredParameters.count != destructuredNames.count || destructuredNames.isEmpty { + throw BridgeJSCoreError("Unexpected borrowed string lowering shape for parameter \(param.name)") + } + + for (index, (paramName, type)) in loweringInfo.loweredParameters.enumerated() { + let abiParamName: String + if loweringInfo.loweredParameters.count == 1 { + abiParamName = param.name + } else { + abiParamName = "\(param.name)\(paramName.capitalizedFirstLetter)" + } + abiParameterSignatures.append((abiParamName, type)) + abiParameterForwardings.append(destructuredNames[index]) + } + borrowedStringArguments.append( + BorrowedStringArgument( + expression: borrowed.expression, + forwardedParameterNames: destructuredNames, + isOptional: borrowed.isOptional + ) + ) + return + } + let initializerExpr = ExprSyntax("\(raw: param.name).bridgeJSLowerParameter()") if loweringInfo.loweredParameters.isEmpty { @@ -164,19 +221,35 @@ public struct ImportTS { let assign = (returnType == .void || returnType.usesSideChannelForOptionalReturn() || liftingInfo.valueToLift == nil) ? "" : "let ret = " - let callExpr = "\(abiName)(\(abiParameterForwardings.joined(separator: ", ")))" + var callExpr = "\(abiName)(\(abiParameterForwardings.joined(separator: ", ")))" if !valuesToExtendLifetimeDuringCall.isEmpty { - body.write( - "\(assign)withExtendedLifetime((\(valuesToExtendLifetimeDuringCall.joined(separator: ", ")))) {" - ) - body.indent { - body.write(callExpr) + callExpr = + "withExtendedLifetime((\(valuesToExtendLifetimeDuringCall.joined(separator: ", ")))) { \(callExpr) }" + } + + if !borrowedStringArguments.isEmpty { + for argument in borrowedStringArguments.reversed() { + if argument.isOptional { + guard argument.forwardedParameterNames.count == 3 else { + throw BridgeJSCoreError( + "Optional borrowed string argument must have 3 lowered values: \(argument.expression)" + ) + } + callExpr = + "_swift_js_with_optional_borrowed_utf8(\(argument.expression)) { \(argument.forwardedParameterNames[0]), \(argument.forwardedParameterNames[1]), \(argument.forwardedParameterNames[2]) in \(callExpr) }" + } else { + guard argument.forwardedParameterNames.count == 2 else { + throw BridgeJSCoreError( + "Borrowed string argument must have 2 lowered values: \(argument.expression)" + ) + } + callExpr = + "_swift_js_with_borrowed_utf8(\(argument.expression)) { \(argument.forwardedParameterNames[0]), \(argument.forwardedParameterNames[1]) in \(callExpr) }" + } } - body.write("}") - } else { - body.write("\(assign)\(callExpr)") } + body.write("\(assign)\(callExpr)") // Add exception check for ImportTS context if context == .importTS { @@ -724,7 +797,7 @@ extension BridgeType { static let int = LoweringParameterInfo(loweredParameters: [("value", .i32)]) static let float = LoweringParameterInfo(loweredParameters: [("value", .f32)]) static let double = LoweringParameterInfo(loweredParameters: [("value", .f64)]) - static let string = LoweringParameterInfo(loweredParameters: [("value", .i32)]) + static let string = LoweringParameterInfo(loweredParameters: [("bytes", .i32), ("length", .i32)]) static let jsObject = LoweringParameterInfo(loweredParameters: [("value", .i32)]) static let jsValue = LoweringParameterInfo(loweredParameters: [ ("kind", .i32), @@ -761,6 +834,9 @@ extension BridgeType { return LoweringParameterInfo(loweredParameters: [("value", .i32)]) } case .rawValueEnum(_, let rawType): + if rawType == .string { + return .string + } let wasmType = rawType.wasmCoreType ?? .i32 return LoweringParameterInfo(loweredParameters: [("value", wasmType)]) case .associatedValueEnum: diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift index ba0d0d86c..eb89a4c85 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift @@ -261,17 +261,24 @@ struct IntrinsicJSFragment: Sendable { } ) static let stringLiftParameter = IntrinsicJSFragment( - parameters: ["objectId"], + parameters: ["bytes", "count"], printCode: { arguments, context in let (scope, printer) = (context.scope, context.printer) - let objectId = arguments[0] - let objectLabel = scope.variable("\(objectId)Object") - // TODO: Implement "take" operation + let bytesExpr = arguments[0] + let countExpr = arguments[1] + let bytesLabel = scope.variable("bytesView") + let bytesToDecodeLabel = scope.variable("bytesToDecode") + let stringLabel = scope.variable("string") + printer.write( + "const \(bytesLabel) = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, \(bytesExpr), \(countExpr));" + ) + printer.write( + "const \(bytesToDecodeLabel) = (typeof SharedArrayBuffer !== \"undefined\" && \(bytesLabel).buffer instanceof SharedArrayBuffer) ? \(bytesLabel).slice() : \(bytesLabel);" + ) printer.write( - "const \(objectLabel) = \(JSGlueVariableScope.reservedSwift).memory.getObject(\(objectId));" + "const \(stringLabel) = \(JSGlueVariableScope.reservedTextDecoder).decode(\(bytesToDecodeLabel));" ) - printer.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(objectId));") - return [objectLabel] + return [stringLabel] } ) static let stringLowerReturn = IntrinsicJSFragment( diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift index c3c3d5e7f..f5525384e 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift @@ -453,19 +453,20 @@ public func _bjs_validateSession(_ session: Int32) -> Void { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_takesFeatureFlag") -fileprivate func bjs_takesFeatureFlag_extern(_ flag: Int32) -> Void +fileprivate func bjs_takesFeatureFlag_extern(_ flagBytes: Int32, _ flagLength: Int32) -> Void #else -fileprivate func bjs_takesFeatureFlag_extern(_ flag: Int32) -> Void { +fileprivate func bjs_takesFeatureFlag_extern(_ flagBytes: Int32, _ flagLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_takesFeatureFlag(_ flag: Int32) -> Void { - return bjs_takesFeatureFlag_extern(flag) +@inline(never) fileprivate func bjs_takesFeatureFlag(_ flagBytes: Int32, _ flagLength: Int32) -> Void { + return bjs_takesFeatureFlag_extern(flagBytes, flagLength) } func _$takesFeatureFlag(_ flag: FeatureFlag) throws(JSException) -> Void { - let flagValue = flag.bridgeJSLowerParameter() - bjs_takesFeatureFlag(flagValue) + _swift_js_with_borrowed_utf8(flag.rawValue) { flagBytes, flagLength in + bjs_takesFeatureFlag(flagBytes, flagLength) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift index b42f71563..c65d81556 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift @@ -20,20 +20,21 @@ func _$console_get() throws(JSException) -> JSConsole { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_JSConsole_log") -fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ message: Int32) -> Void +fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ messageBytes: Int32, _ messageLength: Int32) -> Void #else -fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ message: Int32) -> Void { +fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ messageBytes: Int32, _ messageLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_JSConsole_log(_ self: Int32, _ message: Int32) -> Void { - return bjs_JSConsole_log_extern(self, message) +@inline(never) fileprivate func bjs_JSConsole_log(_ self: Int32, _ messageBytes: Int32, _ messageLength: Int32) -> Void { + return bjs_JSConsole_log_extern(self, messageBytes, messageLength) } func _$JSConsole_log(_ self: JSObject, _ message: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let messageValue = message.bridgeJSLowerParameter() - bjs_JSConsole_log(selfValue, messageValue) + _swift_js_with_borrowed_utf8(message) { messageBytes, messageLength in + bjs_JSConsole_log(selfValue, messageBytes, messageLength) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift index 58d69eed1..c455bea9a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift @@ -20,19 +20,20 @@ func _$console_get() throws(JSException) -> JSConsole { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_parseInt") -fileprivate func bjs_parseInt_extern(_ string: Int32) -> Float64 +fileprivate func bjs_parseInt_extern(_ stringBytes: Int32, _ stringLength: Int32) -> Float64 #else -fileprivate func bjs_parseInt_extern(_ string: Int32) -> Float64 { +fileprivate func bjs_parseInt_extern(_ stringBytes: Int32, _ stringLength: Int32) -> Float64 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_parseInt(_ string: Int32) -> Float64 { - return bjs_parseInt_extern(string) +@inline(never) fileprivate func bjs_parseInt(_ stringBytes: Int32, _ stringLength: Int32) -> Float64 { + return bjs_parseInt_extern(stringBytes, stringLength) } func _$parseInt(_ string: String) throws(JSException) -> Double { - let stringValue = string.bridgeJSLowerParameter() - let ret = bjs_parseInt(stringValue) + let ret = _swift_js_with_borrowed_utf8(string) { stringBytes, stringLength in + bjs_parseInt(stringBytes, stringLength) + } if let error = _swift_js_take_exception() { throw error } @@ -41,20 +42,21 @@ func _$parseInt(_ string: String) throws(JSException) -> Double { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_JSConsole_log") -fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ message: Int32) -> Void +fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ messageBytes: Int32, _ messageLength: Int32) -> Void #else -fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ message: Int32) -> Void { +fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ messageBytes: Int32, _ messageLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_JSConsole_log(_ self: Int32, _ message: Int32) -> Void { - return bjs_JSConsole_log_extern(self, message) +@inline(never) fileprivate func bjs_JSConsole_log(_ self: Int32, _ messageBytes: Int32, _ messageLength: Int32) -> Void { + return bjs_JSConsole_log_extern(self, messageBytes, messageLength) } func _$JSConsole_log(_ self: JSObject, _ message: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let messageValue = message.bridgeJSLowerParameter() - bjs_JSConsole_log(selfValue, messageValue) + _swift_js_with_borrowed_utf8(message) { messageBytes, messageLength in + bjs_JSConsole_log(selfValue, messageBytes, messageLength) + } if let error = _swift_js_take_exception() { throw error } @@ -62,14 +64,14 @@ func _$JSConsole_log(_ self: JSObject, _ message: String) throws(JSException) -> #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WebSocket_init") -fileprivate func bjs_WebSocket_init_extern(_ url: Int32) -> Int32 +fileprivate func bjs_WebSocket_init_extern(_ urlBytes: Int32, _ urlLength: Int32) -> Int32 #else -fileprivate func bjs_WebSocket_init_extern(_ url: Int32) -> Int32 { +fileprivate func bjs_WebSocket_init_extern(_ urlBytes: Int32, _ urlLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WebSocket_init(_ url: Int32) -> Int32 { - return bjs_WebSocket_init_extern(url) +@inline(never) fileprivate func bjs_WebSocket_init(_ urlBytes: Int32, _ urlLength: Int32) -> Int32 { + return bjs_WebSocket_init_extern(urlBytes, urlLength) } #if arch(wasm32) @@ -85,8 +87,9 @@ fileprivate func bjs_WebSocket_close_extern(_ self: Int32) -> Void { } func _$WebSocket_init(_ url: String) throws(JSException) -> JSObject { - let urlValue = url.bridgeJSLowerParameter() - let ret = bjs_WebSocket_init(urlValue) + let ret = _swift_js_with_borrowed_utf8(url) { urlBytes, urlLength in + bjs_WebSocket_init(urlBytes, urlLength) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift index 2ff17ea24..77f130829 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift @@ -136,14 +136,14 @@ fileprivate func bjs_WeirdNaming_Any_get_extern(_ self: Int32) -> Int32 { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WeirdNaming_normalProperty_set") -fileprivate func bjs_WeirdNaming_normalProperty_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_WeirdNaming_normalProperty_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_WeirdNaming_normalProperty_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_WeirdNaming_normalProperty_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WeirdNaming_normalProperty_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_WeirdNaming_normalProperty_set_extern(self, newValue) +@inline(never) fileprivate func bjs_WeirdNaming_normalProperty_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_WeirdNaming_normalProperty_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @@ -172,14 +172,14 @@ fileprivate func bjs_WeirdNaming__123invalidStart_set_extern(_ self: Int32, _ ne #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WeirdNaming_property_with_spaces_set") -fileprivate func bjs_WeirdNaming_property_with_spaces_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_WeirdNaming_property_with_spaces_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_WeirdNaming_property_with_spaces_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_WeirdNaming_property_with_spaces_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WeirdNaming_property_with_spaces_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_WeirdNaming_property_with_spaces_set_extern(self, newValue) +@inline(never) fileprivate func bjs_WeirdNaming_property_with_spaces_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_WeirdNaming_property_with_spaces_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @@ -196,38 +196,38 @@ fileprivate func bjs_WeirdNaming__specialChar_set_extern(_ self: Int32, _ newVal #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WeirdNaming_constructor_set") -fileprivate func bjs_WeirdNaming_constructor_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_WeirdNaming_constructor_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_WeirdNaming_constructor_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_WeirdNaming_constructor_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WeirdNaming_constructor_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_WeirdNaming_constructor_set_extern(self, newValue) +@inline(never) fileprivate func bjs_WeirdNaming_constructor_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_WeirdNaming_constructor_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WeirdNaming_for_set") -fileprivate func bjs_WeirdNaming_for_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_WeirdNaming_for_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_WeirdNaming_for_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_WeirdNaming_for_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WeirdNaming_for_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_WeirdNaming_for_set_extern(self, newValue) +@inline(never) fileprivate func bjs_WeirdNaming_for_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_WeirdNaming_for_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WeirdNaming_any_set") -fileprivate func bjs_WeirdNaming_any_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_WeirdNaming_any_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_WeirdNaming_any_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_WeirdNaming_any_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WeirdNaming_any_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_WeirdNaming_any_set_extern(self, newValue) +@inline(never) fileprivate func bjs_WeirdNaming_any_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_WeirdNaming_any_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @@ -328,8 +328,9 @@ func _$WeirdNaming_Any_get(_ self: JSObject) throws(JSException) -> String { func _$WeirdNaming_normalProperty_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_WeirdNaming_normalProperty_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_WeirdNaming_normalProperty_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -355,8 +356,9 @@ func _$WeirdNaming__123invalidStart_set(_ self: JSObject, _ newValue: Bool) thro func _$WeirdNaming_property_with_spaces_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_WeirdNaming_property_with_spaces_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_WeirdNaming_property_with_spaces_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -373,8 +375,9 @@ func _$WeirdNaming__specialChar_set(_ self: JSObject, _ newValue: Double) throws func _$WeirdNaming_constructor_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_WeirdNaming_constructor_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_WeirdNaming_constructor_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -382,8 +385,9 @@ func _$WeirdNaming_constructor_set(_ self: JSObject, _ newValue: String) throws( func _$WeirdNaming_for_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_WeirdNaming_for_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_WeirdNaming_for_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -391,8 +395,9 @@ func _$WeirdNaming_for_set(_ self: JSObject, _ newValue: String) throws(JSExcept func _$WeirdNaming_any_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_WeirdNaming_any_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_WeirdNaming_any_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift index 11a644759..3bcb539f7 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift @@ -20,14 +20,14 @@ func _$returnAnimatable() throws(JSException) -> Animatable { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_Greeter_init") -fileprivate func bjs_Greeter_init_extern(_ name: Int32) -> Int32 +fileprivate func bjs_Greeter_init_extern(_ nameBytes: Int32, _ nameLength: Int32) -> Int32 #else -fileprivate func bjs_Greeter_init_extern(_ name: Int32) -> Int32 { +fileprivate func bjs_Greeter_init_extern(_ nameBytes: Int32, _ nameLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_Greeter_init(_ name: Int32) -> Int32 { - return bjs_Greeter_init_extern(name) +@inline(never) fileprivate func bjs_Greeter_init(_ nameBytes: Int32, _ nameLength: Int32) -> Int32 { + return bjs_Greeter_init_extern(nameBytes, nameLength) } #if arch(wasm32) @@ -56,14 +56,14 @@ fileprivate func bjs_Greeter_age_get_extern(_ self: Int32) -> Float64 { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_Greeter_name_set") -fileprivate func bjs_Greeter_name_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_Greeter_name_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_Greeter_name_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_Greeter_name_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_Greeter_name_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_Greeter_name_set_extern(self, newValue) +@inline(never) fileprivate func bjs_Greeter_name_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_Greeter_name_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @@ -80,19 +80,20 @@ fileprivate func bjs_Greeter_greet_extern(_ self: Int32) -> Int32 { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_Greeter_changeName") -fileprivate func bjs_Greeter_changeName_extern(_ self: Int32, _ name: Int32) -> Void +fileprivate func bjs_Greeter_changeName_extern(_ self: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void #else -fileprivate func bjs_Greeter_changeName_extern(_ self: Int32, _ name: Int32) -> Void { +fileprivate func bjs_Greeter_changeName_extern(_ self: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_Greeter_changeName(_ self: Int32, _ name: Int32) -> Void { - return bjs_Greeter_changeName_extern(self, name) +@inline(never) fileprivate func bjs_Greeter_changeName(_ self: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { + return bjs_Greeter_changeName_extern(self, nameBytes, nameLength) } func _$Greeter_init(_ name: String) throws(JSException) -> JSObject { - let nameValue = name.bridgeJSLowerParameter() - let ret = bjs_Greeter_init(nameValue) + let ret = _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + bjs_Greeter_init(nameBytes, nameLength) + } if let error = _swift_js_take_exception() { throw error } @@ -119,8 +120,9 @@ func _$Greeter_age_get(_ self: JSObject) throws(JSException) -> Double { func _$Greeter_name_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_Greeter_name_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_Greeter_name_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -137,8 +139,9 @@ func _$Greeter_greet(_ self: JSObject) throws(JSException) -> String { func _$Greeter_changeName(_ self: JSObject, _ name: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let nameValue = name.bridgeJSLowerParameter() - bjs_Greeter_changeName(selfValue, nameValue) + _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + bjs_Greeter_changeName(selfValue, nameBytes, nameLength) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift index eafbd3253..bf1a551ec 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift @@ -348,14 +348,14 @@ fileprivate func _bjs_OptionalPropertyHolder_wrap_extern(_ pointer: UnsafeMutabl #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WithOptionalJSClass_init") -fileprivate func bjs_WithOptionalJSClass_init_extern(_ valueOrNullIsSome: Int32, _ valueOrNullValue: Int32, _ valueOrUndefinedIsSome: Int32, _ valueOrUndefinedValue: Int32) -> Int32 +fileprivate func bjs_WithOptionalJSClass_init_extern(_ valueOrNullIsSome: Int32, _ valueOrNullBytes: Int32, _ valueOrNullLength: Int32, _ valueOrUndefinedIsSome: Int32, _ valueOrUndefinedBytes: Int32, _ valueOrUndefinedLength: Int32) -> Int32 #else -fileprivate func bjs_WithOptionalJSClass_init_extern(_ valueOrNullIsSome: Int32, _ valueOrNullValue: Int32, _ valueOrUndefinedIsSome: Int32, _ valueOrUndefinedValue: Int32) -> Int32 { +fileprivate func bjs_WithOptionalJSClass_init_extern(_ valueOrNullIsSome: Int32, _ valueOrNullBytes: Int32, _ valueOrNullLength: Int32, _ valueOrUndefinedIsSome: Int32, _ valueOrUndefinedBytes: Int32, _ valueOrUndefinedLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WithOptionalJSClass_init(_ valueOrNullIsSome: Int32, _ valueOrNullValue: Int32, _ valueOrUndefinedIsSome: Int32, _ valueOrUndefinedValue: Int32) -> Int32 { - return bjs_WithOptionalJSClass_init_extern(valueOrNullIsSome, valueOrNullValue, valueOrUndefinedIsSome, valueOrUndefinedValue) +@inline(never) fileprivate func bjs_WithOptionalJSClass_init(_ valueOrNullIsSome: Int32, _ valueOrNullBytes: Int32, _ valueOrNullLength: Int32, _ valueOrUndefinedIsSome: Int32, _ valueOrUndefinedBytes: Int32, _ valueOrUndefinedLength: Int32) -> Int32 { + return bjs_WithOptionalJSClass_init_extern(valueOrNullIsSome, valueOrNullBytes, valueOrNullLength, valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedLength) } #if arch(wasm32) @@ -456,26 +456,26 @@ fileprivate func bjs_WithOptionalJSClass_intOrUndefined_get_extern(_ self: Int32 #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WithOptionalJSClass_stringOrNull_set") -fileprivate func bjs_WithOptionalJSClass_stringOrNull_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void +fileprivate func bjs_WithOptionalJSClass_stringOrNull_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_WithOptionalJSClass_stringOrNull_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { +fileprivate func bjs_WithOptionalJSClass_stringOrNull_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WithOptionalJSClass_stringOrNull_set(_ self: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { - return bjs_WithOptionalJSClass_stringOrNull_set_extern(self, newValueIsSome, newValueValue) +@inline(never) fileprivate func bjs_WithOptionalJSClass_stringOrNull_set(_ self: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_WithOptionalJSClass_stringOrNull_set_extern(self, newValueIsSome, newValueBytes, newValueLength) } #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WithOptionalJSClass_stringOrUndefined_set") -fileprivate func bjs_WithOptionalJSClass_stringOrUndefined_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void +fileprivate func bjs_WithOptionalJSClass_stringOrUndefined_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_WithOptionalJSClass_stringOrUndefined_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { +fileprivate func bjs_WithOptionalJSClass_stringOrUndefined_set_extern(_ self: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WithOptionalJSClass_stringOrUndefined_set(_ self: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { - return bjs_WithOptionalJSClass_stringOrUndefined_set_extern(self, newValueIsSome, newValueValue) +@inline(never) fileprivate func bjs_WithOptionalJSClass_stringOrUndefined_set(_ self: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_WithOptionalJSClass_stringOrUndefined_set_extern(self, newValueIsSome, newValueBytes, newValueLength) } #if arch(wasm32) @@ -552,26 +552,26 @@ fileprivate func bjs_WithOptionalJSClass_intOrUndefined_set_extern(_ self: Int32 #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WithOptionalJSClass_roundTripStringOrNull") -fileprivate func bjs_WithOptionalJSClass_roundTripStringOrNull_extern(_ self: Int32, _ valueIsSome: Int32, _ valueValue: Int32) -> Void +fileprivate func bjs_WithOptionalJSClass_roundTripStringOrNull_extern(_ self: Int32, _ valueIsSome: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void #else -fileprivate func bjs_WithOptionalJSClass_roundTripStringOrNull_extern(_ self: Int32, _ valueIsSome: Int32, _ valueValue: Int32) -> Void { +fileprivate func bjs_WithOptionalJSClass_roundTripStringOrNull_extern(_ self: Int32, _ valueIsSome: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WithOptionalJSClass_roundTripStringOrNull(_ self: Int32, _ valueIsSome: Int32, _ valueValue: Int32) -> Void { - return bjs_WithOptionalJSClass_roundTripStringOrNull_extern(self, valueIsSome, valueValue) +@inline(never) fileprivate func bjs_WithOptionalJSClass_roundTripStringOrNull(_ self: Int32, _ valueIsSome: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void { + return bjs_WithOptionalJSClass_roundTripStringOrNull_extern(self, valueIsSome, valueBytes, valueLength) } #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_WithOptionalJSClass_roundTripStringOrUndefined") -fileprivate func bjs_WithOptionalJSClass_roundTripStringOrUndefined_extern(_ self: Int32, _ valueIsSome: Int32, _ valueValue: Int32) -> Void +fileprivate func bjs_WithOptionalJSClass_roundTripStringOrUndefined_extern(_ self: Int32, _ valueIsSome: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void #else -fileprivate func bjs_WithOptionalJSClass_roundTripStringOrUndefined_extern(_ self: Int32, _ valueIsSome: Int32, _ valueValue: Int32) -> Void { +fileprivate func bjs_WithOptionalJSClass_roundTripStringOrUndefined_extern(_ self: Int32, _ valueIsSome: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_WithOptionalJSClass_roundTripStringOrUndefined(_ self: Int32, _ valueIsSome: Int32, _ valueValue: Int32) -> Void { - return bjs_WithOptionalJSClass_roundTripStringOrUndefined_extern(self, valueIsSome, valueValue) +@inline(never) fileprivate func bjs_WithOptionalJSClass_roundTripStringOrUndefined(_ self: Int32, _ valueIsSome: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void { + return bjs_WithOptionalJSClass_roundTripStringOrUndefined_extern(self, valueIsSome, valueBytes, valueLength) } #if arch(wasm32) @@ -647,9 +647,11 @@ fileprivate func bjs_WithOptionalJSClass_roundTripIntOrUndefined_extern(_ self: } func _$WithOptionalJSClass_init(_ valueOrNull: Optional, _ valueOrUndefined: JSUndefinedOr) throws(JSException) -> JSObject { - let (valueOrNullIsSome, valueOrNullValue) = valueOrNull.bridgeJSLowerParameter() - let (valueOrUndefinedIsSome, valueOrUndefinedValue) = valueOrUndefined.bridgeJSLowerParameter() - let ret = bjs_WithOptionalJSClass_init(valueOrNullIsSome, valueOrNullValue, valueOrUndefinedIsSome, valueOrUndefinedValue) + let ret = _swift_js_with_optional_borrowed_utf8(valueOrNull.asOptional) { valueOrNullIsSome, valueOrNullBytes, valueOrNullLength in + _swift_js_with_optional_borrowed_utf8(valueOrUndefined.asOptional) { valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedLength in + bjs_WithOptionalJSClass_init(valueOrNullIsSome, valueOrNullBytes, valueOrNullLength, valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedLength) + } + } if let error = _swift_js_take_exception() { throw error } @@ -730,8 +732,9 @@ func _$WithOptionalJSClass_intOrUndefined_get(_ self: JSObject) throws(JSExcepti func _$WithOptionalJSClass_stringOrNull_set(_ self: JSObject, _ newValue: Optional) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let (newValueIsSome, newValueValue) = newValue.bridgeJSLowerParameter() - bjs_WithOptionalJSClass_stringOrNull_set(selfValue, newValueIsSome, newValueValue) + _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in + bjs_WithOptionalJSClass_stringOrNull_set(selfValue, newValueIsSome, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -739,8 +742,9 @@ func _$WithOptionalJSClass_stringOrNull_set(_ self: JSObject, _ newValue: Option func _$WithOptionalJSClass_stringOrUndefined_set(_ self: JSObject, _ newValue: JSUndefinedOr) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let (newValueIsSome, newValueValue) = newValue.bridgeJSLowerParameter() - bjs_WithOptionalJSClass_stringOrUndefined_set(selfValue, newValueIsSome, newValueValue) + _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in + bjs_WithOptionalJSClass_stringOrUndefined_set(selfValue, newValueIsSome, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -802,8 +806,9 @@ func _$WithOptionalJSClass_intOrUndefined_set(_ self: JSObject, _ newValue: JSUn func _$WithOptionalJSClass_roundTripStringOrNull(_ self: JSObject, _ value: Optional) throws(JSException) -> Optional { let selfValue = self.bridgeJSLowerParameter() - let (valueIsSome, valueValue) = value.bridgeJSLowerParameter() - bjs_WithOptionalJSClass_roundTripStringOrNull(selfValue, valueIsSome, valueValue) + _swift_js_with_optional_borrowed_utf8(value.asOptional) { valueIsSome, valueBytes, valueLength in + bjs_WithOptionalJSClass_roundTripStringOrNull(selfValue, valueIsSome, valueBytes, valueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -812,8 +817,9 @@ func _$WithOptionalJSClass_roundTripStringOrNull(_ self: JSObject, _ value: Opti func _$WithOptionalJSClass_roundTripStringOrUndefined(_ self: JSObject, _ value: JSUndefinedOr) throws(JSException) -> JSUndefinedOr { let selfValue = self.bridgeJSLowerParameter() - let (valueIsSome, valueValue) = value.bridgeJSLowerParameter() - bjs_WithOptionalJSClass_roundTripStringOrUndefined(selfValue, valueIsSome, valueValue) + _swift_js_with_optional_borrowed_utf8(value.asOptional) { valueIsSome, valueBytes, valueLength in + bjs_WithOptionalJSClass_roundTripStringOrUndefined(selfValue, valueIsSome, valueBytes, valueLength) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift index 785cb997a..ae71ee634 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift @@ -8,8 +8,7 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto func onValueChanged(_ value: String) -> Void { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let valueValue = value.bridgeJSLowerParameter() - _extern_onValueChanged(jsObjectValue, valueValue) + _swift_js_with_borrowed_utf8(value) { valueBytes, valueLength in _extern_onValueChanged(jsObjectValue, valueBytes, valueLength) } } func onCountUpdated(count: Int) -> Bool { @@ -21,9 +20,7 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto func onLabelUpdated(_ prefix: String, _ suffix: String) -> Void { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let prefixValue = prefix.bridgeJSLowerParameter() - let suffixValue = suffix.bridgeJSLowerParameter() - _extern_onLabelUpdated(jsObjectValue, prefixValue, suffixValue) + _swift_js_with_borrowed_utf8(prefix) { prefixBytes, prefixLength in _swift_js_with_borrowed_utf8(suffix) { suffixBytes, suffixLength in _extern_onLabelUpdated(jsObjectValue, prefixBytes, prefixLength, suffixBytes, suffixLength) } } } func isCountEven() -> Bool { @@ -103,8 +100,7 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let (newValueIsSome, newValueValue) = newValue.bridgeJSLowerParameter() - bjs_MyViewControllerDelegate_optionalName_set(jsObjectValue, newValueIsSome, newValueValue) + _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in bjs_MyViewControllerDelegate_optionalName_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } } } @@ -116,8 +112,7 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let (newValueIsSome, newValueValue) = newValue.bridgeJSLowerParameter() - bjs_MyViewControllerDelegate_optionalRawEnum_set(jsObjectValue, newValueIsSome, newValueValue) + _swift_js_with_optional_borrowed_utf8(newValue.asOptional?.rawValue) { newValueIsSome, newValueBytes, newValueLength in bjs_MyViewControllerDelegate_optionalRawEnum_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } } } @@ -129,8 +124,7 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_MyViewControllerDelegate_rawStringEnum_set(jsObjectValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue.rawValue) { newValueBytes, newValueLength in bjs_MyViewControllerDelegate_rawStringEnum_set(jsObjectValue, newValueBytes, newValueLength) } } } @@ -231,14 +225,14 @@ fileprivate func _extern_onSomethingHappened_extern(_ jsObject: Int32) -> Void { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_onValueChanged") -fileprivate func _extern_onValueChanged_extern(_ jsObject: Int32, _ value: Int32) -> Void +fileprivate func _extern_onValueChanged_extern(_ jsObject: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void #else -fileprivate func _extern_onValueChanged_extern(_ jsObject: Int32, _ value: Int32) -> Void { +fileprivate func _extern_onValueChanged_extern(_ jsObject: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func _extern_onValueChanged(_ jsObject: Int32, _ value: Int32) -> Void { - return _extern_onValueChanged_extern(jsObject, value) +@inline(never) fileprivate func _extern_onValueChanged(_ jsObject: Int32, _ valueBytes: Int32, _ valueLength: Int32) -> Void { + return _extern_onValueChanged_extern(jsObject, valueBytes, valueLength) } #if arch(wasm32) @@ -255,14 +249,14 @@ fileprivate func _extern_onCountUpdated_extern(_ jsObject: Int32, _ count: Int32 #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_onLabelUpdated") -fileprivate func _extern_onLabelUpdated_extern(_ jsObject: Int32, _ prefix: Int32, _ suffix: Int32) -> Void +fileprivate func _extern_onLabelUpdated_extern(_ jsObject: Int32, _ prefixBytes: Int32, _ prefixLength: Int32, _ suffixBytes: Int32, _ suffixLength: Int32) -> Void #else -fileprivate func _extern_onLabelUpdated_extern(_ jsObject: Int32, _ prefix: Int32, _ suffix: Int32) -> Void { +fileprivate func _extern_onLabelUpdated_extern(_ jsObject: Int32, _ prefixBytes: Int32, _ prefixLength: Int32, _ suffixBytes: Int32, _ suffixLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func _extern_onLabelUpdated(_ jsObject: Int32, _ prefix: Int32, _ suffix: Int32) -> Void { - return _extern_onLabelUpdated_extern(jsObject, prefix, suffix) +@inline(never) fileprivate func _extern_onLabelUpdated(_ jsObject: Int32, _ prefixBytes: Int32, _ prefixLength: Int32, _ suffixBytes: Int32, _ suffixLength: Int32) -> Void { + return _extern_onLabelUpdated_extern(jsObject, prefixBytes, prefixLength, suffixBytes, suffixLength) } #if arch(wasm32) @@ -411,14 +405,14 @@ fileprivate func bjs_MyViewControllerDelegate_optionalName_get_extern(_ jsObject #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalName_set") -fileprivate func bjs_MyViewControllerDelegate_optionalName_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void +fileprivate func bjs_MyViewControllerDelegate_optionalName_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_MyViewControllerDelegate_optionalName_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { +fileprivate func bjs_MyViewControllerDelegate_optionalName_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_MyViewControllerDelegate_optionalName_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { - return bjs_MyViewControllerDelegate_optionalName_set_extern(jsObject, newValueIsSome, newValueValue) +@inline(never) fileprivate func bjs_MyViewControllerDelegate_optionalName_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_MyViewControllerDelegate_optionalName_set_extern(jsObject, newValueIsSome, newValueBytes, newValueLength) } #if arch(wasm32) @@ -435,14 +429,14 @@ fileprivate func bjs_MyViewControllerDelegate_optionalRawEnum_get_extern(_ jsObj #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_optionalRawEnum_set") -fileprivate func bjs_MyViewControllerDelegate_optionalRawEnum_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void +fileprivate func bjs_MyViewControllerDelegate_optionalRawEnum_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_MyViewControllerDelegate_optionalRawEnum_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { +fileprivate func bjs_MyViewControllerDelegate_optionalRawEnum_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_MyViewControllerDelegate_optionalRawEnum_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { - return bjs_MyViewControllerDelegate_optionalRawEnum_set_extern(jsObject, newValueIsSome, newValueValue) +@inline(never) fileprivate func bjs_MyViewControllerDelegate_optionalRawEnum_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_MyViewControllerDelegate_optionalRawEnum_set_extern(jsObject, newValueIsSome, newValueBytes, newValueLength) } #if arch(wasm32) @@ -459,14 +453,14 @@ fileprivate func bjs_MyViewControllerDelegate_rawStringEnum_get_extern(_ jsObjec #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_MyViewControllerDelegate_rawStringEnum_set") -fileprivate func bjs_MyViewControllerDelegate_rawStringEnum_set_extern(_ jsObject: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_MyViewControllerDelegate_rawStringEnum_set_extern(_ jsObject: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_MyViewControllerDelegate_rawStringEnum_set_extern(_ jsObject: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_MyViewControllerDelegate_rawStringEnum_set_extern(_ jsObject: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_MyViewControllerDelegate_rawStringEnum_set(_ jsObject: Int32, _ newValue: Int32) -> Void { - return bjs_MyViewControllerDelegate_rawStringEnum_set_extern(jsObject, newValue) +@inline(never) fileprivate func bjs_MyViewControllerDelegate_rawStringEnum_set(_ jsObject: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_MyViewControllerDelegate_rawStringEnum_set_extern(jsObject, newValueBytes, newValueLength) } #if arch(wasm32) diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift index 427c2b576..9b14b26b2 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift @@ -21,19 +21,20 @@ public func _bjs_roundtripString(_ aBytes: Int32, _ aLength: Int32) -> Void { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_checkString") -fileprivate func bjs_checkString_extern(_ a: Int32) -> Void +fileprivate func bjs_checkString_extern(_ aBytes: Int32, _ aLength: Int32) -> Void #else -fileprivate func bjs_checkString_extern(_ a: Int32) -> Void { +fileprivate func bjs_checkString_extern(_ aBytes: Int32, _ aLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_checkString(_ a: Int32) -> Void { - return bjs_checkString_extern(a) +@inline(never) fileprivate func bjs_checkString(_ aBytes: Int32, _ aLength: Int32) -> Void { + return bjs_checkString_extern(aBytes, aLength) } func _$checkString(_ a: String) throws(JSException) -> Void { - let aValue = a.bridgeJSLowerParameter() - bjs_checkString(aValue) + _swift_js_with_borrowed_utf8(a) { aBytes, aLength in + bjs_checkString(aBytes, aLength) + } if let error = _swift_js_take_exception() { throw error } @@ -41,20 +42,21 @@ func _$checkString(_ a: String) throws(JSException) -> Void { #if arch(wasm32) @_extern(wasm, module: "TestModule", name: "bjs_checkStringWithLength") -fileprivate func bjs_checkStringWithLength_extern(_ a: Int32, _ b: Float64) -> Void +fileprivate func bjs_checkStringWithLength_extern(_ aBytes: Int32, _ aLength: Int32, _ b: Float64) -> Void #else -fileprivate func bjs_checkStringWithLength_extern(_ a: Int32, _ b: Float64) -> Void { +fileprivate func bjs_checkStringWithLength_extern(_ aBytes: Int32, _ aLength: Int32, _ b: Float64) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_checkStringWithLength(_ a: Int32, _ b: Float64) -> Void { - return bjs_checkStringWithLength_extern(a, b) +@inline(never) fileprivate func bjs_checkStringWithLength(_ aBytes: Int32, _ aLength: Int32, _ b: Float64) -> Void { + return bjs_checkStringWithLength_extern(aBytes, aLength, b) } func _$checkStringWithLength(_ a: String, _ b: Double) throws(JSException) -> Void { - let aValue = a.bridgeJSLowerParameter() let bValue = b.bridgeJSLowerParameter() - bjs_checkStringWithLength(aValue, bValue) + _swift_js_with_borrowed_utf8(a) { aBytes, aLength in + bjs_checkStringWithLength(aBytes, aLength, bValue) + } if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift index d4bdc4a58..93de693b6 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift @@ -63,14 +63,14 @@ public func _invoke_swift_closure_TestModule_10TestModule10HttpStatusO_10HttpSta #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO") -fileprivate func invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO_extern(_ callback: Int32, _ param0: Int32) -> Int32 +fileprivate func invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 #else -fileprivate func invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO_extern(_ callback: Int32, _ param0: Int32) -> Int32 { +fileprivate func invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO(_ callback: Int32, _ param0: Int32) -> Int32 { - return invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO_extern(callback, param0) +@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { + return invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO_extern(callback, param0Bytes, param0Length) } #if arch(wasm32) @@ -91,8 +91,9 @@ private enum _BJS_Closure_10TestModule5ThemeO_5ThemeO { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let param0Value = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO(callbackValue, param0Value) + let ret = _swift_js_with_borrowed_utf8(param0.rawValue) { param0Bytes, param0Length in + invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO(callbackValue, param0Bytes, param0Length) + } return Theme.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -315,14 +316,14 @@ public func _invoke_swift_closure_TestModule_10TestModule9DirectionO_9DirectionO #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_TestModule_10TestModuleSS_SS") -fileprivate func invoke_js_callback_TestModule_10TestModuleSS_SS_extern(_ callback: Int32, _ param0: Int32) -> Int32 +fileprivate func invoke_js_callback_TestModule_10TestModuleSS_SS_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 #else -fileprivate func invoke_js_callback_TestModule_10TestModuleSS_SS_extern(_ callback: Int32, _ param0: Int32) -> Int32 { +fileprivate func invoke_js_callback_TestModule_10TestModuleSS_SS_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSS_SS(_ callback: Int32, _ param0: Int32) -> Int32 { - return invoke_js_callback_TestModule_10TestModuleSS_SS_extern(callback, param0) +@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSS_SS(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { + return invoke_js_callback_TestModule_10TestModuleSS_SS_extern(callback, param0Bytes, param0Length) } #if arch(wasm32) @@ -343,8 +344,9 @@ private enum _BJS_Closure_10TestModuleSS_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let param0Value = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_TestModule_10TestModuleSS_SS(callbackValue, param0Value) + let ret = _swift_js_with_borrowed_utf8(param0) { param0Bytes, param0Length in + invoke_js_callback_TestModule_10TestModuleSS_SS(callbackValue, param0Bytes, param0Length) + } return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -693,14 +695,14 @@ public func _invoke_swift_closure_TestModule_10TestModuleSq10HttpStatusO_Sq10Htt #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO") -fileprivate func invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Void +fileprivate func invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Void #else -fileprivate func invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Void { +fileprivate func invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Void { - return invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO_extern(callback, param0IsSome, param0Value) +@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Void { + return invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO_extern(callback, param0IsSome, param0Bytes, param0Length) } #if arch(wasm32) @@ -721,8 +723,9 @@ private enum _BJS_Closure_10TestModuleSq5ThemeO_Sq5ThemeO { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let (param0IsSome, param0Value) = param0.bridgeJSLowerParameter() - invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO(callbackValue, param0IsSome, param0Value) + _swift_js_with_optional_borrowed_utf8(param0.asOptional?.rawValue) { param0IsSome, param0Bytes, param0Length in + invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO(callbackValue, param0IsSome, param0Bytes, param0Length) + } return Optional.bridgeJSLiftReturnFromSideChannel() #else fatalError("Only available on WebAssembly") @@ -945,14 +948,14 @@ public func _invoke_swift_closure_TestModule_10TestModuleSq9DirectionO_Sq9Direct #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_TestModule_10TestModuleSqSS_SqSS") -fileprivate func invoke_js_callback_TestModule_10TestModuleSqSS_SqSS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Void +fileprivate func invoke_js_callback_TestModule_10TestModuleSqSS_SqSS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Void #else -fileprivate func invoke_js_callback_TestModule_10TestModuleSqSS_SqSS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Void { +fileprivate func invoke_js_callback_TestModule_10TestModuleSqSS_SqSS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSqSS_SqSS(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Void { - return invoke_js_callback_TestModule_10TestModuleSqSS_SqSS_extern(callback, param0IsSome, param0Value) +@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSqSS_SqSS(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Void { + return invoke_js_callback_TestModule_10TestModuleSqSS_SqSS_extern(callback, param0IsSome, param0Bytes, param0Length) } #if arch(wasm32) @@ -973,8 +976,9 @@ private enum _BJS_Closure_10TestModuleSqSS_SqSS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let (param0IsSome, param0Value) = param0.bridgeJSLowerParameter() - invoke_js_callback_TestModule_10TestModuleSqSS_SqSS(callbackValue, param0IsSome, param0Value) + _swift_js_with_optional_borrowed_utf8(param0.asOptional) { param0IsSome, param0Bytes, param0Length in + invoke_js_callback_TestModule_10TestModuleSqSS_SqSS(callbackValue, param0IsSome, param0Bytes, param0Length) + } return Optional.bridgeJSLiftReturnFromSideChannel() #else fatalError("Only available on WebAssembly") diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js index efd75f1fa..3a5377241 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js @@ -261,11 +261,12 @@ export async function createInstantiator(options, swift) { } bjs["swift_js_closure_unregister"] = function(funcRef) {} const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; - TestModule["bjs_takesFeatureFlag"] = function bjs_takesFeatureFlag(flag) { + TestModule["bjs_takesFeatureFlag"] = function bjs_takesFeatureFlag(flagBytes, flagCount) { try { - const flagObject = swift.memory.getObject(flag); - swift.memory.release(flag); - imports.takesFeatureFlag(flagObject); + const bytesView = new Uint8Array(memory.buffer, flagBytes, flagCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + imports.takesFeatureFlag(string); } catch (error) { setException(error); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js index 21613733e..7faf7a511 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js @@ -195,11 +195,12 @@ export async function createInstantiator(options, swift) { return 0 } } - TestModule["bjs_JSConsole_log"] = function bjs_JSConsole_log(self, message) { + TestModule["bjs_JSConsole_log"] = function bjs_JSConsole_log(self, messageBytes, messageCount) { try { - const messageObject = swift.memory.getObject(message); - swift.memory.release(message); - swift.memory.getObject(self).log(messageObject); + const bytesView = new Uint8Array(memory.buffer, messageBytes, messageCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).log(string); } catch (error) { setException(error); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js index 21296b88a..7f3ff6bc3 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js @@ -194,31 +194,34 @@ export async function createInstantiator(options, swift) { return 0 } } - TestModule["bjs_parseInt"] = function bjs_parseInt(string) { + TestModule["bjs_parseInt"] = function bjs_parseInt(stringBytes, stringCount) { try { - const stringObject = swift.memory.getObject(string); - swift.memory.release(string); - let ret = globalThis.parseInt(stringObject); + const bytesView = new Uint8Array(memory.buffer, stringBytes, stringCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + let ret = globalThis.parseInt(string); return ret; } catch (error) { setException(error); return 0 } } - TestModule["bjs_JSConsole_log"] = function bjs_JSConsole_log(self, message) { + TestModule["bjs_JSConsole_log"] = function bjs_JSConsole_log(self, messageBytes, messageCount) { try { - const messageObject = swift.memory.getObject(message); - swift.memory.release(message); - swift.memory.getObject(self).log(messageObject); + const bytesView = new Uint8Array(memory.buffer, messageBytes, messageCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).log(string); } catch (error) { setException(error); } } - TestModule["bjs_WebSocket_init"] = function bjs_WebSocket_init(url) { + TestModule["bjs_WebSocket_init"] = function bjs_WebSocket_init(urlBytes, urlCount) { try { - const urlObject = swift.memory.getObject(url); - swift.memory.release(url); - return swift.memory.retain(new globalThis.WebSocket(urlObject)); + const bytesView = new Uint8Array(memory.buffer, urlBytes, urlCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + return swift.memory.retain(new globalThis.WebSocket(string)); } catch (error) { setException(error); return 0 diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js index eabfe8c13..7002fdd72 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js @@ -276,11 +276,12 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_WeirdNaming_normalProperty_set"] = function bjs_WeirdNaming_normalProperty_set(self, newValue) { + TestModule["bjs_WeirdNaming_normalProperty_set"] = function bjs_WeirdNaming_normalProperty_set(self, newValueBytes, newValueCount) { try { - const newValueObject = swift.memory.getObject(newValue); - swift.memory.release(newValue); - swift.memory.getObject(self).normalProperty = newValueObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).normalProperty = string; } catch (error) { setException(error); } @@ -299,11 +300,12 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_WeirdNaming_property_with_spaces_set"] = function bjs_WeirdNaming_property_with_spaces_set(self, newValue) { + TestModule["bjs_WeirdNaming_property_with_spaces_set"] = function bjs_WeirdNaming_property_with_spaces_set(self, newValueBytes, newValueCount) { try { - const newValueObject = swift.memory.getObject(newValue); - swift.memory.release(newValue); - swift.memory.getObject(self)["property with spaces"] = newValueObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self)["property with spaces"] = string; } catch (error) { setException(error); } @@ -315,29 +317,32 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_WeirdNaming_constructor_set"] = function bjs_WeirdNaming_constructor_set(self, newValue) { + TestModule["bjs_WeirdNaming_constructor_set"] = function bjs_WeirdNaming_constructor_set(self, newValueBytes, newValueCount) { try { - const newValueObject = swift.memory.getObject(newValue); - swift.memory.release(newValue); - swift.memory.getObject(self).constructor = newValueObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).constructor = string; } catch (error) { setException(error); } } - TestModule["bjs_WeirdNaming_for_set"] = function bjs_WeirdNaming_for_set(self, newValue) { + TestModule["bjs_WeirdNaming_for_set"] = function bjs_WeirdNaming_for_set(self, newValueBytes, newValueCount) { try { - const newValueObject = swift.memory.getObject(newValue); - swift.memory.release(newValue); - swift.memory.getObject(self).for = newValueObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).for = string; } catch (error) { setException(error); } } - TestModule["bjs_WeirdNaming_any_set"] = function bjs_WeirdNaming_any_set(self, newValue) { + TestModule["bjs_WeirdNaming_any_set"] = function bjs_WeirdNaming_any_set(self, newValueBytes, newValueCount) { try { - const newValueObject = swift.memory.getObject(newValue); - swift.memory.release(newValue); - swift.memory.getObject(self).Any = newValueObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).Any = string; } catch (error) { setException(error); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js index d84c77d45..ef2b8f1f2 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js @@ -195,11 +195,12 @@ export async function createInstantiator(options, swift) { return 0 } } - TestModule["bjs_Greeter_init"] = function bjs_Greeter_init(name) { + TestModule["bjs_Greeter_init"] = function bjs_Greeter_init(nameBytes, nameCount) { try { - const nameObject = swift.memory.getObject(name); - swift.memory.release(name); - return swift.memory.retain(new imports.Greeter(nameObject)); + const bytesView = new Uint8Array(memory.buffer, nameBytes, nameCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + return swift.memory.retain(new imports.Greeter(string)); } catch (error) { setException(error); return 0 @@ -223,11 +224,12 @@ export async function createInstantiator(options, swift) { return 0 } } - TestModule["bjs_Greeter_name_set"] = function bjs_Greeter_name_set(self, newValue) { + TestModule["bjs_Greeter_name_set"] = function bjs_Greeter_name_set(self, newValueBytes, newValueCount) { try { - const newValueObject = swift.memory.getObject(newValue); - swift.memory.release(newValue); - swift.memory.getObject(self).name = newValueObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).name = string; } catch (error) { setException(error); } @@ -241,11 +243,12 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_Greeter_changeName"] = function bjs_Greeter_changeName(self, name) { + TestModule["bjs_Greeter_changeName"] = function bjs_Greeter_changeName(self, nameBytes, nameCount) { try { - const nameObject = swift.memory.getObject(name); - swift.memory.release(name); - swift.memory.getObject(self).changeName(nameObject); + const bytesView = new Uint8Array(memory.buffer, nameBytes, nameCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).changeName(string); } catch (error) { setException(error); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js index 941d66ea9..d6d6e68c3 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js @@ -198,21 +198,23 @@ export async function createInstantiator(options, swift) { return swift.memory.retain(obj); }; const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; - TestModule["bjs_WithOptionalJSClass_init"] = function bjs_WithOptionalJSClass_init(valueOrNullIsSome, valueOrNullObjectId, valueOrUndefinedIsSome, valueOrUndefinedObjectId) { + TestModule["bjs_WithOptionalJSClass_init"] = function bjs_WithOptionalJSClass_init(valueOrNullIsSome, valueOrNullBytes, valueOrNullCount, valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedCount) { try { let optResult; if (valueOrNullIsSome) { - const valueOrNullObjectIdObject = swift.memory.getObject(valueOrNullObjectId); - swift.memory.release(valueOrNullObjectId); - optResult = valueOrNullObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, valueOrNullBytes, valueOrNullCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = null; } let optResult1; if (valueOrUndefinedIsSome) { - const valueOrUndefinedObjectIdObject = swift.memory.getObject(valueOrUndefinedObjectId); - swift.memory.release(valueOrUndefinedObjectId); - optResult1 = valueOrUndefinedObjectIdObject; + const bytesView1 = new Uint8Array(memory.buffer, valueOrUndefinedBytes, valueOrUndefinedCount); + const bytesToDecode1 = (typeof SharedArrayBuffer !== "undefined" && bytesView1.buffer instanceof SharedArrayBuffer) ? bytesView1.slice() : bytesView1; + const string1 = textDecoder.decode(bytesToDecode1); + optResult1 = string1; } else { optResult1 = undefined; } @@ -294,13 +296,14 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_WithOptionalJSClass_stringOrNull_set"] = function bjs_WithOptionalJSClass_stringOrNull_set(self, newValueIsSome, newValueObjectId) { + TestModule["bjs_WithOptionalJSClass_stringOrNull_set"] = function bjs_WithOptionalJSClass_stringOrNull_set(self, newValueIsSome, newValueBytes, newValueCount) { try { let optResult; if (newValueIsSome) { - const newValueObjectIdObject = swift.memory.getObject(newValueObjectId); - swift.memory.release(newValueObjectId); - optResult = newValueObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = null; } @@ -309,13 +312,14 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_WithOptionalJSClass_stringOrUndefined_set"] = function bjs_WithOptionalJSClass_stringOrUndefined_set(self, newValueIsSome, newValueObjectId) { + TestModule["bjs_WithOptionalJSClass_stringOrUndefined_set"] = function bjs_WithOptionalJSClass_stringOrUndefined_set(self, newValueIsSome, newValueBytes, newValueCount) { try { let optResult; if (newValueIsSome) { - const newValueObjectIdObject = swift.memory.getObject(newValueObjectId); - swift.memory.release(newValueObjectId); - optResult = newValueObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = undefined; } @@ -366,13 +370,14 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_WithOptionalJSClass_roundTripStringOrNull"] = function bjs_WithOptionalJSClass_roundTripStringOrNull(self, valueIsSome, valueObjectId) { + TestModule["bjs_WithOptionalJSClass_roundTripStringOrNull"] = function bjs_WithOptionalJSClass_roundTripStringOrNull(self, valueIsSome, valueBytes, valueCount) { try { let optResult; if (valueIsSome) { - const valueObjectIdObject = swift.memory.getObject(valueObjectId); - swift.memory.release(valueObjectId); - optResult = valueObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = null; } @@ -383,13 +388,14 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_WithOptionalJSClass_roundTripStringOrUndefined"] = function bjs_WithOptionalJSClass_roundTripStringOrUndefined(self, valueIsSome, valueObjectId) { + TestModule["bjs_WithOptionalJSClass_roundTripStringOrUndefined"] = function bjs_WithOptionalJSClass_roundTripStringOrUndefined(self, valueIsSome, valueBytes, valueCount) { try { let optResult; if (valueIsSome) { - const valueObjectIdObject = swift.memory.getObject(valueObjectId); - swift.memory.release(valueObjectId); - optResult = valueObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = undefined; } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js index 6e95d2ddf..105d15134 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js @@ -291,13 +291,14 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_MyViewControllerDelegate_optionalName_set"] = function bjs_MyViewControllerDelegate_optionalName_set(self, valueIsSome, valueObjectId) { + TestModule["bjs_MyViewControllerDelegate_optionalName_set"] = function bjs_MyViewControllerDelegate_optionalName_set(self, valueIsSome, valueBytes, valueCount) { try { let optResult; if (valueIsSome) { - const valueObjectIdObject = swift.memory.getObject(valueObjectId); - swift.memory.release(valueObjectId); - optResult = valueObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = null; } @@ -314,13 +315,14 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_MyViewControllerDelegate_optionalRawEnum_set"] = function bjs_MyViewControllerDelegate_optionalRawEnum_set(self, valueIsSome, valueObjectId) { + TestModule["bjs_MyViewControllerDelegate_optionalRawEnum_set"] = function bjs_MyViewControllerDelegate_optionalRawEnum_set(self, valueIsSome, valueBytes, valueCount) { try { let optResult; if (valueIsSome) { - const valueObjectIdObject = swift.memory.getObject(valueObjectId); - swift.memory.release(valueObjectId); - optResult = valueObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = null; } @@ -338,11 +340,12 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_MyViewControllerDelegate_rawStringEnum_set"] = function bjs_MyViewControllerDelegate_rawStringEnum_set(self, value) { + TestModule["bjs_MyViewControllerDelegate_rawStringEnum_set"] = function bjs_MyViewControllerDelegate_rawStringEnum_set(self, valueBytes, valueCount) { try { - const valueObject = swift.memory.getObject(value); - swift.memory.release(value); - swift.memory.getObject(self).rawStringEnum = valueObject; + const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).rawStringEnum = string; } catch (error) { setException(error); } @@ -462,11 +465,12 @@ export async function createInstantiator(options, swift) { setException(error); } } - TestModule["bjs_MyViewControllerDelegate_onValueChanged"] = function bjs_MyViewControllerDelegate_onValueChanged(self, value) { + TestModule["bjs_MyViewControllerDelegate_onValueChanged"] = function bjs_MyViewControllerDelegate_onValueChanged(self, valueBytes, valueCount) { try { - const valueObject = swift.memory.getObject(value); - swift.memory.release(value); - swift.memory.getObject(self).onValueChanged(valueObject); + const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + swift.memory.getObject(self).onValueChanged(string); } catch (error) { setException(error); } @@ -480,13 +484,15 @@ export async function createInstantiator(options, swift) { return 0 } } - TestModule["bjs_MyViewControllerDelegate_onLabelUpdated"] = function bjs_MyViewControllerDelegate_onLabelUpdated(self, prefix, suffix) { + TestModule["bjs_MyViewControllerDelegate_onLabelUpdated"] = function bjs_MyViewControllerDelegate_onLabelUpdated(self, prefixBytes, prefixCount, suffixBytes, suffixCount) { try { - const prefixObject = swift.memory.getObject(prefix); - swift.memory.release(prefix); - const suffixObject = swift.memory.getObject(suffix); - swift.memory.release(suffix); - swift.memory.getObject(self).onLabelUpdated(prefixObject, suffixObject); + const bytesView = new Uint8Array(memory.buffer, prefixBytes, prefixCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + const bytesView1 = new Uint8Array(memory.buffer, suffixBytes, suffixCount); + const bytesToDecode1 = (typeof SharedArrayBuffer !== "undefined" && bytesView1.buffer instanceof SharedArrayBuffer) ? bytesView1.slice() : bytesView1; + const string1 = textDecoder.decode(bytesToDecode1); + swift.memory.getObject(self).onLabelUpdated(string, string1); } catch (error) { setException(error); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js index 480f40f92..093694a68 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js @@ -186,20 +186,22 @@ export async function createInstantiator(options, swift) { } bjs["swift_js_closure_unregister"] = function(funcRef) {} const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; - TestModule["bjs_checkString"] = function bjs_checkString(a) { + TestModule["bjs_checkString"] = function bjs_checkString(aBytes, aCount) { try { - const aObject = swift.memory.getObject(a); - swift.memory.release(a); - imports.checkString(aObject); + const bytesView = new Uint8Array(memory.buffer, aBytes, aCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + imports.checkString(string); } catch (error) { setException(error); } } - TestModule["bjs_checkStringWithLength"] = function bjs_checkStringWithLength(a, b) { + TestModule["bjs_checkStringWithLength"] = function bjs_checkStringWithLength(aBytes, aCount, b) { try { - const aObject = swift.memory.getObject(a); - swift.memory.release(a); - imports.checkStringWithLength(aObject, b); + const bytesView = new Uint8Array(memory.buffer, aBytes, aCount); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + imports.checkStringWithLength(string, b); } catch (error) { setException(error); } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js index 023a2fab0..f7103d56d 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js @@ -327,12 +327,13 @@ export async function createInstantiator(options, swift) { }; return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModule10HttpStatusO_10HttpStatusO); } - bjs["invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO"] = function(callbackId, param0) { + bjs["invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO"] = function(callbackId, param0Bytes, param0Count) { try { const callback = swift.memory.getObject(callbackId); - const param0Object = swift.memory.getObject(param0); - swift.memory.release(param0); - let ret = callback(param0Object); + const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + let ret = callback(string); tmpRetBytes = textEncoder.encode(ret); return tmpRetBytes.length; } catch (error) { @@ -428,12 +429,13 @@ export async function createInstantiator(options, swift) { }; return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModule9DirectionO_9DirectionO); } - bjs["invoke_js_callback_TestModule_10TestModuleSS_SS"] = function(callbackId, param0) { + bjs["invoke_js_callback_TestModule_10TestModuleSS_SS"] = function(callbackId, param0Bytes, param0Count) { try { const callback = swift.memory.getObject(callbackId); - const param0Object = swift.memory.getObject(param0); - swift.memory.release(param0); - let ret = callback(param0Object); + const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + let ret = callback(string); tmpRetBytes = textEncoder.encode(ret); return tmpRetBytes.length; } catch (error) { @@ -575,14 +577,15 @@ export async function createInstantiator(options, swift) { }; return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModuleSq10HttpStatusO_Sq10HttpStatusO); } - bjs["invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO"] = function(callbackId, param0IsSome, param0ObjectId) { + bjs["invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO"] = function(callbackId, param0IsSome, param0Bytes, param0Count) { try { const callback = swift.memory.getObject(callbackId); let optResult; if (param0IsSome) { - const param0ObjectIdObject = swift.memory.getObject(param0ObjectId); - swift.memory.release(param0ObjectId); - optResult = param0ObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = null; } @@ -723,14 +726,15 @@ export async function createInstantiator(options, swift) { }; return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModuleSq9DirectionO_Sq9DirectionO); } - bjs["invoke_js_callback_TestModule_10TestModuleSqSS_SqSS"] = function(callbackId, param0IsSome, param0ObjectId) { + bjs["invoke_js_callback_TestModule_10TestModuleSqSS_SqSS"] = function(callbackId, param0IsSome, param0Bytes, param0Count) { try { const callback = swift.memory.getObject(callbackId); let optResult; if (param0IsSome) { - const param0ObjectIdObject = swift.memory.getObject(param0ObjectId); - swift.memory.release(param0ObjectId); - optResult = param0ObjectIdObject; + const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); + const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; + const string = textDecoder.decode(bytesToDecode); + optResult = string; } else { optResult = null; } diff --git a/Sources/JavaScriptKit/BridgeJSIntrinsics.swift b/Sources/JavaScriptKit/BridgeJSIntrinsics.swift index 87f83d8a9..56c2fede9 100644 --- a/Sources/JavaScriptKit/BridgeJSIntrinsics.swift +++ b/Sources/JavaScriptKit/BridgeJSIntrinsics.swift @@ -395,6 +395,38 @@ extension String: _BridgedSwiftStackType { } } +/// Runs `body` with a borrowed UTF-8 (address, count) tuple. +@_spi(BridgeJS) @_transparent +public func _swift_js_with_borrowed_utf8( + _ value: consuming String, + _ body: (Int32, Int32) -> R +) -> R { + return value.withUTF8 { utf8 in + let address = utf8.baseAddress.map { Int32(truncatingIfNeeded: UInt(bitPattern: $0)) } ?? 0 + return body(address, Int32(utf8.count)) + } +} + +/// Runs `body` with optional borrowed UTF-8 components. +/// +/// .none is encoded as `(0, 0, 0)`. +/// .some is encoded as `(1, address, count)`. +@_spi(BridgeJS) @_transparent +public func _swift_js_with_optional_borrowed_utf8( + _ value: consuming String?, + _ body: (Int32, Int32, Int32) -> R +) -> R { + switch consume value { + case .none: + return body(0, 0, 0) + case .some(var value): + return value.withUTF8 { utf8 in + let address = utf8.baseAddress.map { Int32(truncatingIfNeeded: UInt(bitPattern: $0)) } ?? 0 + return body(1, address, Int32(utf8.count)) + } + } +} + extension JSObject: _BridgedSwiftStackType { // JSObject is a non-final class, so we must explicitly specify the associated type // rather than relying on the default `Self` (which Swift requires for covariant returns). diff --git a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift index 07c065be1..354fe97e8 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift +++ b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift @@ -72,14 +72,14 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTests10H #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS") -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS_extern(_ callback: Int32, _ param0: Int32) -> Int32 +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 #else -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS_extern(_ callback: Int32, _ param0: Int32) -> Int32 { +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS(_ callback: Int32, _ param0: Int32) -> Int32 { - return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS_extern(callback, param0) +@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { + return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS_extern(callback, param0Bytes, param0Length) } #if arch(wasm32) @@ -100,8 +100,9 @@ private enum _BJS_Closure_20BridgeJSRuntimeTests5ThemeO_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let param0Value = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS(callbackValue, param0Value) + let ret = _swift_js_with_borrowed_utf8(param0.rawValue) { param0Bytes, param0Length in + invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS(callbackValue, param0Bytes, param0Length) + } return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -135,14 +136,14 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5Th #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb") -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb_extern(_ callback: Int32, _ param0: Int32) -> Int32 +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 #else -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb_extern(_ callback: Int32, _ param0: Int32) -> Int32 { +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb(_ callback: Int32, _ param0: Int32) -> Int32 { - return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb_extern(callback, param0) +@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { + return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb_extern(callback, param0Bytes, param0Length) } #if arch(wasm32) @@ -163,8 +164,9 @@ private enum _BJS_Closure_20BridgeJSRuntimeTests5ThemeO_Sb { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let param0Value = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb(callbackValue, param0Value) + let ret = _swift_js_with_borrowed_utf8(param0.rawValue) { param0Bytes, param0Length in + invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb(callbackValue, param0Bytes, param0Length) + } return Bool.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -513,14 +515,14 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTests9Di #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC") -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC_extern(_ callback: Int32, _ param0: Int32) -> UnsafeMutableRawPointer +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> UnsafeMutableRawPointer #else -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC_extern(_ callback: Int32, _ param0: Int32) -> UnsafeMutableRawPointer { +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> UnsafeMutableRawPointer { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC(_ callback: Int32, _ param0: Int32) -> UnsafeMutableRawPointer { - return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC_extern(callback, param0) +@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> UnsafeMutableRawPointer { + return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC_extern(callback, param0Bytes, param0Length) } #if arch(wasm32) @@ -541,8 +543,9 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSS_7GreeterC { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let param0Value = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC(callbackValue, param0Value) + let ret = _swift_js_with_borrowed_utf8(param0) { param0Bytes, param0Length in + invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC(callbackValue, param0Bytes, param0Length) + } return Greeter.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -576,14 +579,14 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_ #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS") -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS_extern(_ callback: Int32, _ param0: Int32) -> Int32 +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 #else -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS_extern(_ callback: Int32, _ param0: Int32) -> Int32 { +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS_extern(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS(_ callback: Int32, _ param0: Int32) -> Int32 { - return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS_extern(callback, param0) +@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS(_ callback: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { + return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS_extern(callback, param0Bytes, param0Length) } #if arch(wasm32) @@ -604,8 +607,9 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSS_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let param0Value = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS(callbackValue, param0Value) + let ret = _swift_js_with_borrowed_utf8(param0) { param0Bytes, param0Length in + invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS(callbackValue, param0Bytes, param0Length) + } return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -702,14 +706,14 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSd_ #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS") -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS_extern(_ callback: Int32, _ param0: Int32, _ param1: Int32, _ param2: Float64) -> Int32 +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS_extern(_ callback: Int32, _ param0: Int32, _ param1Bytes: Int32, _ param1Length: Int32, _ param2: Float64) -> Int32 #else -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS_extern(_ callback: Int32, _ param0: Int32, _ param1: Int32, _ param2: Float64) -> Int32 { +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS_extern(_ callback: Int32, _ param0: Int32, _ param1Bytes: Int32, _ param1Length: Int32, _ param2: Float64) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS(_ callback: Int32, _ param0: Int32, _ param1: Int32, _ param2: Float64) -> Int32 { - return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS_extern(callback, param0, param1, param2) +@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS(_ callback: Int32, _ param0: Int32, _ param1Bytes: Int32, _ param1Length: Int32, _ param2: Float64) -> Int32 { + return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS_extern(callback, param0, param1Bytes, param1Length, param2) } #if arch(wasm32) @@ -731,9 +735,10 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSiSSSd_SS { #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() let param0Value = param0.bridgeJSLowerParameter() - let param1Value = param1.bridgeJSLowerParameter() let param2Value = param2.bridgeJSLowerParameter() - let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS(callbackValue, param0Value, param1Value, param2Value) + let ret = _swift_js_with_borrowed_utf8(param1) { param1Bytes, param1Length in + invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS(callbackValue, param0Value, param1Bytes, param1Length, param2Value) + } return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -1020,14 +1025,14 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_ #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS") -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Int32 +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 #else -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Int32 { +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Int32 { - return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS_extern(callback, param0IsSome, param0Value) +@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { + return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS_extern(callback, param0IsSome, param0Bytes, param0Length) } #if arch(wasm32) @@ -1048,8 +1053,9 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSq5ThemeO_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let (param0IsSome, param0Value) = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS(callbackValue, param0IsSome, param0Value) + let ret = _swift_js_with_optional_borrowed_utf8(param0.asOptional?.rawValue) { param0IsSome, param0Bytes, param0Length in + invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS(callbackValue, param0IsSome, param0Bytes, param0Length) + } return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -1335,14 +1341,14 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq9 #if arch(wasm32) @_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS") -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Int32 +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 #else -fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Int32 { +fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS_extern(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Int32 { - return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS_extern(callback, param0IsSome, param0Value) +@inline(never) fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS(_ callback: Int32, _ param0IsSome: Int32, _ param0Bytes: Int32, _ param0Length: Int32) -> Int32 { + return invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS_extern(callback, param0IsSome, param0Bytes, param0Length) } #if arch(wasm32) @@ -1363,8 +1369,9 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSqSS_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let (param0IsSome, param0Value) = param0.bridgeJSLowerParameter() - let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS(callbackValue, param0IsSome, param0Value) + let ret = _swift_js_with_optional_borrowed_utf8(param0.asOptional) { param0IsSome, param0Bytes, param0Length in + invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS(callbackValue, param0IsSome, param0Bytes, param0Length) + } return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -1705,9 +1712,7 @@ struct AnyDataProcessor: DataProcessor, _BridgedSwiftProtocolWrapper { func setLabelElements(_ labelPrefix: String, _ labelSuffix: String) -> Void { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let labelPrefixValue = labelPrefix.bridgeJSLowerParameter() - let labelSuffixValue = labelSuffix.bridgeJSLowerParameter() - _extern_setLabelElements(jsObjectValue, labelPrefixValue, labelSuffixValue) + _swift_js_with_borrowed_utf8(labelPrefix) { labelPrefixBytes, labelPrefixLength in _swift_js_with_borrowed_utf8(labelSuffix) { labelSuffixBytes, labelSuffixLength in _extern_setLabelElements(jsObjectValue, labelPrefixBytes, labelPrefixLength, labelSuffixBytes, labelSuffixLength) } } } func getLabel() -> String { @@ -1789,8 +1794,7 @@ struct AnyDataProcessor: DataProcessor, _BridgedSwiftProtocolWrapper { } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let (newValueIsSome, newValueValue) = newValue.bridgeJSLowerParameter() - bjs_DataProcessor_optionalTag_set(jsObjectValue, newValueIsSome, newValueValue) + _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in bjs_DataProcessor_optionalTag_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } } } @@ -1828,8 +1832,7 @@ struct AnyDataProcessor: DataProcessor, _BridgedSwiftProtocolWrapper { } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - let (newValueIsSome, newValueValue) = newValue.bridgeJSLowerParameter() - bjs_DataProcessor_optionalTheme_set(jsObjectValue, newValueIsSome, newValueValue) + _swift_js_with_optional_borrowed_utf8(newValue.asOptional?.rawValue) { newValueIsSome, newValueBytes, newValueLength in bjs_DataProcessor_optionalTheme_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } } } @@ -1916,14 +1919,14 @@ fileprivate func _extern_getValue_extern(_ jsObject: Int32) -> Int32 { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_setLabelElements") -fileprivate func _extern_setLabelElements_extern(_ jsObject: Int32, _ labelPrefix: Int32, _ labelSuffix: Int32) -> Void +fileprivate func _extern_setLabelElements_extern(_ jsObject: Int32, _ labelPrefixBytes: Int32, _ labelPrefixLength: Int32, _ labelSuffixBytes: Int32, _ labelSuffixLength: Int32) -> Void #else -fileprivate func _extern_setLabelElements_extern(_ jsObject: Int32, _ labelPrefix: Int32, _ labelSuffix: Int32) -> Void { +fileprivate func _extern_setLabelElements_extern(_ jsObject: Int32, _ labelPrefixBytes: Int32, _ labelPrefixLength: Int32, _ labelSuffixBytes: Int32, _ labelSuffixLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func _extern_setLabelElements(_ jsObject: Int32, _ labelPrefix: Int32, _ labelSuffix: Int32) -> Void { - return _extern_setLabelElements_extern(jsObject, labelPrefix, labelSuffix) +@inline(never) fileprivate func _extern_setLabelElements(_ jsObject: Int32, _ labelPrefixBytes: Int32, _ labelPrefixLength: Int32, _ labelSuffixBytes: Int32, _ labelSuffixLength: Int32) -> Void { + return _extern_setLabelElements_extern(jsObject, labelPrefixBytes, labelPrefixLength, labelSuffixBytes, labelSuffixLength) } #if arch(wasm32) @@ -2072,14 +2075,14 @@ fileprivate func bjs_DataProcessor_optionalTag_get_extern(_ jsObject: Int32) -> #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalTag_set") -fileprivate func bjs_DataProcessor_optionalTag_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void +fileprivate func bjs_DataProcessor_optionalTag_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_DataProcessor_optionalTag_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { +fileprivate func bjs_DataProcessor_optionalTag_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_DataProcessor_optionalTag_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { - return bjs_DataProcessor_optionalTag_set_extern(jsObject, newValueIsSome, newValueValue) +@inline(never) fileprivate func bjs_DataProcessor_optionalTag_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_DataProcessor_optionalTag_set_extern(jsObject, newValueIsSome, newValueBytes, newValueLength) } #if arch(wasm32) @@ -2144,14 +2147,14 @@ fileprivate func bjs_DataProcessor_optionalTheme_get_extern(_ jsObject: Int32) - #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_DataProcessor_optionalTheme_set") -fileprivate func bjs_DataProcessor_optionalTheme_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void +fileprivate func bjs_DataProcessor_optionalTheme_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_DataProcessor_optionalTheme_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { +fileprivate func bjs_DataProcessor_optionalTheme_set_extern(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_DataProcessor_optionalTheme_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueValue: Int32) -> Void { - return bjs_DataProcessor_optionalTheme_set_extern(jsObject, newValueIsSome, newValueValue) +@inline(never) fileprivate func bjs_DataProcessor_optionalTheme_set(_ jsObject: Int32, _ newValueIsSome: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_DataProcessor_optionalTheme_set_extern(jsObject, newValueIsSome, newValueBytes, newValueLength) } #if arch(wasm32) @@ -9223,14 +9226,14 @@ fileprivate func _bjs_LeakCheck_wrap_extern(_ pointer: UnsafeMutableRawPointer) #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_ArrayElementObject_init") -fileprivate func bjs_ArrayElementObject_init_extern(_ id: Int32) -> Int32 +fileprivate func bjs_ArrayElementObject_init_extern(_ idBytes: Int32, _ idLength: Int32) -> Int32 #else -fileprivate func bjs_ArrayElementObject_init_extern(_ id: Int32) -> Int32 { +fileprivate func bjs_ArrayElementObject_init_extern(_ idBytes: Int32, _ idLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_ArrayElementObject_init(_ id: Int32) -> Int32 { - return bjs_ArrayElementObject_init_extern(id) +@inline(never) fileprivate func bjs_ArrayElementObject_init(_ idBytes: Int32, _ idLength: Int32) -> Int32 { + return bjs_ArrayElementObject_init_extern(idBytes, idLength) } #if arch(wasm32) @@ -9246,8 +9249,9 @@ fileprivate func bjs_ArrayElementObject_id_get_extern(_ self: Int32) -> Int32 { } func _$ArrayElementObject_init(_ id: String) throws(JSException) -> JSObject { - let idValue = id.bridgeJSLowerParameter() - let ret = bjs_ArrayElementObject_init(idValue) + let ret = _swift_js_with_borrowed_utf8(id) { idBytes, idLength in + bjs_ArrayElementObject_init(idBytes, idLength) + } if let error = _swift_js_take_exception() { throw error } @@ -9667,14 +9671,14 @@ fileprivate func bjs_ClosureSupportImports_jsApplyDouble_static_extern(_ value: #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_ClosureSupportImports_jsApplyString_static") -fileprivate func bjs_ClosureSupportImports_jsApplyString_static_extern(_ value: Int32, _ transform: Int32) -> Int32 +fileprivate func bjs_ClosureSupportImports_jsApplyString_static_extern(_ valueBytes: Int32, _ valueLength: Int32, _ transform: Int32) -> Int32 #else -fileprivate func bjs_ClosureSupportImports_jsApplyString_static_extern(_ value: Int32, _ transform: Int32) -> Int32 { +fileprivate func bjs_ClosureSupportImports_jsApplyString_static_extern(_ valueBytes: Int32, _ valueLength: Int32, _ transform: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_ClosureSupportImports_jsApplyString_static(_ value: Int32, _ transform: Int32) -> Int32 { - return bjs_ClosureSupportImports_jsApplyString_static_extern(value, transform) +@inline(never) fileprivate func bjs_ClosureSupportImports_jsApplyString_static(_ valueBytes: Int32, _ valueLength: Int32, _ transform: Int32) -> Int32 { + return bjs_ClosureSupportImports_jsApplyString_static_extern(valueBytes, valueLength, transform) } #if arch(wasm32) @@ -9715,14 +9719,14 @@ fileprivate func bjs_ClosureSupportImports_jsMakeDoubleToDouble_static_extern(_ #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_ClosureSupportImports_jsMakeStringToString_static") -fileprivate func bjs_ClosureSupportImports_jsMakeStringToString_static_extern(_ prefix: Int32) -> Int32 +fileprivate func bjs_ClosureSupportImports_jsMakeStringToString_static_extern(_ prefixBytes: Int32, _ prefixLength: Int32) -> Int32 #else -fileprivate func bjs_ClosureSupportImports_jsMakeStringToString_static_extern(_ prefix: Int32) -> Int32 { +fileprivate func bjs_ClosureSupportImports_jsMakeStringToString_static_extern(_ prefixBytes: Int32, _ prefixLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_ClosureSupportImports_jsMakeStringToString_static(_ prefix: Int32) -> Int32 { - return bjs_ClosureSupportImports_jsMakeStringToString_static_extern(prefix) +@inline(never) fileprivate func bjs_ClosureSupportImports_jsMakeStringToString_static(_ prefixBytes: Int32, _ prefixLength: Int32) -> Int32 { + return bjs_ClosureSupportImports_jsMakeStringToString_static_extern(prefixBytes, prefixLength) } #if arch(wasm32) @@ -9871,9 +9875,10 @@ func _$ClosureSupportImports_jsApplyDouble(_ value: Double, _ transform: JSTyped } func _$ClosureSupportImports_jsApplyString(_ value: String, _ transform: JSTypedClosure<(String) -> String>) throws(JSException) -> String { - let valueValue = value.bridgeJSLowerParameter() let transformFuncRef = transform.bridgeJSLowerParameter() - let ret = bjs_ClosureSupportImports_jsApplyString_static(valueValue, transformFuncRef) + let ret = _swift_js_with_borrowed_utf8(value) { valueBytes, valueLength in + bjs_ClosureSupportImports_jsApplyString_static(valueBytes, valueLength, transformFuncRef) + } if let error = _swift_js_take_exception() { throw error } @@ -9909,8 +9914,9 @@ func _$ClosureSupportImports_jsMakeDoubleToDouble(_ base: Double) throws(JSExcep } func _$ClosureSupportImports_jsMakeStringToString(_ prefix: String) throws(JSException) -> (String) -> String { - let prefixValue = prefix.bridgeJSLowerParameter() - let ret = bjs_ClosureSupportImports_jsMakeStringToString_static(prefixValue) + let ret = _swift_js_with_borrowed_utf8(prefix) { prefixBytes, prefixLength in + bjs_ClosureSupportImports_jsMakeStringToString_static(prefixBytes, prefixLength) + } if let error = _swift_js_take_exception() { throw error } @@ -10140,14 +10146,14 @@ func _$DictionarySupportImports_jsRoundTripDictionaryDoubleArray(_ values: [Stri #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Foo_init") -fileprivate func bjs_Foo_init_extern(_ value: Int32) -> Int32 +fileprivate func bjs_Foo_init_extern(_ valueBytes: Int32, _ valueLength: Int32) -> Int32 #else -fileprivate func bjs_Foo_init_extern(_ value: Int32) -> Int32 { +fileprivate func bjs_Foo_init_extern(_ valueBytes: Int32, _ valueLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_Foo_init(_ value: Int32) -> Int32 { - return bjs_Foo_init_extern(value) +@inline(never) fileprivate func bjs_Foo_init(_ valueBytes: Int32, _ valueLength: Int32) -> Int32 { + return bjs_Foo_init_extern(valueBytes, valueLength) } #if arch(wasm32) @@ -10163,8 +10169,9 @@ fileprivate func bjs_Foo_value_get_extern(_ self: Int32) -> Int32 { } func _$Foo_init(_ value: String) throws(JSException) -> JSObject { - let valueValue = value.bridgeJSLowerParameter() - let ret = bjs_Foo_init(valueValue) + let ret = _swift_js_with_borrowed_utf8(value) { valueBytes, valueLength in + bjs_Foo_init(valueBytes, valueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -10263,19 +10270,20 @@ func _$jsRoundTripBool(_ v: Bool) throws(JSException) -> Bool { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripString") -fileprivate func bjs_jsRoundTripString_extern(_ v: Int32) -> Int32 +fileprivate func bjs_jsRoundTripString_extern(_ vBytes: Int32, _ vLength: Int32) -> Int32 #else -fileprivate func bjs_jsRoundTripString_extern(_ v: Int32) -> Int32 { +fileprivate func bjs_jsRoundTripString_extern(_ vBytes: Int32, _ vLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_jsRoundTripString(_ v: Int32) -> Int32 { - return bjs_jsRoundTripString_extern(v) +@inline(never) fileprivate func bjs_jsRoundTripString(_ vBytes: Int32, _ vLength: Int32) -> Int32 { + return bjs_jsRoundTripString_extern(vBytes, vLength) } func _$jsRoundTripString(_ v: String) throws(JSException) -> String { - let vValue = v.bridgeJSLowerParameter() - let ret = bjs_jsRoundTripString(vValue) + let ret = _swift_js_with_borrowed_utf8(v) { vBytes, vLength in + bjs_jsRoundTripString(vBytes, vLength) + } if let error = _swift_js_take_exception() { throw error } @@ -10388,19 +10396,20 @@ func _$jsThrowOrString(_ shouldThrow: Bool) throws(JSException) -> String { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripFeatureFlag") -fileprivate func bjs_jsRoundTripFeatureFlag_extern(_ flag: Int32) -> Int32 +fileprivate func bjs_jsRoundTripFeatureFlag_extern(_ flagBytes: Int32, _ flagLength: Int32) -> Int32 #else -fileprivate func bjs_jsRoundTripFeatureFlag_extern(_ flag: Int32) -> Int32 { +fileprivate func bjs_jsRoundTripFeatureFlag_extern(_ flagBytes: Int32, _ flagLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_jsRoundTripFeatureFlag(_ flag: Int32) -> Int32 { - return bjs_jsRoundTripFeatureFlag_extern(flag) +@inline(never) fileprivate func bjs_jsRoundTripFeatureFlag(_ flagBytes: Int32, _ flagLength: Int32) -> Int32 { + return bjs_jsRoundTripFeatureFlag_extern(flagBytes, flagLength) } func _$jsRoundTripFeatureFlag(_ flag: FeatureFlag) throws(JSException) -> FeatureFlag { - let flagValue = flag.bridgeJSLowerParameter() - let ret = bjs_jsRoundTripFeatureFlag(flagValue) + let ret = _swift_js_with_borrowed_utf8(flag.rawValue) { flagBytes, flagLength in + bjs_jsRoundTripFeatureFlag(flagBytes, flagLength) + } if let error = _swift_js_take_exception() { throw error } @@ -10449,19 +10458,20 @@ func _$_jsWeirdFunction() throws(JSException) -> Double { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_parseInt") -fileprivate func bjs_parseInt_extern(_ string: Int32) -> Float64 +fileprivate func bjs_parseInt_extern(_ stringBytes: Int32, _ stringLength: Int32) -> Float64 #else -fileprivate func bjs_parseInt_extern(_ string: Int32) -> Float64 { +fileprivate func bjs_parseInt_extern(_ stringBytes: Int32, _ stringLength: Int32) -> Float64 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_parseInt(_ string: Int32) -> Float64 { - return bjs_parseInt_extern(string) +@inline(never) fileprivate func bjs_parseInt(_ stringBytes: Int32, _ stringLength: Int32) -> Float64 { + return bjs_parseInt_extern(stringBytes, stringLength) } func _$parseInt(_ string: String) throws(JSException) -> Double { - let stringValue = string.bridgeJSLowerParameter() - let ret = bjs_parseInt(stringValue) + let ret = _swift_js_with_borrowed_utf8(string) { stringBytes, stringLength in + bjs_parseInt(stringBytes, stringLength) + } if let error = _swift_js_take_exception() { throw error } @@ -10470,14 +10480,14 @@ func _$parseInt(_ string: String) throws(JSException) -> Double { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_JsGreeter_init") -fileprivate func bjs_JsGreeter_init_extern(_ name: Int32, _ prefix: Int32) -> Int32 +fileprivate func bjs_JsGreeter_init_extern(_ nameBytes: Int32, _ nameLength: Int32, _ prefixBytes: Int32, _ prefixLength: Int32) -> Int32 #else -fileprivate func bjs_JsGreeter_init_extern(_ name: Int32, _ prefix: Int32) -> Int32 { +fileprivate func bjs_JsGreeter_init_extern(_ nameBytes: Int32, _ nameLength: Int32, _ prefixBytes: Int32, _ prefixLength: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_JsGreeter_init(_ name: Int32, _ prefix: Int32) -> Int32 { - return bjs_JsGreeter_init_extern(name, prefix) +@inline(never) fileprivate func bjs_JsGreeter_init(_ nameBytes: Int32, _ nameLength: Int32, _ prefixBytes: Int32, _ prefixLength: Int32) -> Int32 { + return bjs_JsGreeter_init_extern(nameBytes, nameLength, prefixBytes, prefixLength) } #if arch(wasm32) @@ -10506,14 +10516,14 @@ fileprivate func bjs_JsGreeter_prefix_get_extern(_ self: Int32) -> Int32 { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_JsGreeter_name_set") -fileprivate func bjs_JsGreeter_name_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_JsGreeter_name_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_JsGreeter_name_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_JsGreeter_name_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_JsGreeter_name_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_JsGreeter_name_set_extern(self, newValue) +@inline(never) fileprivate func bjs_JsGreeter_name_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_JsGreeter_name_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @@ -10530,20 +10540,22 @@ fileprivate func bjs_JsGreeter_greet_extern(_ self: Int32) -> Int32 { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_JsGreeter_changeName") -fileprivate func bjs_JsGreeter_changeName_extern(_ self: Int32, _ name: Int32) -> Void +fileprivate func bjs_JsGreeter_changeName_extern(_ self: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void #else -fileprivate func bjs_JsGreeter_changeName_extern(_ self: Int32, _ name: Int32) -> Void { +fileprivate func bjs_JsGreeter_changeName_extern(_ self: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_JsGreeter_changeName(_ self: Int32, _ name: Int32) -> Void { - return bjs_JsGreeter_changeName_extern(self, name) +@inline(never) fileprivate func bjs_JsGreeter_changeName(_ self: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { + return bjs_JsGreeter_changeName_extern(self, nameBytes, nameLength) } func _$JsGreeter_init(_ name: String, _ prefix: String) throws(JSException) -> JSObject { - let nameValue = name.bridgeJSLowerParameter() - let prefixValue = prefix.bridgeJSLowerParameter() - let ret = bjs_JsGreeter_init(nameValue, prefixValue) + let ret = _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + _swift_js_with_borrowed_utf8(prefix) { prefixBytes, prefixLength in + bjs_JsGreeter_init(nameBytes, nameLength, prefixBytes, prefixLength) + } + } if let error = _swift_js_take_exception() { throw error } @@ -10570,8 +10582,9 @@ func _$JsGreeter_prefix_get(_ self: JSObject) throws(JSException) -> String { func _$JsGreeter_name_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_JsGreeter_name_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_JsGreeter_name_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -10588,8 +10601,9 @@ func _$JsGreeter_greet(_ self: JSObject) throws(JSException) -> String { func _$JsGreeter_changeName(_ self: JSObject, _ name: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let nameValue = name.bridgeJSLowerParameter() - bjs_JsGreeter_changeName(selfValue, nameValue) + _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + bjs_JsGreeter_changeName(selfValue, nameBytes, nameLength) + } if let error = _swift_js_take_exception() { throw error } @@ -10761,14 +10775,14 @@ func _$StaticBox_value(_ self: JSObject) throws(JSException) -> Double { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Animal_init") -fileprivate func bjs_Animal_init_extern(_ name: Int32, _ age: Float64, _ isCat: Int32) -> Int32 +fileprivate func bjs_Animal_init_extern(_ nameBytes: Int32, _ nameLength: Int32, _ age: Float64, _ isCat: Int32) -> Int32 #else -fileprivate func bjs_Animal_init_extern(_ name: Int32, _ age: Float64, _ isCat: Int32) -> Int32 { +fileprivate func bjs_Animal_init_extern(_ nameBytes: Int32, _ nameLength: Int32, _ age: Float64, _ isCat: Int32) -> Int32 { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_Animal_init(_ name: Int32, _ age: Float64, _ isCat: Int32) -> Int32 { - return bjs_Animal_init_extern(name, age, isCat) +@inline(never) fileprivate func bjs_Animal_init(_ nameBytes: Int32, _ nameLength: Int32, _ age: Float64, _ isCat: Int32) -> Int32 { + return bjs_Animal_init_extern(nameBytes, nameLength, age, isCat) } #if arch(wasm32) @@ -10809,14 +10823,14 @@ fileprivate func bjs_Animal_isCat_get_extern(_ self: Int32) -> Int32 { #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Animal_name_set") -fileprivate func bjs_Animal_name_set_extern(_ self: Int32, _ newValue: Int32) -> Void +fileprivate func bjs_Animal_name_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void #else -fileprivate func bjs_Animal_name_set_extern(_ self: Int32, _ newValue: Int32) -> Void { +fileprivate func bjs_Animal_name_set_extern(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_Animal_name_set(_ self: Int32, _ newValue: Int32) -> Void { - return bjs_Animal_name_set_extern(self, newValue) +@inline(never) fileprivate func bjs_Animal_name_set(_ self: Int32, _ newValueBytes: Int32, _ newValueLength: Int32) -> Void { + return bjs_Animal_name_set_extern(self, newValueBytes, newValueLength) } #if arch(wasm32) @@ -10868,10 +10882,11 @@ fileprivate func bjs_Animal_getIsCat_extern(_ self: Int32) -> Int32 { } func _$Animal_init(_ name: String, _ age: Double, _ isCat: Bool) throws(JSException) -> JSObject { - let nameValue = name.bridgeJSLowerParameter() let ageValue = age.bridgeJSLowerParameter() let isCatValue = isCat.bridgeJSLowerParameter() - let ret = bjs_Animal_init(nameValue, ageValue, isCatValue) + let ret = _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + bjs_Animal_init(nameBytes, nameLength, ageValue, isCatValue) + } if let error = _swift_js_take_exception() { throw error } @@ -10907,8 +10922,9 @@ func _$Animal_isCat_get(_ self: JSObject) throws(JSException) -> Bool { func _$Animal_name_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - let newValueValue = newValue.bridgeJSLowerParameter() - bjs_Animal_name_set(selfValue, newValueValue) + _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + bjs_Animal_name_set(selfValue, newValueBytes, newValueLength) + } if let error = _swift_js_take_exception() { throw error } @@ -11348,26 +11364,26 @@ fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalNumberUndefined_s #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static") -fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static_extern(_ nameIsSome: Int32, _ nameValue: Int32) -> Void +fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static_extern(_ nameIsSome: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void #else -fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static_extern(_ nameIsSome: Int32, _ nameValue: Int32) -> Void { +fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static_extern(_ nameIsSome: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(_ nameIsSome: Int32, _ nameValue: Int32) -> Void { - return bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static_extern(nameIsSome, nameValue) +@inline(never) fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(_ nameIsSome: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { + return bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static_extern(nameIsSome, nameBytes, nameLength) } #if arch(wasm32) @_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static") -fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static_extern(_ nameIsSome: Int32, _ nameValue: Int32) -> Void +fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static_extern(_ nameIsSome: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void #else -fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static_extern(_ nameIsSome: Int32, _ nameValue: Int32) -> Void { +fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static_extern(_ nameIsSome: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { fatalError("Only available on WebAssembly") } #endif -@inline(never) fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(_ nameIsSome: Int32, _ nameValue: Int32) -> Void { - return bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static_extern(nameIsSome, nameValue) +@inline(never) fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(_ nameIsSome: Int32, _ nameBytes: Int32, _ nameLength: Int32) -> Void { + return bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static_extern(nameIsSome, nameBytes, nameLength) } #if arch(wasm32) @@ -11449,8 +11465,9 @@ func _$OptionalSupportImports_jsRoundTripOptionalNumberUndefined(_ value: JSUnde } func _$OptionalSupportImports_jsRoundTripOptionalStringNull(_ name: Optional) throws(JSException) -> Optional { - let (nameIsSome, nameValue) = name.bridgeJSLowerParameter() - bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(nameIsSome, nameValue) + _swift_js_with_optional_borrowed_utf8(name.asOptional) { nameIsSome, nameBytes, nameLength in + bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(nameIsSome, nameBytes, nameLength) + } if let error = _swift_js_take_exception() { throw error } @@ -11458,8 +11475,9 @@ func _$OptionalSupportImports_jsRoundTripOptionalStringNull(_ name: Optional) throws(JSException) -> JSUndefinedOr { - let (nameIsSome, nameValue) = name.bridgeJSLowerParameter() - bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(nameIsSome, nameValue) + _swift_js_with_optional_borrowed_utf8(name.asOptional) { nameIsSome, nameBytes, nameLength in + bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(nameIsSome, nameBytes, nameLength) + } if let error = _swift_js_take_exception() { throw error } From e0a9b3b381a5966e550aa14a670887fc105f14e5 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2026 07:01:46 +0000 Subject: [PATCH 2/5] Generalize lowering parameters with borrowing scope --- Benchmarks/Sources/Generated/BridgeJS.swift | 4 +- .../PlayBridgeJS/Generated/BridgeJS.swift | 6 +- .../Sources/BridgeJSCore/ClosureCodegen.swift | 7 +- .../Sources/BridgeJSCore/ExportSwift.swift | 19 +- .../Sources/BridgeJSCore/ImportTS.swift | 215 +++++++++--------- .../BridgeJSCodegenTests/EnumRawType.swift | 2 +- .../BridgeJSCodegenTests/GlobalGetter.swift | 2 +- .../GlobalThisImports.swift | 14 +- .../InvalidPropertyNames.swift | 10 +- .../BridgeJSCodegenTests/JSClass.swift | 10 +- .../BridgeJSCodegenTests/Optionals.swift | 17 +- .../BridgeJSCodegenTests/Protocol.swift | 22 +- .../StringParameter.swift | 6 +- .../BridgeJSCodegenTests/SwiftClosure.swift | 16 +- .../JavaScriptKit/BridgeJSIntrinsics.swift | 61 ++--- .../Generated/BridgeJS.swift | 131 +++++++---- 16 files changed, 289 insertions(+), 253 deletions(-) diff --git a/Benchmarks/Sources/Generated/BridgeJS.swift b/Benchmarks/Sources/Generated/BridgeJS.swift index 59d4ab8de..7b7c6e690 100644 --- a/Benchmarks/Sources/Generated/BridgeJS.swift +++ b/Benchmarks/Sources/Generated/BridgeJS.swift @@ -1761,8 +1761,8 @@ fileprivate func bjs_benchmarkRunner_extern(_ nameBytes: Int32, _ nameLength: In } func _$benchmarkRunner(_ name: String, _ body: JSObject) throws(JSException) -> Void { - let bodyValue = body.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + name.bridgeJSWithLoweredParameter { (nameBytes, nameLength) in + let bodyValue = body.bridgeJSLowerParameter() bjs_benchmarkRunner(nameBytes, nameLength, bodyValue) } if let error = _swift_js_take_exception() { diff --git a/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift b/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift index d4dd8fff0..f1baf3aa1 100644 --- a/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift +++ b/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift @@ -261,9 +261,11 @@ fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ tsBytes: Int32, _ func _$TS2Swift_convert(_ self: JSObject, _ ts: String) throws(JSException) -> String { let selfValue = self.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(ts) { tsBytes, tsLength in - bjs_TS2Swift_convert(selfValue, tsBytes, tsLength) + let ret0 = ts.bridgeJSWithLoweredParameter { (tsBytes, tsLength) in + let ret = bjs_TS2Swift_convert(selfValue, tsBytes, tsLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift index d974fc16d..e5648ccf7 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ClosureCodegen.swift @@ -26,9 +26,10 @@ public struct ClosureCodegen { let externName = "invoke_js_callback_\(signature.moduleName)_\(mangledName)" // Use CallJSEmission to generate the callback invocation - let builder = ImportTS.CallJSEmission( + let builder = try ImportTS.CallJSEmission( moduleName: "bjs", abiName: externName, + returnType: signature.returnType, context: .exportSwift ) @@ -41,8 +42,8 @@ public struct ClosureCodegen { } // Generate the call and return value lifting - try builder.call(returnType: signature.returnType) - try builder.liftReturnValue(returnType: signature.returnType) + try builder.call() + try builder.liftReturnValue() // Generate extern declaration using CallJSEmission let externDecl = builder.renderImportDecl() diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift index 7311b24be..9b238b26c 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift @@ -1166,17 +1166,18 @@ struct ProtocolCodegen { var externDecls: [DeclSyntax] = [] for method in proto.methods { - let builder = ImportTS.CallJSEmission( + let builder = try ImportTS.CallJSEmission( moduleName: moduleName, abiName: "_extern_\(method.name)", + returnType: method.returnType, context: .exportSwift ) try builder.lowerParameter(param: Parameter(label: nil, name: "jsObject", type: .jsObject(nil))) for param in method.parameters { try builder.lowerParameter(param: param) } - try builder.call(returnType: method.returnType) - try builder.liftReturnValue(returnType: method.returnType) + try builder.call() + try builder.liftReturnValue() // Build function signature using SwiftSignatureBuilder let signature = SwiftSignatureBuilder.buildFunctionSignature( @@ -1264,14 +1265,15 @@ struct ProtocolCodegen { className: protocolName ) - let getterBuilder = ImportTS.CallJSEmission( + let getterBuilder = try ImportTS.CallJSEmission( moduleName: moduleName, abiName: getterAbiName, + returnType: property.type, context: .exportSwift ) try getterBuilder.lowerParameter(param: Parameter(label: nil, name: "jsObject", type: .jsObject(nil))) - try getterBuilder.call(returnType: property.type) - try getterBuilder.liftReturnValue(returnType: property.type) + try getterBuilder.call() + try getterBuilder.liftReturnValue() // Build getter extern declaration using helper function let getterExternDeclPrinter = CodeFragmentPrinter() @@ -1296,14 +1298,15 @@ struct ProtocolCodegen { if property.isReadonly { return } - let setterBuilder = ImportTS.CallJSEmission( + let setterBuilder = try ImportTS.CallJSEmission( moduleName: moduleName, abiName: setterAbiName, + returnType: .void, context: .exportSwift ) try setterBuilder.lowerParameter(param: Parameter(label: nil, name: "jsObject", type: .jsObject(nil))) try setterBuilder.lowerParameter(param: Parameter(label: nil, name: "newValue", type: property.type)) - try setterBuilder.call(returnType: .void) + try setterBuilder.call() // Build setter extern declaration using helper function let setterExternDeclPrinter = CodeFragmentPrinter() diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift index 91b68d2ca..a432e1ac0 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift @@ -66,9 +66,13 @@ public struct ImportTS { _ getter: ImportedGetterSkeleton, topLevelDecls: inout [DeclSyntax] ) throws -> [DeclSyntax] { - let builder = CallJSEmission(moduleName: moduleName, abiName: getter.abiName(context: nil)) - try builder.call(returnType: getter.type) - try builder.liftReturnValue(returnType: getter.type) + let builder = try CallJSEmission( + moduleName: moduleName, + abiName: getter.abiName(context: nil), + returnType: getter.type + ) + try builder.call() + try builder.liftReturnValue() topLevelDecls.append(builder.renderImportDecl()) return [ builder.renderThunkDecl( @@ -81,14 +85,16 @@ public struct ImportTS { } class CallJSEmission { - private struct BorrowedStringArgument { - let expression: String - let forwardedParameterNames: [String] - let isOptional: Bool + private struct BorrowedArgument { + /// The variable name of the `bridgeJSWithLoweredParameter` return value + let returnVariableName: String? + /// The closure to close the borrowing call + let closeBorrowedClosure: (CodeFragmentPrinter) -> Void } let abiName: String let moduleName: String + let returnType: BridgeType let context: BridgeContext var body = CodeFragmentPrinter() @@ -101,33 +107,18 @@ public struct ImportTS { var stackLoweringStmts: [String] = [] // Values to extend lifetime during call var valuesToExtendLifetimeDuringCall: [String] = [] - // String-like parameters lowered through borrowed UTF-8 wrappers at call site. - private var borrowedStringArguments: [BorrowedStringArgument] = [] + // Closures to close borrowed parameters + private var borrowedArguments: [BorrowedArgument] = [] + private let needsReturnVariable: Bool - init(moduleName: String, abiName: String, context: BridgeContext = .importTS) { + init(moduleName: String, abiName: String, returnType: BridgeType, context: BridgeContext = .importTS) throws { self.moduleName = moduleName self.abiName = abiName + self.returnType = returnType self.context = context - } - - private func borrowedStringInfo(for param: Parameter) -> (expression: String, isOptional: Bool)? { - switch param.type { - case .string: - return (param.name, false) - case .rawValueEnum(_, .string): - return ("\(param.name).rawValue", false) - case .nullable(let wrappedType, _): - switch wrappedType { - case .string: - return ("\(param.name).asOptional", true) - case .rawValueEnum(_, .string): - return ("\(param.name).asOptional?.rawValue", true) - default: - return nil - } - default: - return nil - } + let liftingInfo = try returnType.liftingReturnInfo(context: context) + needsReturnVariable = + !(returnType == .void || returnType.usesSideChannelForOptionalReturn() || liftingInfo.valueToLift == nil) } func lowerParameter(param: Parameter) throws { @@ -144,41 +135,6 @@ public struct ImportTS { break } - if let borrowed = borrowedStringInfo(for: param) { - let destructuredNames = loweringInfo.loweredParameters.map { - "\(param.name)\($0.name.capitalizedFirstLetter)" - } - if loweringInfo.loweredParameters.count != destructuredNames.count || destructuredNames.isEmpty { - throw BridgeJSCoreError("Unexpected borrowed string lowering shape for parameter \(param.name)") - } - - for (index, (paramName, type)) in loweringInfo.loweredParameters.enumerated() { - let abiParamName: String - if loweringInfo.loweredParameters.count == 1 { - abiParamName = param.name - } else { - abiParamName = "\(param.name)\(paramName.capitalizedFirstLetter)" - } - abiParameterSignatures.append((abiParamName, type)) - abiParameterForwardings.append(destructuredNames[index]) - } - borrowedStringArguments.append( - BorrowedStringArgument( - expression: borrowed.expression, - forwardedParameterNames: destructuredNames, - isOptional: borrowed.isOptional - ) - ) - return - } - - let initializerExpr = ExprSyntax("\(raw: param.name).bridgeJSLowerParameter()") - - if loweringInfo.loweredParameters.isEmpty { - stackLoweringStmts.insert("let _ = \(initializerExpr)", at: 0) - return - } - // Generate destructured variable names for all lowered parameters let destructuredNames = loweringInfo.loweredParameters.map { "\(param.name)\($0.name.capitalizedFirstLetter)" @@ -192,7 +148,30 @@ public struct ImportTS { pattern = "(" + destructuredNames.joined(separator: ", ") + ")" } - body.write("let \(pattern) = \(initializerExpr)") + if loweringInfo.useBorrowing { + let returnVariableName = "ret\(borrowedArguments.count)" + let assign = needsReturnVariable ? "let \(returnVariableName) = " : "" + body.write("\(assign)\(param.name).bridgeJSWithLoweredParameter { \(pattern) in") + body.indent() + borrowedArguments.append( + BorrowedArgument( + returnVariableName: needsReturnVariable ? returnVariableName : nil, + closeBorrowedClosure: { printer in + printer.unindent() + printer.write("}") + } + ) + ) + } else { + let initializerExpr = ExprSyntax("\(raw: param.name).bridgeJSLowerParameter()") + + if loweringInfo.loweredParameters.isEmpty { + stackLoweringStmts.insert("let _ = \(initializerExpr)", at: 0) + return + } + + body.write("let \(pattern) = \(initializerExpr)") + } destructuredVarNames.append(contentsOf: destructuredNames) // Add to signatures and forwardings (unified for both single and multiple) @@ -212,15 +191,12 @@ public struct ImportTS { } } - func call(returnType: BridgeType) throws { - let liftingInfo: BridgeType.LiftingReturnInfo = try returnType.liftingReturnInfo(context: context) + func call() throws { for stmt in stackLoweringStmts { body.write(stmt.description) } - let assign = - (returnType == .void || returnType.usesSideChannelForOptionalReturn() || liftingInfo.valueToLift == nil) - ? "" : "let ret = " + let assign = needsReturnVariable ? "let ret = " : "" var callExpr = "\(abiName)(\(abiParameterForwardings.joined(separator: ", ")))" if !valuesToExtendLifetimeDuringCall.isEmpty { @@ -228,28 +204,23 @@ public struct ImportTS { "withExtendedLifetime((\(valuesToExtendLifetimeDuringCall.joined(separator: ", ")))) { \(callExpr) }" } - if !borrowedStringArguments.isEmpty { - for argument in borrowedStringArguments.reversed() { - if argument.isOptional { - guard argument.forwardedParameterNames.count == 3 else { - throw BridgeJSCoreError( - "Optional borrowed string argument must have 3 lowered values: \(argument.expression)" - ) - } - callExpr = - "_swift_js_with_optional_borrowed_utf8(\(argument.expression)) { \(argument.forwardedParameterNames[0]), \(argument.forwardedParameterNames[1]), \(argument.forwardedParameterNames[2]) in \(callExpr) }" - } else { - guard argument.forwardedParameterNames.count == 2 else { - throw BridgeJSCoreError( - "Borrowed string argument must have 2 lowered values: \(argument.expression)" - ) - } - callExpr = - "_swift_js_with_borrowed_utf8(\(argument.expression)) { \(argument.forwardedParameterNames[0]), \(argument.forwardedParameterNames[1]) in \(callExpr) }" + body.write("\(assign)\(callExpr)") + + if let firstBorrowedArgument = borrowedArguments.first { + if needsReturnVariable { + body.write("return ret") + } + for borrowedArgument in borrowedArguments.dropFirst().reversed() { + borrowedArgument.closeBorrowedClosure(body) + if let returnVariableName = borrowedArgument.returnVariableName { + body.write("return \(returnVariableName)") } } + firstBorrowedArgument.closeBorrowedClosure(body) + if let returnVariableName = firstBorrowedArgument.returnVariableName { + body.write("let ret = \(returnVariableName)") + } } - body.write("\(assign)\(callExpr)") // Add exception check for ImportTS context if context == .importTS { @@ -257,7 +228,7 @@ public struct ImportTS { } } - func liftReturnValue(returnType: BridgeType) throws { + func liftReturnValue() throws { let liftingInfo = try returnType.liftingReturnInfo(context: context) if returnType == .void { @@ -367,12 +338,16 @@ public struct ImportTS { _ function: ImportedFunctionSkeleton, topLevelDecls: inout [DeclSyntax] ) throws -> [DeclSyntax] { - let builder = CallJSEmission(moduleName: moduleName, abiName: function.abiName(context: nil)) + let builder = try CallJSEmission( + moduleName: moduleName, + abiName: function.abiName(context: nil), + returnType: function.returnType + ) for param in function.parameters { try builder.lowerParameter(param: param) } - try builder.call(returnType: function.returnType) - try builder.liftReturnValue(returnType: function.returnType) + try builder.call() + try builder.liftReturnValue() topLevelDecls.append(builder.renderImportDecl()) return [ builder.renderThunkDecl( @@ -389,13 +364,17 @@ public struct ImportTS { var decls: [DeclSyntax] = [] func renderMethod(method: ImportedFunctionSkeleton) throws -> [DeclSyntax] { - let builder = CallJSEmission(moduleName: moduleName, abiName: method.abiName(context: type)) + let builder = try CallJSEmission( + moduleName: moduleName, + abiName: method.abiName(context: type), + returnType: method.returnType + ) try builder.lowerParameter(param: selfParameter) for param in method.parameters { try builder.lowerParameter(param: param) } - try builder.call(returnType: method.returnType) - try builder.liftReturnValue(returnType: method.returnType) + try builder.call() + try builder.liftReturnValue() topLevelDecls.append(builder.renderImportDecl()) return [ builder.renderThunkDecl( @@ -408,12 +387,12 @@ public struct ImportTS { func renderStaticMethod(method: ImportedFunctionSkeleton) throws -> [DeclSyntax] { let abiName = method.abiName(context: type, operation: "static") - let builder = CallJSEmission(moduleName: moduleName, abiName: abiName) + let builder = try CallJSEmission(moduleName: moduleName, abiName: abiName, returnType: method.returnType) for param in method.parameters { try builder.lowerParameter(param: param) } - try builder.call(returnType: method.returnType) - try builder.liftReturnValue(returnType: method.returnType) + try builder.call() + try builder.liftReturnValue() topLevelDecls.append(builder.renderImportDecl()) return [ builder.renderThunkDecl( @@ -425,12 +404,16 @@ public struct ImportTS { } func renderConstructorDecl(constructor: ImportedConstructorSkeleton) throws -> [DeclSyntax] { - let builder = CallJSEmission(moduleName: moduleName, abiName: constructor.abiName(context: type)) + let builder = try CallJSEmission( + moduleName: moduleName, + abiName: constructor.abiName(context: type), + returnType: .jsObject(nil) + ) for param in constructor.parameters { try builder.lowerParameter(param: param) } - try builder.call(returnType: .jsObject(nil)) - try builder.liftReturnValue(returnType: .jsObject(nil)) + try builder.call() + try builder.liftReturnValue() topLevelDecls.append(builder.renderImportDecl()) return [ builder.renderThunkDecl( @@ -442,13 +425,14 @@ public struct ImportTS { } func renderGetterDecl(getter: ImportedGetterSkeleton) throws -> DeclSyntax { - let builder = CallJSEmission( + let builder = try CallJSEmission( moduleName: moduleName, - abiName: getter.abiName(context: type) + abiName: getter.abiName(context: type), + returnType: getter.type ) try builder.lowerParameter(param: selfParameter) - try builder.call(returnType: getter.type) - try builder.liftReturnValue(returnType: getter.type) + try builder.call() + try builder.liftReturnValue() topLevelDecls.append(builder.renderImportDecl()) return DeclSyntax( builder.renderThunkDecl( @@ -460,14 +444,15 @@ public struct ImportTS { } func renderSetterDecl(setter: ImportedSetterSkeleton) throws -> DeclSyntax { - let builder = CallJSEmission( + let builder = try CallJSEmission( moduleName: moduleName, - abiName: setter.abiName(context: type) + abiName: setter.abiName(context: type), + returnType: .void ) let newValue = Parameter(label: nil, name: "newValue", type: setter.type) try builder.lowerParameter(param: selfParameter) try builder.lowerParameter(param: newValue) - try builder.call(returnType: .void) + try builder.call() topLevelDecls.append(builder.renderImportDecl()) // Use functionName if available (has lowercase first char), otherwise derive from name let propertyNameForThunk: String @@ -792,12 +777,18 @@ enum SwiftCodePattern { extension BridgeType { struct LoweringParameterInfo { let loweredParameters: [(name: String, type: WasmCoreType)] + /// If true, the parameter should be lowered by using `bridgeJSWithLoweredParameter` + /// that takes a closure to handle the borrowed parameter. + var useBorrowing: Bool = false static let bool = LoweringParameterInfo(loweredParameters: [("value", .i32)]) static let int = LoweringParameterInfo(loweredParameters: [("value", .i32)]) static let float = LoweringParameterInfo(loweredParameters: [("value", .f32)]) static let double = LoweringParameterInfo(loweredParameters: [("value", .f64)]) - static let string = LoweringParameterInfo(loweredParameters: [("bytes", .i32), ("length", .i32)]) + static let string = LoweringParameterInfo( + loweredParameters: [("bytes", .i32), ("length", .i32)], + useBorrowing: true + ) static let jsObject = LoweringParameterInfo(loweredParameters: [("value", .i32)]) static let jsValue = LoweringParameterInfo(loweredParameters: [ ("kind", .i32), @@ -860,7 +851,7 @@ extension BridgeType { let wrappedInfo = try wrappedType.loweringParameterInfo(context: context) var params = [("isSome", WasmCoreType.i32)] params.append(contentsOf: wrappedInfo.loweredParameters) - return LoweringParameterInfo(loweredParameters: params) + return LoweringParameterInfo(loweredParameters: params, useBorrowing: wrappedInfo.useBorrowing) case .array, .dictionary: return LoweringParameterInfo(loweredParameters: []) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift index f5525384e..4511ed1df 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/EnumRawType.swift @@ -464,7 +464,7 @@ fileprivate func bjs_takesFeatureFlag_extern(_ flagBytes: Int32, _ flagLength: I } func _$takesFeatureFlag(_ flag: FeatureFlag) throws(JSException) -> Void { - _swift_js_with_borrowed_utf8(flag.rawValue) { flagBytes, flagLength in + flag.bridgeJSWithLoweredParameter { (flagBytes, flagLength) in bjs_takesFeatureFlag(flagBytes, flagLength) } if let error = _swift_js_take_exception() { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift index c65d81556..5e7088db8 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.swift @@ -32,7 +32,7 @@ fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ messageBytes: Int32, func _$JSConsole_log(_ self: JSObject, _ message: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(message) { messageBytes, messageLength in + message.bridgeJSWithLoweredParameter { (messageBytes, messageLength) in bjs_JSConsole_log(selfValue, messageBytes, messageLength) } if let error = _swift_js_take_exception() { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift index c455bea9a..35b1c6281 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.swift @@ -31,9 +31,11 @@ fileprivate func bjs_parseInt_extern(_ stringBytes: Int32, _ stringLength: Int32 } func _$parseInt(_ string: String) throws(JSException) -> Double { - let ret = _swift_js_with_borrowed_utf8(string) { stringBytes, stringLength in - bjs_parseInt(stringBytes, stringLength) + let ret0 = string.bridgeJSWithLoweredParameter { (stringBytes, stringLength) in + let ret = bjs_parseInt(stringBytes, stringLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -54,7 +56,7 @@ fileprivate func bjs_JSConsole_log_extern(_ self: Int32, _ messageBytes: Int32, func _$JSConsole_log(_ self: JSObject, _ message: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(message) { messageBytes, messageLength in + message.bridgeJSWithLoweredParameter { (messageBytes, messageLength) in bjs_JSConsole_log(selfValue, messageBytes, messageLength) } if let error = _swift_js_take_exception() { @@ -87,9 +89,11 @@ fileprivate func bjs_WebSocket_close_extern(_ self: Int32) -> Void { } func _$WebSocket_init(_ url: String) throws(JSException) -> JSObject { - let ret = _swift_js_with_borrowed_utf8(url) { urlBytes, urlLength in - bjs_WebSocket_init(urlBytes, urlLength) + let ret0 = url.bridgeJSWithLoweredParameter { (urlBytes, urlLength) in + let ret = bjs_WebSocket_init(urlBytes, urlLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift index 77f130829..7f7fa8685 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.swift @@ -328,7 +328,7 @@ func _$WeirdNaming_Any_get(_ self: JSObject) throws(JSException) -> String { func _$WeirdNaming_normalProperty_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_WeirdNaming_normalProperty_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -356,7 +356,7 @@ func _$WeirdNaming__123invalidStart_set(_ self: JSObject, _ newValue: Bool) thro func _$WeirdNaming_property_with_spaces_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_WeirdNaming_property_with_spaces_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -375,7 +375,7 @@ func _$WeirdNaming__specialChar_set(_ self: JSObject, _ newValue: Double) throws func _$WeirdNaming_constructor_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_WeirdNaming_constructor_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -385,7 +385,7 @@ func _$WeirdNaming_constructor_set(_ self: JSObject, _ newValue: String) throws( func _$WeirdNaming_for_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_WeirdNaming_for_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -395,7 +395,7 @@ func _$WeirdNaming_for_set(_ self: JSObject, _ newValue: String) throws(JSExcept func _$WeirdNaming_any_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_WeirdNaming_any_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift index 3bcb539f7..3e1de0030 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.swift @@ -91,9 +91,11 @@ fileprivate func bjs_Greeter_changeName_extern(_ self: Int32, _ nameBytes: Int32 } func _$Greeter_init(_ name: String) throws(JSException) -> JSObject { - let ret = _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in - bjs_Greeter_init(nameBytes, nameLength) + let ret0 = name.bridgeJSWithLoweredParameter { (nameBytes, nameLength) in + let ret = bjs_Greeter_init(nameBytes, nameLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -120,7 +122,7 @@ func _$Greeter_age_get(_ self: JSObject) throws(JSException) -> Double { func _$Greeter_name_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_Greeter_name_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -139,7 +141,7 @@ func _$Greeter_greet(_ self: JSObject) throws(JSException) -> String { func _$Greeter_changeName(_ self: JSObject, _ name: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + name.bridgeJSWithLoweredParameter { (nameBytes, nameLength) in bjs_Greeter_changeName(selfValue, nameBytes, nameLength) } if let error = _swift_js_take_exception() { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift index bf1a551ec..ca1078836 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.swift @@ -647,11 +647,14 @@ fileprivate func bjs_WithOptionalJSClass_roundTripIntOrUndefined_extern(_ self: } func _$WithOptionalJSClass_init(_ valueOrNull: Optional, _ valueOrUndefined: JSUndefinedOr) throws(JSException) -> JSObject { - let ret = _swift_js_with_optional_borrowed_utf8(valueOrNull.asOptional) { valueOrNullIsSome, valueOrNullBytes, valueOrNullLength in - _swift_js_with_optional_borrowed_utf8(valueOrUndefined.asOptional) { valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedLength in - bjs_WithOptionalJSClass_init(valueOrNullIsSome, valueOrNullBytes, valueOrNullLength, valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedLength) + let ret0 = valueOrNull.bridgeJSWithLoweredParameter { (valueOrNullIsSome, valueOrNullBytes, valueOrNullLength) in + let ret1 = valueOrUndefined.bridgeJSWithLoweredParameter { (valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedLength) in + let ret = bjs_WithOptionalJSClass_init(valueOrNullIsSome, valueOrNullBytes, valueOrNullLength, valueOrUndefinedIsSome, valueOrUndefinedBytes, valueOrUndefinedLength) + return ret } + return ret1 } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -732,7 +735,7 @@ func _$WithOptionalJSClass_intOrUndefined_get(_ self: JSObject) throws(JSExcepti func _$WithOptionalJSClass_stringOrNull_set(_ self: JSObject, _ newValue: Optional) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueIsSome, newValueBytes, newValueLength) in bjs_WithOptionalJSClass_stringOrNull_set(selfValue, newValueIsSome, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -742,7 +745,7 @@ func _$WithOptionalJSClass_stringOrNull_set(_ self: JSObject, _ newValue: Option func _$WithOptionalJSClass_stringOrUndefined_set(_ self: JSObject, _ newValue: JSUndefinedOr) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueIsSome, newValueBytes, newValueLength) in bjs_WithOptionalJSClass_stringOrUndefined_set(selfValue, newValueIsSome, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -806,7 +809,7 @@ func _$WithOptionalJSClass_intOrUndefined_set(_ self: JSObject, _ newValue: JSUn func _$WithOptionalJSClass_roundTripStringOrNull(_ self: JSObject, _ value: Optional) throws(JSException) -> Optional { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(value.asOptional) { valueIsSome, valueBytes, valueLength in + value.bridgeJSWithLoweredParameter { (valueIsSome, valueBytes, valueLength) in bjs_WithOptionalJSClass_roundTripStringOrNull(selfValue, valueIsSome, valueBytes, valueLength) } if let error = _swift_js_take_exception() { @@ -817,7 +820,7 @@ func _$WithOptionalJSClass_roundTripStringOrNull(_ self: JSObject, _ value: Opti func _$WithOptionalJSClass_roundTripStringOrUndefined(_ self: JSObject, _ value: JSUndefinedOr) throws(JSException) -> JSUndefinedOr { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(value.asOptional) { valueIsSome, valueBytes, valueLength in + value.bridgeJSWithLoweredParameter { (valueIsSome, valueBytes, valueLength) in bjs_WithOptionalJSClass_roundTripStringOrUndefined(selfValue, valueIsSome, valueBytes, valueLength) } if let error = _swift_js_take_exception() { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift index ae71ee634..d9cd22836 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Protocol.swift @@ -8,7 +8,9 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto func onValueChanged(_ value: String) -> Void { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(value) { valueBytes, valueLength in _extern_onValueChanged(jsObjectValue, valueBytes, valueLength) } + value.bridgeJSWithLoweredParameter { (valueBytes, valueLength) in + _extern_onValueChanged(jsObjectValue, valueBytes, valueLength) + } } func onCountUpdated(count: Int) -> Bool { @@ -20,7 +22,11 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto func onLabelUpdated(_ prefix: String, _ suffix: String) -> Void { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(prefix) { prefixBytes, prefixLength in _swift_js_with_borrowed_utf8(suffix) { suffixBytes, suffixLength in _extern_onLabelUpdated(jsObjectValue, prefixBytes, prefixLength, suffixBytes, suffixLength) } } + prefix.bridgeJSWithLoweredParameter { (prefixBytes, prefixLength) in + suffix.bridgeJSWithLoweredParameter { (suffixBytes, suffixLength) in + _extern_onLabelUpdated(jsObjectValue, prefixBytes, prefixLength, suffixBytes, suffixLength) + } + } } func isCountEven() -> Bool { @@ -100,7 +106,9 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in bjs_MyViewControllerDelegate_optionalName_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } + newValue.bridgeJSWithLoweredParameter { (newValueIsSome, newValueBytes, newValueLength) in + bjs_MyViewControllerDelegate_optionalName_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) + } } } @@ -112,7 +120,9 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(newValue.asOptional?.rawValue) { newValueIsSome, newValueBytes, newValueLength in bjs_MyViewControllerDelegate_optionalRawEnum_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } + newValue.bridgeJSWithLoweredParameter { (newValueIsSome, newValueBytes, newValueLength) in + bjs_MyViewControllerDelegate_optionalRawEnum_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) + } } } @@ -124,7 +134,9 @@ struct AnyMyViewControllerDelegate: MyViewControllerDelegate, _BridgedSwiftProto } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue.rawValue) { newValueBytes, newValueLength in bjs_MyViewControllerDelegate_rawStringEnum_set(jsObjectValue, newValueBytes, newValueLength) } + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in + bjs_MyViewControllerDelegate_rawStringEnum_set(jsObjectValue, newValueBytes, newValueLength) + } } } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift index 9b14b26b2..0760df764 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StringParameter.swift @@ -32,7 +32,7 @@ fileprivate func bjs_checkString_extern(_ aBytes: Int32, _ aLength: Int32) -> Vo } func _$checkString(_ a: String) throws(JSException) -> Void { - _swift_js_with_borrowed_utf8(a) { aBytes, aLength in + a.bridgeJSWithLoweredParameter { (aBytes, aLength) in bjs_checkString(aBytes, aLength) } if let error = _swift_js_take_exception() { @@ -53,8 +53,8 @@ fileprivate func bjs_checkStringWithLength_extern(_ aBytes: Int32, _ aLength: In } func _$checkStringWithLength(_ a: String, _ b: Double) throws(JSException) -> Void { - let bValue = b.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(a) { aBytes, aLength in + a.bridgeJSWithLoweredParameter { (aBytes, aLength) in + let bValue = b.bridgeJSLowerParameter() bjs_checkStringWithLength(aBytes, aLength, bValue) } if let error = _swift_js_take_exception() { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift index 93de693b6..e2cf9e09b 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosure.swift @@ -91,9 +91,11 @@ private enum _BJS_Closure_10TestModule5ThemeO_5ThemeO { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(param0.rawValue) { param0Bytes, param0Length in - invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO(callbackValue, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0Bytes, param0Length) in + let ret = invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO(callbackValue, param0Bytes, param0Length) + return ret } + let ret = ret0 return Theme.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -344,9 +346,11 @@ private enum _BJS_Closure_10TestModuleSS_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(param0) { param0Bytes, param0Length in - invoke_js_callback_TestModule_10TestModuleSS_SS(callbackValue, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0Bytes, param0Length) in + let ret = invoke_js_callback_TestModule_10TestModuleSS_SS(callbackValue, param0Bytes, param0Length) + return ret } + let ret = ret0 return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -723,7 +727,7 @@ private enum _BJS_Closure_10TestModuleSq5ThemeO_Sq5ThemeO { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(param0.asOptional?.rawValue) { param0IsSome, param0Bytes, param0Length in + param0.bridgeJSWithLoweredParameter { (param0IsSome, param0Bytes, param0Length) in invoke_js_callback_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO(callbackValue, param0IsSome, param0Bytes, param0Length) } return Optional.bridgeJSLiftReturnFromSideChannel() @@ -976,7 +980,7 @@ private enum _BJS_Closure_10TestModuleSqSS_SqSS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(param0.asOptional) { param0IsSome, param0Bytes, param0Length in + param0.bridgeJSWithLoweredParameter { (param0IsSome, param0Bytes, param0Length) in invoke_js_callback_TestModule_10TestModuleSqSS_SqSS(callbackValue, param0IsSome, param0Bytes, param0Length) } return Optional.bridgeJSLiftReturnFromSideChannel() diff --git a/Sources/JavaScriptKit/BridgeJSIntrinsics.swift b/Sources/JavaScriptKit/BridgeJSIntrinsics.swift index 56c2fede9..ae8ee2de0 100644 --- a/Sources/JavaScriptKit/BridgeJSIntrinsics.swift +++ b/Sources/JavaScriptKit/BridgeJSIntrinsics.swift @@ -348,9 +348,9 @@ extension String: _BridgedSwiftStackType { // MARK: ImportTS - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Int32 { + @_spi(BridgeJS) public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32) -> T) -> T { return self.withUTF8 { b in - _swift_js_make_js_string(b.baseAddress.unsafelyUnwrapped, Int32(b.count)) + body(Int32(bitPattern: UInt32(UInt(bitPattern: b.baseAddress))), Int32(b.count)) } } @@ -395,38 +395,6 @@ extension String: _BridgedSwiftStackType { } } -/// Runs `body` with a borrowed UTF-8 (address, count) tuple. -@_spi(BridgeJS) @_transparent -public func _swift_js_with_borrowed_utf8( - _ value: consuming String, - _ body: (Int32, Int32) -> R -) -> R { - return value.withUTF8 { utf8 in - let address = utf8.baseAddress.map { Int32(truncatingIfNeeded: UInt(bitPattern: $0)) } ?? 0 - return body(address, Int32(utf8.count)) - } -} - -/// Runs `body` with optional borrowed UTF-8 components. -/// -/// .none is encoded as `(0, 0, 0)`. -/// .some is encoded as `(1, address, count)`. -@_spi(BridgeJS) @_transparent -public func _swift_js_with_optional_borrowed_utf8( - _ value: consuming String?, - _ body: (Int32, Int32, Int32) -> R -) -> R { - switch consume value { - case .none: - return body(0, 0, 0) - case .some(var value): - return value.withUTF8 { utf8 in - let address = utf8.baseAddress.map { Int32(truncatingIfNeeded: UInt(bitPattern: $0)) } ?? 0 - return body(1, address, Int32(utf8.count)) - } - } -} - extension JSObject: _BridgedSwiftStackType { // JSObject is a non-final class, so we must explicitly specify the associated type // rather than relying on the default `Self` (which Swift requires for covariant returns). @@ -764,7 +732,7 @@ extension _BridgedSwiftStruct { extension _BridgedSwiftEnumNoPayload where Self: RawRepresentable, RawValue == String { // MARK: ImportTS - @_spi(BridgeJS) public consuming func bridgeJSLowerParameter() -> Int32 { rawValue.bridgeJSLowerParameter() } + @_spi(BridgeJS) public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32) -> T) -> T { rawValue.bridgeJSWithLoweredParameter(body) } @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ id: Int32) -> Self { Self(rawValue: .bridgeJSLiftReturn(id))! @@ -1534,10 +1502,15 @@ extension _BridgedAsOptional where Wrapped == Bool { } extension _BridgedAsOptional where Wrapped == String { - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> ( - isSome: Int32, value: Int32 - ) { - asOptional._bridgeJSLowerParameter(noneValue: 0, lowerWrapped: { $0.bridgeJSLowerParameter() }) + @_spi(BridgeJS) @_transparent public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32, Int32) -> T) -> T { + switch asOptional { + case .none: + return body(0, 0, 0) + case .some(let value): + return value.bridgeJSWithLoweredParameter { bytes, count in + return body(1, bytes, count) + } + } } @_spi(BridgeJS) public static func bridgeJSLiftParameter( @@ -1710,14 +1683,14 @@ extension _BridgedAsOptional where Wrapped: _BridgedSwiftCaseEnum { extension _BridgedAsOptional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepresentable, Wrapped.RawValue == String { - @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> ( - isSome: Int32, value: Int32 - ) { + @_spi(BridgeJS) @_transparent public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32, Int32) -> T) -> T { switch asOptional { case .none: - return (isSome: 0, value: 0) + return body(0, 0, 0) case .some(let wrapped): - return (isSome: 1, value: wrapped.bridgeJSLowerParameter()) + return wrapped.bridgeJSWithLoweredParameter { bytes, count in + return body(1, bytes, count) + } } } diff --git a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift index 354fe97e8..13aab1455 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift +++ b/Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift @@ -100,9 +100,11 @@ private enum _BJS_Closure_20BridgeJSRuntimeTests5ThemeO_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(param0.rawValue) { param0Bytes, param0Length in - invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS(callbackValue, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0Bytes, param0Length) in + let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_SS(callbackValue, param0Bytes, param0Length) + return ret } + let ret = ret0 return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -164,9 +166,11 @@ private enum _BJS_Closure_20BridgeJSRuntimeTests5ThemeO_Sb { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(param0.rawValue) { param0Bytes, param0Length in - invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb(callbackValue, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0Bytes, param0Length) in + let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTests5ThemeO_Sb(callbackValue, param0Bytes, param0Length) + return ret } + let ret = ret0 return Bool.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -543,9 +547,11 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSS_7GreeterC { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(param0) { param0Bytes, param0Length in - invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC(callbackValue, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0Bytes, param0Length) in + let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_7GreeterC(callbackValue, param0Bytes, param0Length) + return ret } + let ret = ret0 return Greeter.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -607,9 +613,11 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSS_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(param0) { param0Bytes, param0Length in - invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS(callbackValue, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0Bytes, param0Length) in + let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSS_SS(callbackValue, param0Bytes, param0Length) + return ret } + let ret = ret0 return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -735,10 +743,12 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSiSSSd_SS { #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() let param0Value = param0.bridgeJSLowerParameter() - let param2Value = param2.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(param1) { param1Bytes, param1Length in - invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS(callbackValue, param0Value, param1Bytes, param1Length, param2Value) + let ret0 = param1.bridgeJSWithLoweredParameter { (param1Bytes, param1Length) in + let param2Value = param2.bridgeJSLowerParameter() + let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSiSSSd_SS(callbackValue, param0Value, param1Bytes, param1Length, param2Value) + return ret } + let ret = ret0 return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -1053,9 +1063,11 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSq5ThemeO_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_optional_borrowed_utf8(param0.asOptional?.rawValue) { param0IsSome, param0Bytes, param0Length in - invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS(callbackValue, param0IsSome, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0IsSome, param0Bytes, param0Length) in + let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS(callbackValue, param0IsSome, param0Bytes, param0Length) + return ret } + let ret = ret0 return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -1369,9 +1381,11 @@ private enum _BJS_Closure_20BridgeJSRuntimeTestsSqSS_SS { return { [callback] param0 in #if arch(wasm32) let callbackValue = callback.bridgeJSLowerParameter() - let ret = _swift_js_with_optional_borrowed_utf8(param0.asOptional) { param0IsSome, param0Bytes, param0Length in - invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS(callbackValue, param0IsSome, param0Bytes, param0Length) + let ret0 = param0.bridgeJSWithLoweredParameter { (param0IsSome, param0Bytes, param0Length) in + let ret = invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSqSS_SS(callbackValue, param0IsSome, param0Bytes, param0Length) + return ret } + let ret = ret0 return String.bridgeJSLiftReturn(ret) #else fatalError("Only available on WebAssembly") @@ -1712,7 +1726,11 @@ struct AnyDataProcessor: DataProcessor, _BridgedSwiftProtocolWrapper { func setLabelElements(_ labelPrefix: String, _ labelSuffix: String) -> Void { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(labelPrefix) { labelPrefixBytes, labelPrefixLength in _swift_js_with_borrowed_utf8(labelSuffix) { labelSuffixBytes, labelSuffixLength in _extern_setLabelElements(jsObjectValue, labelPrefixBytes, labelPrefixLength, labelSuffixBytes, labelSuffixLength) } } + labelPrefix.bridgeJSWithLoweredParameter { (labelPrefixBytes, labelPrefixLength) in + labelSuffix.bridgeJSWithLoweredParameter { (labelSuffixBytes, labelSuffixLength) in + _extern_setLabelElements(jsObjectValue, labelPrefixBytes, labelPrefixLength, labelSuffixBytes, labelSuffixLength) + } + } } func getLabel() -> String { @@ -1794,7 +1812,9 @@ struct AnyDataProcessor: DataProcessor, _BridgedSwiftProtocolWrapper { } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(newValue.asOptional) { newValueIsSome, newValueBytes, newValueLength in bjs_DataProcessor_optionalTag_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } + newValue.bridgeJSWithLoweredParameter { (newValueIsSome, newValueBytes, newValueLength) in + bjs_DataProcessor_optionalTag_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) + } } } @@ -1832,7 +1852,9 @@ struct AnyDataProcessor: DataProcessor, _BridgedSwiftProtocolWrapper { } set { let jsObjectValue = jsObject.bridgeJSLowerParameter() - _swift_js_with_optional_borrowed_utf8(newValue.asOptional?.rawValue) { newValueIsSome, newValueBytes, newValueLength in bjs_DataProcessor_optionalTheme_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) } + newValue.bridgeJSWithLoweredParameter { (newValueIsSome, newValueBytes, newValueLength) in + bjs_DataProcessor_optionalTheme_set(jsObjectValue, newValueIsSome, newValueBytes, newValueLength) + } } } @@ -9249,9 +9271,11 @@ fileprivate func bjs_ArrayElementObject_id_get_extern(_ self: Int32) -> Int32 { } func _$ArrayElementObject_init(_ id: String) throws(JSException) -> JSObject { - let ret = _swift_js_with_borrowed_utf8(id) { idBytes, idLength in - bjs_ArrayElementObject_init(idBytes, idLength) + let ret0 = id.bridgeJSWithLoweredParameter { (idBytes, idLength) in + let ret = bjs_ArrayElementObject_init(idBytes, idLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -9875,10 +9899,12 @@ func _$ClosureSupportImports_jsApplyDouble(_ value: Double, _ transform: JSTyped } func _$ClosureSupportImports_jsApplyString(_ value: String, _ transform: JSTypedClosure<(String) -> String>) throws(JSException) -> String { - let transformFuncRef = transform.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(value) { valueBytes, valueLength in - bjs_ClosureSupportImports_jsApplyString_static(valueBytes, valueLength, transformFuncRef) + let ret0 = value.bridgeJSWithLoweredParameter { (valueBytes, valueLength) in + let transformFuncRef = transform.bridgeJSLowerParameter() + let ret = bjs_ClosureSupportImports_jsApplyString_static(valueBytes, valueLength, transformFuncRef) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -9914,9 +9940,11 @@ func _$ClosureSupportImports_jsMakeDoubleToDouble(_ base: Double) throws(JSExcep } func _$ClosureSupportImports_jsMakeStringToString(_ prefix: String) throws(JSException) -> (String) -> String { - let ret = _swift_js_with_borrowed_utf8(prefix) { prefixBytes, prefixLength in - bjs_ClosureSupportImports_jsMakeStringToString_static(prefixBytes, prefixLength) + let ret0 = prefix.bridgeJSWithLoweredParameter { (prefixBytes, prefixLength) in + let ret = bjs_ClosureSupportImports_jsMakeStringToString_static(prefixBytes, prefixLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -10169,9 +10197,11 @@ fileprivate func bjs_Foo_value_get_extern(_ self: Int32) -> Int32 { } func _$Foo_init(_ value: String) throws(JSException) -> JSObject { - let ret = _swift_js_with_borrowed_utf8(value) { valueBytes, valueLength in - bjs_Foo_init(valueBytes, valueLength) + let ret0 = value.bridgeJSWithLoweredParameter { (valueBytes, valueLength) in + let ret = bjs_Foo_init(valueBytes, valueLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -10281,9 +10311,11 @@ fileprivate func bjs_jsRoundTripString_extern(_ vBytes: Int32, _ vLength: Int32) } func _$jsRoundTripString(_ v: String) throws(JSException) -> String { - let ret = _swift_js_with_borrowed_utf8(v) { vBytes, vLength in - bjs_jsRoundTripString(vBytes, vLength) + let ret0 = v.bridgeJSWithLoweredParameter { (vBytes, vLength) in + let ret = bjs_jsRoundTripString(vBytes, vLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -10407,9 +10439,11 @@ fileprivate func bjs_jsRoundTripFeatureFlag_extern(_ flagBytes: Int32, _ flagLen } func _$jsRoundTripFeatureFlag(_ flag: FeatureFlag) throws(JSException) -> FeatureFlag { - let ret = _swift_js_with_borrowed_utf8(flag.rawValue) { flagBytes, flagLength in - bjs_jsRoundTripFeatureFlag(flagBytes, flagLength) + let ret0 = flag.bridgeJSWithLoweredParameter { (flagBytes, flagLength) in + let ret = bjs_jsRoundTripFeatureFlag(flagBytes, flagLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -10469,9 +10503,11 @@ fileprivate func bjs_parseInt_extern(_ stringBytes: Int32, _ stringLength: Int32 } func _$parseInt(_ string: String) throws(JSException) -> Double { - let ret = _swift_js_with_borrowed_utf8(string) { stringBytes, stringLength in - bjs_parseInt(stringBytes, stringLength) + let ret0 = string.bridgeJSWithLoweredParameter { (stringBytes, stringLength) in + let ret = bjs_parseInt(stringBytes, stringLength) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -10551,11 +10587,14 @@ fileprivate func bjs_JsGreeter_changeName_extern(_ self: Int32, _ nameBytes: Int } func _$JsGreeter_init(_ name: String, _ prefix: String) throws(JSException) -> JSObject { - let ret = _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in - _swift_js_with_borrowed_utf8(prefix) { prefixBytes, prefixLength in - bjs_JsGreeter_init(nameBytes, nameLength, prefixBytes, prefixLength) + let ret0 = name.bridgeJSWithLoweredParameter { (nameBytes, nameLength) in + let ret1 = prefix.bridgeJSWithLoweredParameter { (prefixBytes, prefixLength) in + let ret = bjs_JsGreeter_init(nameBytes, nameLength, prefixBytes, prefixLength) + return ret } + return ret1 } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -10582,7 +10621,7 @@ func _$JsGreeter_prefix_get(_ self: JSObject) throws(JSException) -> String { func _$JsGreeter_name_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_JsGreeter_name_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -10601,7 +10640,7 @@ func _$JsGreeter_greet(_ self: JSObject) throws(JSException) -> String { func _$JsGreeter_changeName(_ self: JSObject, _ name: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in + name.bridgeJSWithLoweredParameter { (nameBytes, nameLength) in bjs_JsGreeter_changeName(selfValue, nameBytes, nameLength) } if let error = _swift_js_take_exception() { @@ -10882,11 +10921,13 @@ fileprivate func bjs_Animal_getIsCat_extern(_ self: Int32) -> Int32 { } func _$Animal_init(_ name: String, _ age: Double, _ isCat: Bool) throws(JSException) -> JSObject { - let ageValue = age.bridgeJSLowerParameter() - let isCatValue = isCat.bridgeJSLowerParameter() - let ret = _swift_js_with_borrowed_utf8(name) { nameBytes, nameLength in - bjs_Animal_init(nameBytes, nameLength, ageValue, isCatValue) + let ret0 = name.bridgeJSWithLoweredParameter { (nameBytes, nameLength) in + let ageValue = age.bridgeJSLowerParameter() + let isCatValue = isCat.bridgeJSLowerParameter() + let ret = bjs_Animal_init(nameBytes, nameLength, ageValue, isCatValue) + return ret } + let ret = ret0 if let error = _swift_js_take_exception() { throw error } @@ -10922,7 +10963,7 @@ func _$Animal_isCat_get(_ self: JSObject) throws(JSException) -> Bool { func _$Animal_name_set(_ self: JSObject, _ newValue: String) throws(JSException) -> Void { let selfValue = self.bridgeJSLowerParameter() - _swift_js_with_borrowed_utf8(newValue) { newValueBytes, newValueLength in + newValue.bridgeJSWithLoweredParameter { (newValueBytes, newValueLength) in bjs_Animal_name_set(selfValue, newValueBytes, newValueLength) } if let error = _swift_js_take_exception() { @@ -11465,7 +11506,7 @@ func _$OptionalSupportImports_jsRoundTripOptionalNumberUndefined(_ value: JSUnde } func _$OptionalSupportImports_jsRoundTripOptionalStringNull(_ name: Optional) throws(JSException) -> Optional { - _swift_js_with_optional_borrowed_utf8(name.asOptional) { nameIsSome, nameBytes, nameLength in + name.bridgeJSWithLoweredParameter { (nameIsSome, nameBytes, nameLength) in bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(nameIsSome, nameBytes, nameLength) } if let error = _swift_js_take_exception() { @@ -11475,7 +11516,7 @@ func _$OptionalSupportImports_jsRoundTripOptionalStringNull(_ name: Optional) throws(JSException) -> JSUndefinedOr { - _swift_js_with_optional_borrowed_utf8(name.asOptional) { nameIsSome, nameBytes, nameLength in + name.bridgeJSWithLoweredParameter { (nameIsSome, nameBytes, nameLength) in bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(nameIsSome, nameBytes, nameLength) } if let error = _swift_js_take_exception() { From 6fc3f018ac16ca9e076135f8db7a226393a460de Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Thu, 5 Mar 2026 11:14:18 +0100 Subject: [PATCH 3/5] factored out decodeString global --- .../Sources/BridgeJSLink/BridgeJSLink.swift | 29 +++++++------- .../Sources/BridgeJSLink/JSGlueGen.swift | 12 ++---- .../BridgeJSLinkTests/ArrayTypes.js | 15 ++++--- .../__Snapshots__/BridgeJSLinkTests/Async.js | 15 ++++--- .../BridgeJSLinkTests/DefaultParameters.js | 15 ++++--- .../BridgeJSLinkTests/DictionaryTypes.js | 15 ++++--- .../BridgeJSLinkTests/EnumAssociatedValue.js | 15 ++++--- .../BridgeJSLinkTests/EnumCase.js | 15 ++++--- .../BridgeJSLinkTests/EnumNamespace.Global.js | 15 ++++--- .../BridgeJSLinkTests/EnumNamespace.js | 15 ++++--- .../BridgeJSLinkTests/EnumRawType.js | 19 ++++----- .../BridgeJSLinkTests/GlobalGetter.js | 19 ++++----- .../BridgeJSLinkTests/GlobalThisImports.js | 27 +++++-------- .../BridgeJSLinkTests/ImportArray.js | 15 ++++--- .../ImportedTypeInExportedInterface.js | 15 ++++--- .../BridgeJSLinkTests/InvalidPropertyNames.js | 35 ++++++----------- .../BridgeJSLinkTests/JSClass.js | 27 +++++-------- .../JSClassStaticFunctions.js | 15 ++++--- .../BridgeJSLinkTests/JSValue.js | 15 ++++--- .../BridgeJSLinkTests/MixedGlobal.js | 15 ++++--- .../BridgeJSLinkTests/MixedModules.js | 15 ++++--- .../BridgeJSLinkTests/MixedPrivate.js | 15 ++++--- .../BridgeJSLinkTests/Namespaces.Global.js | 15 ++++--- .../BridgeJSLinkTests/Namespaces.js | 15 ++++--- .../BridgeJSLinkTests/Optionals.js | 39 +++++++------------ .../BridgeJSLinkTests/PrimitiveParameters.js | 15 ++++--- .../BridgeJSLinkTests/PrimitiveReturn.js | 15 ++++--- .../BridgeJSLinkTests/PropertyTypes.js | 15 ++++--- .../BridgeJSLinkTests/Protocol.js | 39 +++++++------------ .../StaticFunctions.Global.js | 15 ++++--- .../BridgeJSLinkTests/StaticFunctions.js | 15 ++++--- .../StaticProperties.Global.js | 15 ++++--- .../BridgeJSLinkTests/StaticProperties.js | 15 ++++--- .../BridgeJSLinkTests/StringParameter.js | 23 +++++------ .../BridgeJSLinkTests/StringReturn.js | 15 ++++--- .../BridgeJSLinkTests/SwiftClass.js | 15 ++++--- .../BridgeJSLinkTests/SwiftClosure.js | 31 ++++++--------- .../BridgeJSLinkTests/SwiftClosureImports.js | 15 ++++--- .../BridgeJSLinkTests/SwiftStruct.js | 15 ++++--- .../BridgeJSLinkTests/SwiftStructImports.js | 15 ++++--- .../__Snapshots__/BridgeJSLinkTests/Throws.js | 15 ++++--- .../BridgeJSLinkTests/UnsafePointer.js | 15 ++++--- .../VoidParameterVoidReturn.js | 15 ++++--- 43 files changed, 336 insertions(+), 444 deletions(-) diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift index af2456f81..68232b673 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift @@ -289,6 +289,7 @@ public struct BridgeJSLink { "let \(JSGlueVariableScope.reservedInstance);", "let \(JSGlueVariableScope.reservedMemory);", "let \(JSGlueVariableScope.reservedSetException);", + "let \(JSGlueVariableScope.reservedDecodeString);", "const \(JSGlueVariableScope.reservedTextDecoder) = new TextDecoder(\"utf-8\");", "const \(JSGlueVariableScope.reservedTextEncoder) = new TextEncoder(\"utf-8\");", "let \(JSGlueVariableScope.reservedStorageToReturnString);", @@ -337,10 +338,7 @@ public struct BridgeJSLink { printer.write("bjs[\"swift_js_return_string\"] = function(ptr, len) {") printer.indent { printer.write( - "const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len)\(sharedMemory ? ".slice()" : "");" - ) - printer.write( - "\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedTextDecoder).decode(bytes);" + "\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedDecodeString)(ptr, len);" ) } printer.write("}") @@ -361,10 +359,7 @@ public struct BridgeJSLink { printer.write("bjs[\"swift_js_make_js_string\"] = function(ptr, len) {") printer.indent { printer.write( - "const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len)\(sharedMemory ? ".slice()" : "");" - ) - printer.write( - "return \(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).retain(\(JSGlueVariableScope.reservedTextDecoder).decode(bytes));" + "return \(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).retain(\(JSGlueVariableScope.reservedDecodeString)(ptr, len));" ) } printer.write("}") @@ -413,10 +408,7 @@ public struct BridgeJSLink { printer.write("}") printer.write("bjs[\"swift_js_push_string\"] = function(ptr, len) {") printer.indent { - printer.write( - "const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len)\(sharedMemory ? ".slice()" : "");" - ) - printer.write("const value = \(JSGlueVariableScope.reservedTextDecoder).decode(bytes);") + printer.write("const value = \(JSGlueVariableScope.reservedDecodeString)(ptr, len);") printer.write("\(JSGlueVariableScope.reservedStringStack).push(value);") } printer.write("}") @@ -529,8 +521,7 @@ public struct BridgeJSLink { printer.write("} else {") printer.indent { printer.write(lines: [ - "const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len);", - "\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedTextDecoder).decode(bytes);", + "\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedDecodeString)(ptr, len);" ]) } printer.write("}") @@ -1039,6 +1030,16 @@ public struct BridgeJSLink { "\(JSGlueVariableScope.reservedMemory) = \(JSGlueVariableScope.reservedInstance).exports.memory;", ]) printer.nextLine() + if sharedMemory { + printer.write( + "\(JSGlueVariableScope.reservedDecodeString) = (ptr, len) => { const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr >>> 0, len >>> 0).slice(); return \(JSGlueVariableScope.reservedTextDecoder).decode(bytes); }" + ) + } else { + printer.write( + "\(JSGlueVariableScope.reservedDecodeString) = (ptr, len) => { const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr >>> 0, len >>> 0); return \(JSGlueVariableScope.reservedTextDecoder).decode(bytes); }" + ) + } + printer.nextLine() // Error handling printer.write("\(JSGlueVariableScope.reservedSetException) = (error) => {") printer.indent { diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift index eb89a4c85..bec5ce3e9 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift @@ -13,6 +13,7 @@ final class JSGlueVariableScope { static let reservedInstance = "instance" static let reservedMemory = "memory" static let reservedSetException = "setException" + static let reservedDecodeString = "decodeString" static let reservedStorageToReturnString = "tmpRetString" static let reservedStorageToReturnBytes = "tmpRetBytes" static let reservedStorageToReturnException = "tmpRetException" @@ -40,6 +41,7 @@ final class JSGlueVariableScope { reservedInstance, reservedMemory, reservedSetException, + reservedDecodeString, reservedStorageToReturnString, reservedStorageToReturnBytes, reservedStorageToReturnException, @@ -266,17 +268,9 @@ struct IntrinsicJSFragment: Sendable { let (scope, printer) = (context.scope, context.printer) let bytesExpr = arguments[0] let countExpr = arguments[1] - let bytesLabel = scope.variable("bytesView") - let bytesToDecodeLabel = scope.variable("bytesToDecode") let stringLabel = scope.variable("string") printer.write( - "const \(bytesLabel) = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, \(bytesExpr), \(countExpr));" - ) - printer.write( - "const \(bytesToDecodeLabel) = (typeof SharedArrayBuffer !== \"undefined\" && \(bytesLabel).buffer instanceof SharedArrayBuffer) ? \(bytesLabel).slice() : \(bytesLabel);" - ) - printer.write( - "const \(stringLabel) = \(JSGlueVariableScope.reservedTextDecoder).decode(\(bytesToDecodeLabel));" + "const \(stringLabel) = \(JSGlueVariableScope.reservedDecodeString)(\(bytesExpr), \(countExpr));" ) return [stringLabel] } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayTypes.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayTypes.js index 81b432012..fcd29e31a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayTypes.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayTypes.js @@ -21,6 +21,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -62,8 +63,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -72,8 +72,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -99,8 +98,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -157,8 +155,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -330,6 +327,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.js index 6e7e810ca..d274e5606 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -189,6 +186,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js index b514a50f8..67e73fb33 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js @@ -14,6 +14,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -79,8 +80,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -89,8 +89,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -116,8 +115,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -181,8 +179,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -261,6 +258,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DictionaryTypes.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DictionaryTypes.js index 8627b2c80..7c31ce220 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DictionaryTypes.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DictionaryTypes.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -223,6 +220,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.js index 8e9dfa65f..18cc2c945 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.js @@ -89,6 +89,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -770,8 +771,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -780,8 +780,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -807,8 +806,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -865,8 +863,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -937,6 +934,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.js index 0ea2b0a53..a79d508a0 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.js @@ -32,6 +32,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -61,8 +62,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -71,8 +71,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -98,8 +97,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -149,8 +147,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -213,6 +210,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Global.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Global.js index 7c3166d36..e52cf02d5 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Global.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Global.js @@ -52,6 +52,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -81,8 +82,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -91,8 +91,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -118,8 +117,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -169,8 +167,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -253,6 +250,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.js index 6eea84ffa..2d05a7a2d 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.js @@ -33,6 +33,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -62,8 +63,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -72,8 +72,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -99,8 +98,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -150,8 +148,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -234,6 +231,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js index 3a5377241..5d28ce60b 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.js @@ -83,6 +83,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -113,8 +114,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -123,8 +123,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -150,8 +149,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -201,8 +199,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -263,9 +260,7 @@ export async function createInstantiator(options, swift) { const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_takesFeatureFlag"] = function bjs_takesFeatureFlag(flagBytes, flagCount) { try { - const bytesView = new Uint8Array(memory.buffer, flagBytes, flagCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(flagBytes, flagCount); imports.takesFeatureFlag(string); } catch (error) { setException(error); @@ -285,6 +280,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js index 7faf7a511..4d1e7abdf 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalGetter.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -197,9 +194,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_JSConsole_log"] = function bjs_JSConsole_log(self, messageBytes, messageCount) { try { - const bytesView = new Uint8Array(memory.buffer, messageBytes, messageCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(messageBytes, messageCount); swift.memory.getObject(self).log(string); } catch (error) { setException(error); @@ -210,6 +205,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js index 7f3ff6bc3..e4a17ebd0 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/GlobalThisImports.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -196,9 +193,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_parseInt"] = function bjs_parseInt(stringBytes, stringCount) { try { - const bytesView = new Uint8Array(memory.buffer, stringBytes, stringCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(stringBytes, stringCount); let ret = globalThis.parseInt(string); return ret; } catch (error) { @@ -208,9 +203,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_JSConsole_log"] = function bjs_JSConsole_log(self, messageBytes, messageCount) { try { - const bytesView = new Uint8Array(memory.buffer, messageBytes, messageCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(messageBytes, messageCount); swift.memory.getObject(self).log(string); } catch (error) { setException(error); @@ -218,9 +211,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_WebSocket_init"] = function bjs_WebSocket_init(urlBytes, urlCount) { try { - const bytesView = new Uint8Array(memory.buffer, urlBytes, urlCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(urlBytes, urlCount); return swift.memory.retain(new globalThis.WebSocket(string)); } catch (error) { setException(error); @@ -239,6 +230,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportArray.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportArray.js index 2a2fd3d50..7a97a47ff 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportArray.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportArray.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -223,6 +220,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportedTypeInExportedInterface.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportedTypeInExportedInterface.js index 8b938f75a..cfb0f75e7 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportedTypeInExportedInterface.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ImportedTypeInExportedInterface.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -76,8 +77,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -86,8 +86,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -113,8 +112,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -171,8 +169,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -244,6 +241,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js index 7002fdd72..ca54d8f16 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -278,9 +275,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_WeirdNaming_normalProperty_set"] = function bjs_WeirdNaming_normalProperty_set(self, newValueBytes, newValueCount) { try { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); swift.memory.getObject(self).normalProperty = string; } catch (error) { setException(error); @@ -302,9 +297,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_WeirdNaming_property_with_spaces_set"] = function bjs_WeirdNaming_property_with_spaces_set(self, newValueBytes, newValueCount) { try { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); swift.memory.getObject(self)["property with spaces"] = string; } catch (error) { setException(error); @@ -319,9 +312,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_WeirdNaming_constructor_set"] = function bjs_WeirdNaming_constructor_set(self, newValueBytes, newValueCount) { try { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); swift.memory.getObject(self).constructor = string; } catch (error) { setException(error); @@ -329,9 +320,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_WeirdNaming_for_set"] = function bjs_WeirdNaming_for_set(self, newValueBytes, newValueCount) { try { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); swift.memory.getObject(self).for = string; } catch (error) { setException(error); @@ -339,9 +328,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_WeirdNaming_any_set"] = function bjs_WeirdNaming_any_set(self, newValueBytes, newValueCount) { try { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); swift.memory.getObject(self).Any = string; } catch (error) { setException(error); @@ -381,6 +368,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js index ef2b8f1f2..7e97170e9 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClass.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -197,9 +194,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_Greeter_init"] = function bjs_Greeter_init(nameBytes, nameCount) { try { - const bytesView = new Uint8Array(memory.buffer, nameBytes, nameCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(nameBytes, nameCount); return swift.memory.retain(new imports.Greeter(string)); } catch (error) { setException(error); @@ -226,9 +221,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_Greeter_name_set"] = function bjs_Greeter_name_set(self, newValueBytes, newValueCount) { try { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); swift.memory.getObject(self).name = string; } catch (error) { setException(error); @@ -245,9 +238,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_Greeter_changeName"] = function bjs_Greeter_changeName(self, nameBytes, nameCount) { try { - const bytesView = new Uint8Array(memory.buffer, nameBytes, nameCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(nameBytes, nameCount); swift.memory.getObject(self).changeName(string); } catch (error) { setException(error); @@ -276,6 +267,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.js index 6912af0f3..39a859e4a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -253,6 +250,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSValue.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSValue.js index 8571bbd6c..9ed0b93f2 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSValue.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSValue.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -127,8 +128,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -137,8 +137,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -164,8 +163,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -215,8 +213,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -324,6 +321,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedGlobal.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedGlobal.js index dd0e0c55e..1b5651765 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedGlobal.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedGlobal.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -197,6 +194,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedModules.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedModules.js index 523df73cd..840a7a107 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedModules.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedModules.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -205,6 +202,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedPrivate.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedPrivate.js index 4d08957d0..672d5ef7b 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedPrivate.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedPrivate.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -197,6 +194,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Global.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Global.js index ac55ae750..8401e96b4 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Global.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Global.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -209,6 +206,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.js index 1fb247cbe..9efb38ffa 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -209,6 +206,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js index d6d6e68c3..f247c8efc 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Optionals.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -202,18 +199,14 @@ export async function createInstantiator(options, swift) { try { let optResult; if (valueOrNullIsSome) { - const bytesView = new Uint8Array(memory.buffer, valueOrNullBytes, valueOrNullCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(valueOrNullBytes, valueOrNullCount); optResult = string; } else { optResult = null; } let optResult1; if (valueOrUndefinedIsSome) { - const bytesView1 = new Uint8Array(memory.buffer, valueOrUndefinedBytes, valueOrUndefinedCount); - const bytesToDecode1 = (typeof SharedArrayBuffer !== "undefined" && bytesView1.buffer instanceof SharedArrayBuffer) ? bytesView1.slice() : bytesView1; - const string1 = textDecoder.decode(bytesToDecode1); + const string1 = decodeString(valueOrUndefinedBytes, valueOrUndefinedCount); optResult1 = string1; } else { optResult1 = undefined; @@ -300,9 +293,7 @@ export async function createInstantiator(options, swift) { try { let optResult; if (newValueIsSome) { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); optResult = string; } else { optResult = null; @@ -316,9 +307,7 @@ export async function createInstantiator(options, swift) { try { let optResult; if (newValueIsSome) { - const bytesView = new Uint8Array(memory.buffer, newValueBytes, newValueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(newValueBytes, newValueCount); optResult = string; } else { optResult = undefined; @@ -374,9 +363,7 @@ export async function createInstantiator(options, swift) { try { let optResult; if (valueIsSome) { - const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(valueBytes, valueCount); optResult = string; } else { optResult = null; @@ -392,9 +379,7 @@ export async function createInstantiator(options, swift) { try { let optResult; if (valueIsSome) { - const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(valueBytes, valueCount); optResult = string; } else { optResult = undefined; @@ -465,6 +450,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.js index 8999ae8b6..770212417 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveParameters.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -198,6 +195,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.js index c8372362d..0efb2d2b5 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PrimitiveReturn.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -209,6 +206,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.js index dbdd030b6..338e87f9a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/PropertyTypes.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -197,6 +194,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js index 105d15134..79711d878 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.js @@ -32,6 +32,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -94,8 +95,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -104,8 +104,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -131,8 +130,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -182,8 +180,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -295,9 +292,7 @@ export async function createInstantiator(options, swift) { try { let optResult; if (valueIsSome) { - const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(valueBytes, valueCount); optResult = string; } else { optResult = null; @@ -319,9 +314,7 @@ export async function createInstantiator(options, swift) { try { let optResult; if (valueIsSome) { - const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(valueBytes, valueCount); optResult = string; } else { optResult = null; @@ -342,9 +335,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_MyViewControllerDelegate_rawStringEnum_set"] = function bjs_MyViewControllerDelegate_rawStringEnum_set(self, valueBytes, valueCount) { try { - const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(valueBytes, valueCount); swift.memory.getObject(self).rawStringEnum = string; } catch (error) { setException(error); @@ -467,9 +458,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_MyViewControllerDelegate_onValueChanged"] = function bjs_MyViewControllerDelegate_onValueChanged(self, valueBytes, valueCount) { try { - const bytesView = new Uint8Array(memory.buffer, valueBytes, valueCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(valueBytes, valueCount); swift.memory.getObject(self).onValueChanged(string); } catch (error) { setException(error); @@ -486,12 +475,8 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_MyViewControllerDelegate_onLabelUpdated"] = function bjs_MyViewControllerDelegate_onLabelUpdated(self, prefixBytes, prefixCount, suffixBytes, suffixCount) { try { - const bytesView = new Uint8Array(memory.buffer, prefixBytes, prefixCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); - const bytesView1 = new Uint8Array(memory.buffer, suffixBytes, suffixCount); - const bytesToDecode1 = (typeof SharedArrayBuffer !== "undefined" && bytesView1.buffer instanceof SharedArrayBuffer) ? bytesView1.slice() : bytesView1; - const string1 = textDecoder.decode(bytesToDecode1); + const string = decodeString(prefixBytes, prefixCount); + const string1 = decodeString(suffixBytes, suffixCount); swift.memory.getObject(self).onLabelUpdated(string, string1); } catch (error) { setException(error); @@ -569,6 +554,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Global.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Global.js index 0e8a5a7bb..800b07107 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Global.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.Global.js @@ -19,6 +19,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -81,8 +82,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -91,8 +91,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -118,8 +117,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -169,8 +167,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -241,6 +238,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.js index 2eea1f12f..a4290f828 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticFunctions.js @@ -19,6 +19,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -81,8 +82,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -91,8 +91,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -118,8 +117,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -169,8 +167,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -241,6 +238,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Global.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Global.js index 16bf8ba0c..056293ff7 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Global.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.Global.js @@ -13,6 +13,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -42,8 +43,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -52,8 +52,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -79,8 +78,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -130,8 +128,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -202,6 +199,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.js index ea6c448ed..06f032555 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StaticProperties.js @@ -13,6 +13,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -42,8 +43,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -52,8 +52,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -79,8 +78,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -130,8 +128,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -202,6 +199,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js index 093694a68..d602d4e5a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringParameter.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -188,9 +185,7 @@ export async function createInstantiator(options, swift) { const TestModule = importObject["TestModule"] = importObject["TestModule"] || {}; TestModule["bjs_checkString"] = function bjs_checkString(aBytes, aCount) { try { - const bytesView = new Uint8Array(memory.buffer, aBytes, aCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(aBytes, aCount); imports.checkString(string); } catch (error) { setException(error); @@ -198,9 +193,7 @@ export async function createInstantiator(options, swift) { } TestModule["bjs_checkStringWithLength"] = function bjs_checkStringWithLength(aBytes, aCount, b) { try { - const bytesView = new Uint8Array(memory.buffer, aBytes, aCount); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(aBytes, aCount); imports.checkStringWithLength(string, b); } catch (error) { setException(error); @@ -211,6 +204,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.js index 49f07b5ef..b2241d6ca 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/StringReturn.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -200,6 +197,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.js index a74d49327..9acf70de2 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClass.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -225,6 +222,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js index f7103d56d..09eb39050 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosure.js @@ -38,6 +38,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -153,8 +154,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -163,8 +163,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -190,8 +189,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -241,8 +239,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -330,9 +327,7 @@ export async function createInstantiator(options, swift) { bjs["invoke_js_callback_TestModule_10TestModule5ThemeO_5ThemeO"] = function(callbackId, param0Bytes, param0Count) { try { const callback = swift.memory.getObject(callbackId); - const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(param0Bytes, param0Count); let ret = callback(string); tmpRetBytes = textEncoder.encode(ret); return tmpRetBytes.length; @@ -432,9 +427,7 @@ export async function createInstantiator(options, swift) { bjs["invoke_js_callback_TestModule_10TestModuleSS_SS"] = function(callbackId, param0Bytes, param0Count) { try { const callback = swift.memory.getObject(callbackId); - const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(param0Bytes, param0Count); let ret = callback(string); tmpRetBytes = textEncoder.encode(ret); return tmpRetBytes.length; @@ -582,9 +575,7 @@ export async function createInstantiator(options, swift) { const callback = swift.memory.getObject(callbackId); let optResult; if (param0IsSome) { - const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(param0Bytes, param0Count); optResult = string; } else { optResult = null; @@ -731,9 +722,7 @@ export async function createInstantiator(options, swift) { const callback = swift.memory.getObject(callbackId); let optResult; if (param0IsSome) { - const bytesView = new Uint8Array(memory.buffer, param0Bytes, param0Count); - const bytesToDecode = (typeof SharedArrayBuffer !== "undefined" && bytesView.buffer instanceof SharedArrayBuffer) ? bytesView.slice() : bytesView; - const string = textDecoder.decode(bytesToDecode); + const string = decodeString(param0Bytes, param0Count); optResult = string; } else { optResult = null; @@ -892,6 +881,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosureImports.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosureImports.js index b5efd2150..21e382395 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosureImports.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftClosureImports.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -63,8 +64,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -73,8 +73,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -100,8 +99,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -151,8 +149,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -264,6 +261,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStruct.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStruct.js index 617bda36b..cd2d396df 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStruct.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStruct.js @@ -13,6 +13,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -227,8 +228,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -237,8 +237,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -264,8 +263,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -364,8 +362,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -436,6 +433,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStructImports.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStructImports.js index d00b7450d..cb161707a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStructImports.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStructImports.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -49,8 +50,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -59,8 +59,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -86,8 +85,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -144,8 +142,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -219,6 +216,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.js index 2c415fc31..ae9625f00 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Throws.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -189,6 +186,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/UnsafePointer.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/UnsafePointer.js index c141a7926..169b001cf 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/UnsafePointer.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/UnsafePointer.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -54,8 +55,7 @@ export async function createInstantiator(options, swift) { bjs = {}; importObject["bjs"] = bjs; bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -64,8 +64,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -91,8 +90,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -149,8 +147,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -213,6 +210,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.js index f8737237e..cc6c0359b 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/VoidParameterVoidReturn.js @@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) { let instance; let memory; let setException; + let decodeString; const textDecoder = new TextDecoder("utf-8"); const textEncoder = new TextEncoder("utf-8"); let tmpRetString; @@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) { importObject["bjs"] = bjs; const imports = options.getImports(importsContext); bjs["swift_js_return_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { const source = swift.memory.getObject(sourceId); @@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) { bytes.set(source); } bjs["swift_js_make_js_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - return swift.memory.retain(textDecoder.decode(bytes)); + return swift.memory.retain(decodeString(ptr, len)); } bjs["swift_js_init_memory_with_result"] = function(ptr, len) { const target = new Uint8Array(memory.buffer, ptr, len); @@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) { f64Stack.push(v); } bjs["swift_js_push_string"] = function(ptr, len) { - const bytes = new Uint8Array(memory.buffer, ptr, len); - const value = textDecoder.decode(bytes); + const value = decodeString(ptr, len); strStack.push(value); } bjs["swift_js_pop_i32"] = function() { @@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) { if (isSome === 0) { tmpRetString = null; } else { - const bytes = new Uint8Array(memory.buffer, ptr, len); - tmpRetString = textDecoder.decode(bytes); + tmpRetString = decodeString(ptr, len); } } bjs["swift_js_return_optional_object"] = function(isSome, objectId) { @@ -198,6 +195,8 @@ export async function createInstantiator(options, swift) { instance = i; memory = instance.exports.memory; + decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); } + setException = (error) => { instance.exports._swift_js_exception.value = swift.memory.retain(error) } From 9ba7c107f0e3ecfa1eae181666ec16d140c6fe19 Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Thu, 5 Mar 2026 11:15:29 +0100 Subject: [PATCH 4/5] formatting --- Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift | 3 ++- Sources/JavaScriptKit/BridgeJSIntrinsics.swift | 12 +++++++++--- .../JSClosure+AsyncTests.swift | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift index a432e1ac0..1b7dce434 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift @@ -118,7 +118,8 @@ public struct ImportTS { self.context = context let liftingInfo = try returnType.liftingReturnInfo(context: context) needsReturnVariable = - !(returnType == .void || returnType.usesSideChannelForOptionalReturn() || liftingInfo.valueToLift == nil) + !(returnType == .void || returnType.usesSideChannelForOptionalReturn() + || liftingInfo.valueToLift == nil) } func lowerParameter(param: Parameter) throws { diff --git a/Sources/JavaScriptKit/BridgeJSIntrinsics.swift b/Sources/JavaScriptKit/BridgeJSIntrinsics.swift index ae8ee2de0..ffb64b9b8 100644 --- a/Sources/JavaScriptKit/BridgeJSIntrinsics.swift +++ b/Sources/JavaScriptKit/BridgeJSIntrinsics.swift @@ -732,7 +732,9 @@ extension _BridgedSwiftStruct { extension _BridgedSwiftEnumNoPayload where Self: RawRepresentable, RawValue == String { // MARK: ImportTS - @_spi(BridgeJS) public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32) -> T) -> T { rawValue.bridgeJSWithLoweredParameter(body) } + @_spi(BridgeJS) public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32) -> T) -> T { + rawValue.bridgeJSWithLoweredParameter(body) + } @_spi(BridgeJS) public static func bridgeJSLiftReturn(_ id: Int32) -> Self { Self(rawValue: .bridgeJSLiftReturn(id))! @@ -1502,7 +1504,9 @@ extension _BridgedAsOptional where Wrapped == Bool { } extension _BridgedAsOptional where Wrapped == String { - @_spi(BridgeJS) @_transparent public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32, Int32) -> T) -> T { + @_spi(BridgeJS) @_transparent public consuming func bridgeJSWithLoweredParameter( + _ body: (Int32, Int32, Int32) -> T + ) -> T { switch asOptional { case .none: return body(0, 0, 0) @@ -1683,7 +1687,9 @@ extension _BridgedAsOptional where Wrapped: _BridgedSwiftCaseEnum { extension _BridgedAsOptional where Wrapped: _BridgedSwiftEnumNoPayload, Wrapped: RawRepresentable, Wrapped.RawValue == String { - @_spi(BridgeJS) @_transparent public consuming func bridgeJSWithLoweredParameter(_ body: (Int32, Int32, Int32) -> T) -> T { + @_spi(BridgeJS) @_transparent public consuming func bridgeJSWithLoweredParameter( + _ body: (Int32, Int32, Int32) -> T + ) -> T { switch asOptional { case .none: return body(0, 0, 0) diff --git a/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift b/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift index db093e549..e3c19a8e4 100644 --- a/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift +++ b/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift @@ -72,7 +72,7 @@ class JSClosureAsyncTests: XCTestCase { )!.value() XCTAssertEqual(result, 42.0) } - + func testAsyncOneshotClosureWithPriority() async throws { let priority = UnsafeSendableBox(nil) let closure = JSOneshotClosure.async(priority: .high) { _ in @@ -83,7 +83,7 @@ class JSClosureAsyncTests: XCTestCase { XCTAssertEqual(result, 42.0) XCTAssertEqual(priority.value, .high) } - + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) func testAsyncOneshotClosureWithTaskExecutor() async throws { let executor = AnyTaskExecutor() @@ -93,7 +93,7 @@ class JSClosureAsyncTests: XCTestCase { let result = try await JSPromise(from: closure.function!())!.value() XCTAssertEqual(result, 42.0) } - + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) func testAsyncOneshotClosureWithTaskExecutorPreference() async throws { let executor = AnyTaskExecutor() From 6acb9e571cd2bd1ea0e8ba37fc848b85deb13076 Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Thu, 5 Mar 2026 11:19:13 +0100 Subject: [PATCH 5/5] format (with Swift 6.2) --- Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift b/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift index e3c19a8e4..db093e549 100644 --- a/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift +++ b/Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift @@ -72,7 +72,7 @@ class JSClosureAsyncTests: XCTestCase { )!.value() XCTAssertEqual(result, 42.0) } - + func testAsyncOneshotClosureWithPriority() async throws { let priority = UnsafeSendableBox(nil) let closure = JSOneshotClosure.async(priority: .high) { _ in @@ -83,7 +83,7 @@ class JSClosureAsyncTests: XCTestCase { XCTAssertEqual(result, 42.0) XCTAssertEqual(priority.value, .high) } - + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) func testAsyncOneshotClosureWithTaskExecutor() async throws { let executor = AnyTaskExecutor() @@ -93,7 +93,7 @@ class JSClosureAsyncTests: XCTestCase { let result = try await JSPromise(from: closure.function!())!.value() XCTAssertEqual(result, 42.0) } - + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) func testAsyncOneshotClosureWithTaskExecutorPreference() async throws { let executor = AnyTaskExecutor()