diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift index ce9d4bb67..499d6f60b 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift @@ -1834,7 +1834,7 @@ extension BridgeJSLink { let returnExpr = try thunkBuilder.call(abiName: function.abiName, returnType: function.returnType) let printer = CodeFragmentPrinter() - printer.write("\(function.name)(\(function.parameters.map { $0.name }.joined(separator: ", "))) {") + printer.write("\(function.name)(\(DefaultValueUtils.formatParameterList(function.parameters))) {") printer.indent { thunkBuilder.renderFunctionBody(into: printer, returnExpr: returnExpr) } @@ -1891,7 +1891,7 @@ extension BridgeJSLink { let methodPrinter = CodeFragmentPrinter() methodPrinter.write( - "\(method.name): function(\(method.parameters.map { $0.name }.joined(separator: ", "))) {" + "\(method.name): function(\(DefaultValueUtils.formatParameterList(method.parameters))) {" ) methodPrinter.indent { thunkBuilder.renderFunctionBody(into: methodPrinter, returnExpr: returnExpr) @@ -2932,8 +2932,12 @@ extension BridgeJSLink { printer.write("class \(klass.name) {") printer.indent { if let constructor = klass.constructor { + let paramSignatures = constructor.parameters.map { param in + let optional = param.hasDefault ? "?" : "" + return "\(param.name)\(optional): \(param.type.tsType)" + } let constructorSignature = - "constructor(\(constructor.parameters.map { "\($0.name): \($0.type.tsType)" }.joined(separator: ", ")));" + "constructor(\(paramSignatures.joined(separator: ", ")));" printer.write(constructorSignature) } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js index a865882ef..b514a50f8 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js @@ -612,7 +612,7 @@ export async function createInstantiator(options, swift) { const structValue = structHelpers.MathOperations.lift(); return structValue; }, - subtract: function(a, b) { + subtract: function(a, b = 5.0) { const ret = instance.exports.bjs_MathOperations_static_subtract(a, b); return ret; }, diff --git a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json index 2c2d3dc0f..8a66705fc 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json +++ b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json @@ -13108,6 +13108,11 @@ } }, { + "defaultValue" : { + "double" : { + "_0" : 5 + } + }, "label" : "b", "name" : "b", "type" : { diff --git a/Tests/BridgeJSRuntimeTests/StructAPIs.swift b/Tests/BridgeJSRuntimeTests/StructAPIs.swift index 6c0079dd1..0a05a517d 100644 --- a/Tests/BridgeJSRuntimeTests/StructAPIs.swift +++ b/Tests/BridgeJSRuntimeTests/StructAPIs.swift @@ -118,7 +118,7 @@ import JavaScriptKit return a * b } - @JS static func subtract(a: Double, b: Double) -> Double { + @JS static func subtract(a: Double, b: Double = 5.0) -> Double { return a - b } } diff --git a/Tests/prelude.mjs b/Tests/prelude.mjs index 86a5fe04f..9e32f1d26 100644 --- a/Tests/prelude.mjs +++ b/Tests/prelude.mjs @@ -975,6 +975,7 @@ function testStructSupport(exports) { assert.deepEqual(exports.roundTripAdvancedConfig(advancedConfig2), advancedConfig2); assert.equal(exports.MathOperations.subtract(10.0, 4.0), 6.0); + assert.equal(exports.MathOperations.subtract(10.0), 5.0); const mathOps = exports.MathOperations.init(); assert.equal(mathOps.baseValue, 0.0); assert.equal(mathOps.add(5.0, 3.0), 8.0);