╭ script.js ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const grid = document.getElementById("grid"); 2 const pageJsonBase64 = document.getElementById("page").textContent; 3 4 let wasmInstance; 5 const decoder = new TextDecoder(); 6 let currentHtml = ""; 7 8 function readWasmString(ptr, len) { 9 const memory = wasmInstance.exports.memory; 10 return decoder.decode(new Uint8Array(memory.buffer, ptr, len)); 11 } 12 13 var importObject = { 14 env: { 15 _consoleLog: function(ptr, len) { 16 console.log(readWasmString(ptr, len)); 17 }, 18 _setHtml: function(ptr, len) { 19 const html = readWasmString(ptr, len); 20 if (html !== currentHtml) { 21 currentHtml = html; 22 grid.innerHTML = html; 23 } 24 }, 25 }, 26 }; 27 28 WebAssembly.instantiateStreaming(fetch("haxy.wasm"), importObject).then((result) => { 29 wasmInstance = result.instance; 30 31 // the page is embedded as base64 so it can sit inside the host html 32 // without worrying about characters that would terminate the script tag. 33 // decode here and hand the raw json bytes to the wasm. 34 const jsonBytes = Uint8Array.from(atob(pageJsonBase64), (c) => c.charCodeAt(0)); 35 const ptr = wasmInstance.exports._alloc(jsonBytes.length); 36 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, jsonBytes.length).set(jsonBytes); 37 wasmInstance.exports._start(ptr, jsonBytes.length); 38 39 document.addEventListener("keydown", (event) => { 40 if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "PageUp", "PageDown", "Home", "End"].includes(event.key)) { 41 event.preventDefault(); 42 } 43 wasmInstance.exports._onKeyDown(event.keyCode); 44 wasmInstance.exports._tick(); 45 }); 46 }); 47