我正在尝试在新的 V8 Google Apps 脚本运行时上运行 WebAssembly,它似乎受支持,但异步函数似乎在返回 Promise 后被终止。
let wasm= new Uint8Array([/* snip */]).buffer
function add(a,b) {
return((async()=>{
console.log("running function...")
results=await WebAssembly.instantiate(wasm)
return results.instance.exports.add(a,b)
})());
}
function test(){
add(2,3).then(console.log).catch(console.error)
}
当我运行
test
时,会记录“正在运行的函数...”,然后什么也没有。没有错误,就没有结果。我已经确认 WebAssembly.instantiate
返回一个 Promise。
有谁知道发生了什么事,或者这是要问谷歌的事情吗?
V8 似乎还没有完全支持异步功能。实际上有一个关于此的“开放问题跟踪器”。您可以点击页面左上角的星标来跟踪此问题。 无论如何,请注意,
官方文档中没有明确说明 V8 中这些功能的可用性。它只是指出您可以在代码中使用像 async
这样的关键字,但它没有提到如果使用它您将获得什么功能。
参考:
async function testWasm() {
let bytes = new Uint8Array([0,97,115,109,1,0,0,0,1,7,1,96,2,127,127,1,127,3,2,1,0,7,7,1,3,97,100,100,0,0,10,9,1,7,0,32,0,32,1,106,11,0,28,4,110,97,109,101,1,6,1,0,3,97,100,100,2,13,1,0,2,0,3,108,104,115,1,3,114,104,115]);
let {
instance: {
exports: {
add
}
}
} = await WebAssembly.instantiate(bytes);
console.log(add(2,3));
}
async
一直到达顶层。
注意下面function add
之前的更改:
let wasm= new Uint8Array([/* snip */]).buffer
async function add(a,b) {
return((async()=>{
console.log("running function...")
results=await WebAssembly.instantiate(wasm)
return results.instance.exports.add(a,b)
})());
}
function test(){
add(2,3).then(console.log).catch(console.error)
}