github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/wasm/export/wasm.js (about)

     1  'use strict';
     2  
     3  const WASM_URL = 'wasm.wasm';
     4  
     5  var wasm;
     6  
     7  function updateResult() {
     8    wasm.exports.update();
     9  }
    10  
    11  function init() {
    12    document.querySelector('#a').oninput = updateResult;
    13    document.querySelector('#b').oninput = updateResult;
    14  
    15    const go = new Go();
    16    if ('instantiateStreaming' in WebAssembly) {
    17      WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject).then(function (obj) {
    18        wasm = obj.instance;
    19        go.run(wasm);
    20        updateResult();
    21      })
    22    } else {
    23      fetch(WASM_URL).then(resp =>
    24        resp.arrayBuffer()
    25      ).then(bytes =>
    26        WebAssembly.instantiate(bytes, go.importObject).then(function (obj) {
    27          wasm = obj.instance;
    28          go.run(wasm);
    29          updateResult();
    30        })
    31      )
    32    }
    33  }
    34  
    35  init();