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-worker.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-worker.js` 66 lines ยท 3594 bytes Raw Blame History 1 'use strict'; 2 /* Runs the compiled Omega kernel off the main thread. The page can stop this 3 worker at any time, so a kernel that loops for a long time never freezes the tab. 4 The worker only executes wasm it fetched itself: the prebuilt /lab/axpy.wasm or 5 the reply of the compile gateway. Messages carry Omega source text and numbers, 6 never code. The wasm has no imports: it cannot touch the page, the network or storage. */ 7 const API = '/aien/api/omega'; 8 const BLOCK = 999999; 9 let kernel = null; 10 function median(xs) { const s = xs.slice().sort((p, q) => p - q); return s[(s.length - 1) >> 1]; } 11 async function install(bytes) { 12 const module = await WebAssembly.compile(bytes); 13 const imports = WebAssembly.Module.imports(module).length; 14 if (imports) throw new Error(`This module asks for ${imports} imports; the lab runs only self-contained kernels.`); 15 kernel = await WebAssembly.instantiate(module, {}); 16 return { type: 'loaded', bytes, imports, exports: WebAssembly.Module.exports(module).map((e) => e.name) }; 17 } 18 async function compile(source) { 19 const t = performance.now(); 20 let reply; 21 try { 22 const response = await fetch(API, { method: 'POST', credentials: 'omit', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ source }) }); 23 reply = (response.headers.get('content-type') || '').includes('json') ? await response.json() : { ok: false, message: `HTTP ${response.status}` }; 24 } catch (err) { 25 reply = { ok: false, message: `The compile service is unreachable (${err.message}). The prebuilt kernel still runs.` }; 26 } 27 const trip = performance.now() - t; 28 if (!reply.ok) return { type: 'refused', trip, stage: reply.stage, code: reply.code, line: reply.line, message: reply.message, hint: reply.hint }; 29 const loaded = await install(Uint8Array.from(atob(reply.wasm), (c) => c.charCodeAt(0))); 30 return Object.assign(loaded, { trip, compileMs: reply.compile_ms, omc: reply.omc_sha256, irwasm: reply.irwasm_sha256 }); 31 } 32 function run(a, n) { 33 if (!kernel || typeof kernel.exports.run !== 'function') { 34 return { type: 'error', message: 'This module has no run export. Keep fn run a long lo long hi long to long.' }; 35 } 36 const go = kernel.exports.run; 37 const big = BigInt(a); 38 const wasmMs = []; 39 const jsMs = []; 40 let sum = 0n; 41 let ref = 0; 42 for (let rep = 0; rep < 3; rep++) { 43 let t = performance.now(); 44 let s = 0n; 45 for (let lo = 0; lo < n; lo += BLOCK) s += go(big, BigInt(lo), BigInt(Math.min(n, lo + BLOCK))); 46 wasmMs.push(performance.now() - t); 47 sum = s; 48 t = performance.now(); 49 let r = 0; 50 for (let i = 0; i < n; i++) r += a * i + (2 * i + 1); 51 jsMs.push(performance.now() - t); 52 ref = r; 53 } 54 return { type: 'ran', sum: sum.toString(), ref: BigInt(ref).toString(), wasmMs, jsMs, wasm: median(wasmMs), js: median(jsMs) }; 55 } 56 self.onmessage = async (event) => { 57 const job = event.data || {}; 58 try { 59 if (job.type === 'prebuilt') self.postMessage(await install(new Uint8Array(await (await fetch('/lab/axpy.wasm')).arrayBuffer()))); 60 else if (job.type === 'compile') self.postMessage(await compile(String(job.source))); 61 else if (job.type === 'run') self.postMessage(run(Math.max(1, Math.min(1000, Math.round(Number(job.a)) || 3)), Math.max(1, Math.min(`4194304`, Math.round(Number(job.n)) || 1)))); 62 } catch (err) { 63 const message = String((err && err.message) || err); 64 self.postMessage({ type: 'error', message: err instanceof WebAssembly.RuntimeError ? `The kernel trapped: ${message}. Omega stops on integer overflow instead of wrapping.` : message }); 65 } 66 };