【问题标题】:WebAssembly.instantiateStreaming requires an optional argumentWebAssembly.instantiateStreaming 需要一个可选参数
【发布时间】:2021-02-03 16:30:01
【问题描述】:

运行以下代码时出现Uncaught (in promise) TypeError: second argument must be an object 错误:

async function loadWasm(url) {
  const fetchPromise = fetch(url)
  const { module, instance } = await WebAssembly.instantiateStreaming(fetchPromise)
  // [...]
}
loadWasm('http://localhost:3000/path/to/file.wasm')

查看documentation,它说WebAssembly.instantiateStreaming (importObject) 的第二个参数是可选的,所以我不明白为什么浏览器仍然强制要求它?

我已经在 Firefox 78.7.0esr(64 位)和 Chrome 88.0.4324.96(官方版本)(64 位)中对此进行了测试。如果它很重要,代码会在 ruby​​ on rails 6.1 应用程序中使用 webpack 5 进行转译。

更新:

我打开an issue on mdn's github repository 认为文档可能需要更新。到目前为止,documentation is good 似乎是一个实现问题。待确认。

更新:

这既不是实现问题,也不是文档错误。我在下面的答案中发布了我对这个问题的结论。

【问题讨论】:

标签: javascript parameters webassembly


【解决方案1】:

https://github.com/WebAssembly/spec/issues/1284#issuecomment-772286062

仅当您要实例化的模块实际导入某些内容时才需要。

它在文档中被标记为可选,因为它并不总是需要,而不是因为我们可以选择是否提供它。 wasm 包决定了它是可选的还是必需的。

【讨论】:

    猜你喜欢
    • 2012-06-23
    • 2011-10-16
    • 2018-10-21
    • 1970-01-01
    • 2013-02-24
    • 2010-10-23
    • 1970-01-01
    • 2013-07-17
    相关资源
    最近更新 更多