以下JavaScript代码引发错误myMathModule.exports is undefined
:
<script>
fetch("test.wasm")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
var moduleBufferView = new Uint8Array(buffer);
var myMathModule = WebAssembly.instantiate(moduleBufferView);
for(var i = 0; i < 5; i++) {
console.log(myMathModule.exports.doubleExp(i));
}
});
</script>
test.wasm
导出doubleExp
函数。
答案 0 :(得分:1)
WebAssembly.instantiate
是一个承诺。您尝试使用承诺在完成时返回的WebAssembly.Instance
。像
fetch("test.wasm")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
var moduleBufferView = new Uint8Array(buffer);
WebAssembly.instantiate(moduleBufferView)
.then(function(instantiated) {
const instance = instantiated.instance;
console.log(instance.exports.doubleExp(i));
})
});