奇数(在承诺中)typeError:可以将50057转换为bigint”。 WebAssembly的错误
我正在尝试与WebAssembly进行交互,并遇到一个奇怪的错误。 Firefox DevTools显示如下:Untured(在承诺中)TypeError:无法将50057转换为Bigint
在此错误发生之前直接运行的代码:
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;
}
此处使用的WebAssembly文件最初是在与Swift和称为WasmInterPreter的库接口中使用的,因此Disciptror
只是一个数字,它是一个密钥在地图中。以LET value = wastm.ReadStdValue(Descriptor);
行读取的数字为50057,它是numbern
。对我来说,这似乎是不对的,它不会将这么小的数字转换为更大的数字类型,而是IDK。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您正在调用
i64
参数的WASM函数。当Wasm期望i64
时,JavaScript需要提供bigint。甚至0
将被隐式转换为0N
。您可以将bigint
构造函数用作转换函数,例如if(typeof value ==='number'){return bigint(value); }
。IOW,调用使用
i64
的WASM函数的要求与将元素写入bigint64array
:使用Bigint Works,字符串,弦乐甚至布尔值都会被隐式转换(因此,您的辅助功能的那些部分是不必要的),传递数字会抛出typeError。(我个人同意这很愚蠢,但规格就是它。)
I guess you're calling a Wasm function that takes an
i64
parameter. When Wasm expects ani64
, then JavaScript needs to provide a BigInt. Not even0
will be converted to0n
implicitly. You can use theBigInt
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 aBigInt64Array
: 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.)