【问题标题】:Unable to use exported function from WebAssembly module?无法使用从 WebAssembly 模块导出的函数?
【发布时间】:2017-08-15 17:56:41
【问题描述】:

以下 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 函数。

【问题讨论】:

    标签: webassembly


    【解决方案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));
        })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2013-05-05
      • 2019-12-28
      • 2021-04-15
      • 1970-01-01
      • 2017-01-11
      • 2017-02-13
      相关资源
      最近更新 更多