Skip to content

Commit be3b300

Browse files
authored
Merge pull request #757 from PassiveLogic/kr/optional-scalar-zero-literal
2 parents c40fc49 + 7aef830 commit be3b300

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3704,7 +3704,10 @@ extension BridgeType {
37043704
extension WasmCoreType {
37053705
fileprivate var placeholderValue: String {
37063706
switch self {
3707-
case .i32, .i64, .f32, .f64, .pointer: return "0"
3707+
// A Wasm `i64` return is a JavaScript `BigInt`, so the error-path placeholder
3708+
// must be a BigInt literal rather than a plain number.
3709+
case .i64: return "0n"
3710+
case .i32, .f32, .f64, .pointer: return "0"
37083711
}
37093712
}
37103713
}

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,10 @@ fileprivate extension WasmCoreType {
26032603
var jsZeroLiteral: String {
26042604
switch self {
26052605
case .f32, .f64: return "0.0"
2606-
case .i32, .i64, .pointer: return "0"
2606+
// A Wasm `i64` parameter is passed as a JavaScript `BigInt`, so its zero
2607+
// placeholder must be a BigInt literal rather than a plain number.
2608+
case .i64: return "0n"
2609+
case .i32, .pointer: return "0"
26072610
}
26082611
}
26092612
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ export async function createInstantiator(options, swift) {
440440
},
441441
roundTripOptionalFileSize: function bjs_roundTripOptionalFileSize(input) {
442442
const isSome = input != null;
443-
instance.exports.bjs_roundTripOptionalFileSize(+isSome, isSome ? input : 0);
443+
instance.exports.bjs_roundTripOptionalFileSize(+isSome, isSome ? input : 0n);
444444
const isSome1 = i32Stack.pop();
445445
let optResult;
446446
if (isSome1) {
@@ -488,7 +488,7 @@ export async function createInstantiator(options, swift) {
488488
},
489489
roundTripOptionalSessionId: function bjs_roundTripOptionalSessionId(input) {
490490
const isSome = input != null;
491-
instance.exports.bjs_roundTripOptionalSessionId(+isSome, isSome ? input : 0);
491+
instance.exports.bjs_roundTripOptionalSessionId(+isSome, isSome ? input : 0n);
492492
const isSome1 = i32Stack.pop();
493493
let optResult;
494494
if (isSome1) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export async function createInstantiator(options, swift) {
258258
return ret;
259259
} catch (error) {
260260
setException(error);
261-
return 0
261+
return 0n
262262
}
263263
}
264264
TestModule["bjs_roundTripUInt64"] = function bjs_roundTripUInt64(v) {
@@ -267,7 +267,7 @@ export async function createInstantiator(options, swift) {
267267
return ret;
268268
} catch (error) {
269269
setException(error);
270-
return 0
270+
return 0n
271271
}
272272
}
273273
},

Tests/BridgeJSRuntimeTests/JavaScript/OptionalSupportTests.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export function runJsOptionalSupportTests(rootExports) {
8080
assert.equal(exports.roundTripOptionalIntRawValueEnum(HttpStatus.Ok), HttpStatusValues.Ok);
8181
assert.equal(exports.roundTripOptionalInt64RawValueEnum(FileSize.Tiny), FileSizeValues.Tiny);
8282
assert.equal(exports.roundTripOptionalUInt64RawValueEnum(SessionId.Active), SessionIdValues.Active);
83+
// The `none` case lowers the i64/u64 placeholder as a BigInt (`0n`); a plain `0`
84+
// would throw "Cannot convert 0 to a BigInt" when calling the Wasm export.
85+
assert.equal(exports.roundTripOptionalInt64RawValueEnum(null), null);
86+
assert.equal(exports.roundTripOptionalUInt64RawValueEnum(null), null);
8387
assert.equal(exports.roundTripOptionalTSEnum(TSDirection.North), TSDirection.North);
8488
assert.equal(exports.roundTripOptionalTSStringEnum(TSTheme.Light), TSTheme.Light);
8589
assert.equal(exports.roundTripOptionalNamespacedEnum(Networking.API.Method.Get), Networking.API.Method.Get);

0 commit comments

Comments
 (0)