I am trying to interface with WebAssembly and am getting a weird error. Firefox DevTools shows it as follows:
Uncaught (in promise) TypeError: can't convert 50057 to BigInt
The code that runs right before this error occurs:
static read_int(descriptor: number): number {
console.log("read_int")
if (descriptor < 0) {
return -1;
}
let value = Wasm.readStdValue(descriptor);
if (typeof value === 'number') {
if (Number.isInteger(value)) {
return value;
}
return Math.floor(value);
}
if (typeof value === 'boolean') {
return value ? 1 : 0;
}
if (typeof value === 'string') {
return parseInt(value);
}
return -1;
}
The WebAssembly files being used here were originally being used in a system interfacing with Swift and a library called WasmInterpreter, so the descriptor
is just a number which is a key in a map. The number being read at the let value = Wasm.readStdValue(descriptor);
line is 50057 and it is a number
. It doesn't seem right to me that it would fail to convert such a small number to a larger number type but idk.
Thanks in advance!
I guess you're calling a Wasm function that takes an i64
parameter. When Wasm expects an i64
, then JavaScript needs to provide a BigInt. Not even 0
will be converted to 0n
implicitly. You can use the BigInt
constructor as a conversion function, e.g. if (typeof value === 'number') { return BigInt(value); }
.
IOW, calling a Wasm function that takes an i64
has the same requirements as writing an element into a BigInt64Array
: Using a BigInt works, strings and even booleans will be converted implicitly (so those parts of your helper function are unnecessary), passing a Number throws a TypeError.
(Personally I agree that this is silly, but the spec is what it is.)