Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
5 changes: 5 additions & 0 deletions Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json
Original file line number Diff line number Diff line change
Expand Up @@ -13108,6 +13108,11 @@
}
},
{
"defaultValue" : {
"double" : {
"_0" : 5
}
},
"label" : "b",
"name" : "b",
"type" : {
Expand Down
2 changes: 1 addition & 1 deletion Tests/BridgeJSRuntimeTests/StructAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/prelude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down