// WAFER JS Loader - generated by wafer build --js // Loads and runs test_js.wasm in the browser. const WAFER = (() => { const CELL_SIZE = 4; const DATA_STACK_TOP = 0x1540; const SYSVAR_BASE = 0x0004; let outputCallback = (s) => { const el = document.getElementById('output'); if (el) el.textContent += s; else console.log(s); }; async function run(opts) { if (opts && opts.output) outputCallback = opts.output; const memory = new WebAssembly.Memory({ initial: 16 }); const dsp = new WebAssembly.Global({ value: 'i32', mutable: true }, 5440); const rsp = new WebAssembly.Global({ value: 'i32', mutable: true }, 9536); const fsp = new WebAssembly.Global({ value: 'i32', mutable: true }, 11584); const table = new WebAssembly.Table({ element: 'anyfunc', initial: 256 }); function emit(code) { outputCallback(String.fromCharCode(code)); } const importObject = { env: { emit, memory, dsp, rsp, fsp, table } }; // Register host functions const view = () => new DataView(memory.buffer); const pop = () => { const sp = dsp.value; const v = view().getInt32(sp, true); dsp.value = sp + CELL_SIZE; return v; }; const push = (v) => { const sp = dsp.value - CELL_SIZE; view().setInt32(sp, v, true); dsp.value = sp; }; table.set(84, new WebAssembly.Function({parameters:[], results:[]}, () => { const n = pop(); const base = view().getUint32(SYSVAR_BASE, true); outputCallback((base === 16 ? n.toString(16).toUpperCase() : n.toString()) + ' '); })); const response = await fetch('test_js.wasm'); const bytes = await response.arrayBuffer(); const { instance } = await WebAssembly.instantiate(bytes, importObject); if (instance.exports._start) { instance.exports._start(); } return instance; } return { run }; })();