Skip to content

Commit 0e35c39

Browse files
authored
BridgeJS: Fix syntax error in generated JS (#789)
1 parent 0717703 commit 0e35c39

7 files changed

Lines changed: 46 additions & 10 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,14 +2381,10 @@ struct IntrinsicJSFragment: Sendable {
23812381
if method.returnType == .void {
23822382
printer.write("\(callExpr);")
23832383
} else {
2384-
printer.write("const ret = \(callExpr);")
2385-
}
2386-
2387-
// Lift return value if needed
2388-
if method.returnType != .void {
23892384
let liftFragment = try IntrinsicJSFragment.liftReturn(type: method.returnType)
2390-
let liftArgs = liftFragment.parameters.isEmpty ? [] : ["ret"]
2391-
let lifted = try liftFragment.printCode(liftArgs, context)
2385+
let returnVariable = context.scope.variable("ret")
2386+
printer.write("const \(returnVariable) = \(callExpr);")
2387+
let lifted = try liftFragment.printCode([returnVariable], context)
23922388
if let liftedValue = lifted.first {
23932389
printer.write("return \(liftedValue);")
23942390
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/SwiftStruct.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ extension Vector2D {
7474
@JS func scaled(by factor: Double) -> Vector2D {
7575
return Vector2D(dx: dx * factor, dy: dy * factor)
7676
}
77+
78+
@JS func describe() -> String {
79+
return "Vector2D(\(dx), \(dy))"
80+
}
7781
}
7882

7983
extension DataPoint {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftStruct.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,23 @@
686686
"_0" : "Vector2D"
687687
}
688688
}
689+
},
690+
{
691+
"abiName" : "bjs_Vector2D_describe",
692+
"effects" : {
693+
"isAsync" : false,
694+
"isStatic" : false,
695+
"isThrows" : false
696+
},
697+
"name" : "describe",
698+
"parameters" : [
699+
700+
],
701+
"returnType" : {
702+
"string" : {
703+
704+
}
705+
}
689706
}
690707
],
691708
"name" : "Vector2D",

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftStruct.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,17 @@ public func _bjs_Vector2D_scaled(_ factor: Float64) -> Void {
525525
#endif
526526
}
527527

528+
@_expose(wasm, "bjs_Vector2D_describe")
529+
@_cdecl("bjs_Vector2D_describe")
530+
public func _bjs_Vector2D_describe() -> Void {
531+
#if arch(wasm32)
532+
let ret = Vector2D.bridgeJSLiftParameter().describe()
533+
return ret.bridgeJSLowerReturn()
534+
#else
535+
fatalError("Only available on WebAssembly")
536+
#endif
537+
}
538+
528539
@_expose(wasm, "bjs_roundtrip")
529540
@_cdecl("bjs_roundtrip")
530541
public func _bjs_roundtrip() -> Void {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export async function createInstantiator(options, swift) {
6767
}.bind(instance1);
6868
instance1.multiply = function(a, b) {
6969
structHelpers.MathOperations.lower(this);
70-
const ret = instance.exports.bjs_MathOperations_multiply(a, b);
71-
return ret;
70+
const ret1 = instance.exports.bjs_MathOperations_multiply(a, b);
71+
return ret1;
7272
}.bind(instance1);
7373
return instance1;
7474
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/SwiftStruct.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface Vector2D {
4848
dy: number;
4949
magnitude(): number;
5050
scaled(factor: number): Vector2D;
51+
describe(): string;
5152
}
5253
export type PrecisionObject = typeof PrecisionValues;
5354

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,17 @@ export async function createInstantiator(options, swift) {
237237
}.bind(instance1);
238238
instance1.scaled = function(factor) {
239239
structHelpers.Vector2D.lower(this);
240-
const ret = instance.exports.bjs_Vector2D_scaled(factor);
240+
const ret1 = instance.exports.bjs_Vector2D_scaled(factor);
241241
const structValue = structHelpers.Vector2D.lift();
242242
return structValue;
243243
}.bind(instance1);
244+
instance1.describe = function() {
245+
structHelpers.Vector2D.lower(this);
246+
const ret2 = instance.exports.bjs_Vector2D_describe();
247+
const ret3 = tmpRetString;
248+
tmpRetString = undefined;
249+
return ret3;
250+
}.bind(instance1);
244251
return instance1;
245252
}
246253
});

0 commit comments

Comments
 (0)