abhishek
abhishek

Reputation: 880

Unable to use exported function from WebAssembly module?

The following JavaScript code is throwing the error 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 exports the doubleExp function.

Upvotes: 2

Views: 599

Answers (1)

JF Bastien
JF Bastien

Reputation: 6853

WebAssembly.instantiate is a promise. You're trying to use the WebAssembly.Instance that the promise returns when it completes. Something like

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

Upvotes: 1

Related Questions