奇数(在承诺中)typeError:可以将50057转换为bigint”。 WebAssembly的错误

发布于 2025-02-09 07:17:39 字数 1033 浏览 1 评论 0原文

我正在尝试与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好多鱼好多余 2025-02-16 07:17:39

我想您正在调用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 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文