wasm_exec.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <!doctype html>
  2. <!--
  3. Copyright 2018 The Go Authors. All rights reserved.
  4. Use of this source code is governed by a BSD-style
  5. license that can be found in the LICENSE file.
  6. -->
  7. <html>
  8. <head>
  9. <meta charset="utf-8">
  10. <title>Go wasm</title>
  11. </head>
  12. <body>
  13. <!--
  14. Add the following polyfill for Microsoft Edge 17/18 support:
  15. <script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
  16. (see https://caniuse.com/#feat=textencoder)
  17. -->
  18. <script src="wasm_exec.js"></script>
  19. <script>
  20. if (!WebAssembly.instantiateStreaming) { // polyfill
  21. WebAssembly.instantiateStreaming = async (resp, importObject) => {
  22. const source = await (await resp).arrayBuffer();
  23. return await WebAssembly.instantiate(source, importObject);
  24. };
  25. }
  26. const go = new Go();
  27. let mod, inst;
  28. WebAssembly.instantiateStreaming(fetch("test.wasm"), go.importObject).then((result) => {
  29. mod = result.module;
  30. inst = result.instance;
  31. document.getElementById("runButton").disabled = false;
  32. }).catch((err) => {
  33. console.error(err);
  34. });
  35. async function run() {
  36. console.clear();
  37. await go.run(inst);
  38. inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
  39. }
  40. </script>
  41. <button onClick="run();" id="runButton" disabled>Run</button>
  42. </body>
  43. </html>