Skip to content

Commit 97933a2

Browse files
Merge pull request #669 from PassiveLogic/kr/fix-static-method-default-values
2 parents c47beee + c702ac2 commit 97933a2

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ extension BridgeJSLink {
18341834
let returnExpr = try thunkBuilder.call(abiName: function.abiName, returnType: function.returnType)
18351835

18361836
let printer = CodeFragmentPrinter()
1837-
printer.write("\(function.name)(\(function.parameters.map { $0.name }.joined(separator: ", "))) {")
1837+
printer.write("\(function.name)(\(DefaultValueUtils.formatParameterList(function.parameters))) {")
18381838
printer.indent {
18391839
thunkBuilder.renderFunctionBody(into: printer, returnExpr: returnExpr)
18401840
}
@@ -1891,7 +1891,7 @@ extension BridgeJSLink {
18911891

18921892
let methodPrinter = CodeFragmentPrinter()
18931893
methodPrinter.write(
1894-
"\(method.name): function(\(method.parameters.map { $0.name }.joined(separator: ", "))) {"
1894+
"\(method.name): function(\(DefaultValueUtils.formatParameterList(method.parameters))) {"
18951895
)
18961896
methodPrinter.indent {
18971897
thunkBuilder.renderFunctionBody(into: methodPrinter, returnExpr: returnExpr)
@@ -2932,8 +2932,12 @@ extension BridgeJSLink {
29322932
printer.write("class \(klass.name) {")
29332933
printer.indent {
29342934
if let constructor = klass.constructor {
2935+
let paramSignatures = constructor.parameters.map { param in
2936+
let optional = param.hasDefault ? "?" : ""
2937+
return "\(param.name)\(optional): \(param.type.tsType)"
2938+
}
29352939
let constructorSignature =
2936-
"constructor(\(constructor.parameters.map { "\($0.name): \($0.type.tsType)" }.joined(separator: ", ")));"
2940+
"constructor(\(paramSignatures.joined(separator: ", ")));"
29372941
printer.write(constructorSignature)
29382942
}
29392943

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ export async function createInstantiator(options, swift) {
612612
const structValue = structHelpers.MathOperations.lift();
613613
return structValue;
614614
},
615-
subtract: function(a, b) {
615+
subtract: function(a, b = 5.0) {
616616
const ret = instance.exports.bjs_MathOperations_static_subtract(a, b);
617617
return ret;
618618
},

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13108,6 +13108,11 @@
1310813108
}
1310913109
},
1311013110
{
13111+
"defaultValue" : {
13112+
"double" : {
13113+
"_0" : 5
13114+
}
13115+
},
1311113116
"label" : "b",
1311213117
"name" : "b",
1311313118
"type" : {

Tests/BridgeJSRuntimeTests/StructAPIs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ import JavaScriptKit
118118
return a * b
119119
}
120120

121-
@JS static func subtract(a: Double, b: Double) -> Double {
121+
@JS static func subtract(a: Double, b: Double = 5.0) -> Double {
122122
return a - b
123123
}
124124
}

Tests/prelude.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ function testStructSupport(exports) {
975975
assert.deepEqual(exports.roundTripAdvancedConfig(advancedConfig2), advancedConfig2);
976976

977977
assert.equal(exports.MathOperations.subtract(10.0, 4.0), 6.0);
978+
assert.equal(exports.MathOperations.subtract(10.0), 5.0);
978979
const mathOps = exports.MathOperations.init();
979980
assert.equal(mathOps.baseValue, 0.0);
980981
assert.equal(mathOps.add(5.0, 3.0), 8.0);

0 commit comments

Comments
 (0)