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

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