Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienos.com/blob/main/public/js/lab.js # aienos.com Source of www.aienos.com, served from my own hardware Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienos.com / public / js / lab.js` 155 lines ยท 7564 bytes Raw Blame History 1 'use strict'; 2 (() => { 3 /* /lab/: compile Omega on the shared gateway, run the wasm in a worker in this 4 tab, check its checksum against an independent reference. Every number here is measured in this 5 tab or read from /lab/record.tsv, which record.sh wrote on the Spark. */ 6 const $ = (id) => document.getElementById(id); 7 const src = $('lab-src'); 8 if (!src) return; 9 const ORIGINAL = src.value; 10 const LINE = 'ret add mul a x y'; 11 const API = '/aien/api/omega'; 12 const LIMIT_MS = 15000; 13 const fmt = (x) => Number(x).toLocaleString('en-US'); 14 const ms = (x) => `${x < 10 ? x.toFixed(2) : x < 100 ? x.toFixed(1) : Math.round(x)} ms`; 15 const state = { bytes: null, recorded: '', worker: null, timer: 0, label: 'prebuilt' }; 16 const status = (text, kind) => { 17 const box = $('lab-status'); 18 box.textContent = text; 19 box.dataset.kind = kind || ''; 20 }; 21 async function sha256(bytes) { 22 if (!crypto.subtle) return ''; 23 const d = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes)); 24 return Array.from(d, (b) => b.toString(16).padStart(2, '0')).join(''); 25 } 26 function hexdump(bytes) { 27 const rows = []; 28 for (let i = 0; i < Math.min(bytes.length, 1024); i += 16) { 29 const row = Array.from(bytes.subarray(i, i + 16), (b) => b.toString(16).padStart(2, '0')).join(' '); 30 rows.push(`${i.toString(16).padStart(4, '0')} ${row}`); 31 } 32 if (bytes.length > 1024) rows.push(`... ${fmt(bytes.length - 1024)} more bytes`); 33 return rows.join('\n'); 34 } 35 function worker() { 36 if (state.worker) return state.worker; 37 state.worker = new Worker('/js/lab-worker.js'); 38 return state.worker; 39 } 40 function stop() { 41 clearTimeout(state.timer); 42 if (state.worker) state.worker.terminate(); 43 state.worker = null; 44 state.bytes = null; 45 } 46 function ask(message) { 47 return new Promise((resolve) => { 48 const w = worker(); 49 state.timer = setTimeout(() => { 50 stop(); 51 resolve({ type: 'error', message: `Stopped after ${LIMIT_MS / 1000} s. The worker was ended, so this tab stays responsive.` }); 52 }, LIMIT_MS); 53 w.onmessage = (event) => { clearTimeout(state.timer); resolve(event.data); }; 54 w.onerror = (event) => { clearTimeout(state.timer); stop(); resolve({ type: 'error', message: event.message || 'worker failed' }); }; 55 w.postMessage(message); 56 }); 57 } 58 async function show(reply, label) { 59 if (reply.type !== 'loaded') { status(reply.message, 'fail'); return false; } 60 state.bytes = reply.bytes; 61 state.label = label; 62 const hash = await sha256(reply.bytes); 63 const same = hash && hash === state.recorded; 64 $('lab-wasm').textContent = hexdump(reply.bytes); 65 $('lab-wasm-meta').textContent = `${label}: ${fmt(reply.bytes.length)} bytes, ${reply.imports} imports, exports ${reply.exports.join(', ')}. sha256 ${hash || 'unavailable'}${same ? ', byte for byte the file record.sh built on the Spark' : ''}.`; 66 return true; 67 } 68 async function prebuilt() { 69 stop(); 70 return show(await ask({ type: 'prebuilt' }), 'prebuilt /lab/axpy.wasm'); 71 } 72 function choice() { 73 const a = Math.max(1, Math.min(1000, Math.round(Number($('lab-a').value) || 3))); 74 const n = Number($('lab-n').value); 75 $('lab-a').value = a; 76 return { a, n }; 77 } 78 async function run() { 79 if (!state.bytes && !(await prebuilt())) return; 80 const { a, n } = choice(); 81 status(`Running ${fmt(n)} elements three times in a worker...`, 'wait'); 82 $('lab-run').disabled = true; 83 const reply = await ask({ type: 'run', a, n }); 84 $('lab-run').disabled = false; 85 if (reply.type !== 'ran') { status(reply.message, 'fail'); return; } 86 const match = reply.sum === reply.ref; 87 $('lab-r-sum').textContent = fmt(reply.sum); 88 $('lab-r-wasm').textContent = `${ms(reply.wasm)} (${reply.wasmMs.map(ms).join(', ')})`; 89 $('lab-r-rate').textContent = `${fmt(Math.round(n / reply.wasm / 1000))} million elements per second`; 90 $('lab-verdict').textContent = match 91 ? `MATCH. ${fmt(n)} elements, a = ${a}, ${state.label}.` 92 : `MISMATCH. The kernel and the reference disagree. ${state.label}.`; 93 $('lab-verdict').dataset.kind = match ? 'pass' : 'fail'; 94 for (const cell of document.querySelectorAll('[data-live]')) { 95 const key = cell.dataset.live; 96 cell.textContent = key === 'n' ? fmt(n) : key === 'time' ? ms(reply.wasm) : match ? 'MATCH' : 'MISMATCH'; 97 } 98 status(match ? 'Checked: every element went into the checksum.' : 'The check caught a wrong kernel.', match ? 'pass' : 'fail'); 99 } 100 async function compile(label) { 101 const source = src.value; 102 status('Compiling on the gateway: omc check, IR, irwasm...', 'wait'); 103 $('lab-compile').disabled = true; 104 stop(); 105 const reply = await ask({ type: 'compile', source }); 106 $('lab-compile').disabled = false; 107 if (reply.trip !== undefined) $('lab-c-trip').textContent = ms(reply.trip); 108 if (reply.type === 'refused') { 109 const where = reply.line ? `line ${reply.line}: ` : ''; 110 status(`${reply.stage ? `${reply.stage} ` : ''}${reply.code ? `error ${reply.code} ` : ''}${where}${reply.message || 'compile failed'}${reply.hint ? `. ${reply.hint}` : ''}`, 'fail'); 111 if (reply.line) { 112 const lines = source.split('\n'); 113 const start = lines.slice(0, reply.line - 1).join('\n').length + (reply.line > 1 ? 1 : 0); 114 src.focus(); 115 src.setSelectionRange(start, start + (lines[reply.line - 1] || '').length); 116 } 117 return false; 118 } 119 if (reply.type === 'loaded') { 120 $('lab-c-server').textContent = `${fmt(reply.compileMs)} ms`; 121 $('lab-c-pin').textContent = `omc ${String(reply.omc || '').slice(0, 12)}, irwasm ${String(reply.irwasm || '').slice(0, 12)}`; 122 } 123 const ok = await show(reply, label || 'your compile'); 124 if (ok) await run(); 125 return ok; 126 } 127 async function start() { 128 $('lab-run').addEventListener('click', run); 129 $('lab-compile').addEventListener('click', () => compile('your compile')); 130 $('lab-break').addEventListener('click', () => { 131 if (!src.value.includes(LINE)) src.value = ORIGINAL; 132 src.value = src.value.replace(LINE, 'ret sub mul a x y'); 133 compile('broken on purpose: add changed to sub'); 134 }); 135 $('lab-reset').addEventListener('click', () => { src.value = ORIGINAL; status('Source reset. Compile or run.', ''); }); 136 const copy = $('lab-copy'); 137 if (copy) copy.addEventListener('click', async () => { 138 try { await navigator.clipboard.writeText($('lab-home-cmd').textContent); copy.textContent = 'Copied'; } catch { copy.textContent = 'Select and copy'; } 139 setTimeout(() => { copy.textContent = 'Copy'; }, 2000); 140 }); 141 try { 142 const tsv = await (await fetch('/lab/record.tsv', { cache: 'no-cache' })).text(); 143 const rec = Object.fromEntries(tsv.trim().split('\n').map((l) => l.split('\t'))); 144 state.recorded = rec.wasm_sha256 || ''; 145 for (const cell of document.querySelectorAll('[data-us]')) if (rec[cell.dataset.us]) cell.textContent = ms(Number(rec[cell.dataset.us]) / 1000); 146 for (const cell of document.querySelectorAll('[data-bytes]')) if (rec[cell.dataset.bytes]) cell.textContent = `${fmt(rec[cell.dataset.bytes])} bytes`; 147 } catch { /* the cells keep their link to record.tsv */ } 148 try { 149 if (await prebuilt()) status('Ready. Run uses the prebuilt kernel; Compile builds your edit on the gateway.', 'pass'); 150 } catch (err) { 151 status(`Could not load the prebuilt kernel: ${err.message}`, 'fail'); 152 } 153 } 154 start(); 155 })();