2017-08-15 60 views
2

下面的JavaScript代碼引發錯誤myMathModule.exports is undefined無法使用來自WebAssembly模塊的導出函數?

<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功能。

回答

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)); 
    }) 
});