AtlatestRepositorysigil-geometry
sigil-geometry / tree / harnessrun-wasm.mjs
1
// Boot a Sigil wasm32-wasi web build under Node the way sigil-web-app.js does2
// (sigil_wasm_init, then sigil_wasm_start) and print what the program writes.3
// A minimal hand-rolled WASI: stdout/stderr, clocks, random; no filesystem.4
import fs from "node:fs";6
const wasmPath = process.argv[2];7
if (!wasmPath || !fs.existsSync(wasmPath)) {8
console.error(`SETUP-FAILED: no wasm at ${wasmPath}`);9
process.exit(2);10
}11
const module = await WebAssembly.compile(fs.readFileSync(wasmPath));12
let instance = null;13
const decoder = new TextDecoder();14
const view = () => new DataView(instance.exports.memory.buffer);15
const u8 = () => new Uint8Array(instance.exports.memory.buffer);16
const known = {17
fd_write(fd, iovs, n, nwritten) {18
let written = 0;19
for (let i = 0; i < n; i++) {20
const ptr = view().getUint32(iovs + i * 8, true);21
const len = view().getUint32(iovs + i * 8 + 4, true);22
const text = decoder.decode(u8().subarray(ptr, ptr + len));23
(fd === 2 ? process.stderr : process.stdout).write(text);24
written += len;25
}26
if (nwritten) view().setUint32(nwritten, written, true);27
return 0;28
},29
proc_exit(code) { throw new Error(`WASI proc_exit(${code})`); },30
args_sizes_get(a, b) { view().setUint32(a, 0, true); view().setUint32(b, 0, true); return 0; },31
environ_sizes_get(a, b) { view().setUint32(a, 0, true); view().setUint32(b, 0, true); return 0; },32
clock_time_get(_id, _p, t) { view().setBigUint64(t, process.hrtime.bigint(), true); return 0; },33
random_get(ptr, len) { const b = u8(); for (let i = 0; i < len; i++) b[ptr + i] = (Math.random() * 256) | 0; return 0; },34
fd_fdstat_get(fd, s) { u8().fill(0, s, s + 24); view().setUint8(s, 2); return 0; },35
fd_filestat_get(fd, s) { u8().fill(0, s, s + 64); view().setUint8(s + 16, 2); return 0; },36
fd_prestat_get() { return 8; },37
fd_prestat_dir_name() { return 8; },38
path_open() { return 44; },39
};40
const imports = { wasi_snapshot_preview1: {} };41
for (const e of WebAssembly.Module.imports(module)) {42
if (e.kind !== "function") continue;43
imports[e.module] ??= {};44
imports[e.module][e.name] = (e.module === "wasi_snapshot_preview1" && known[e.name]) || (() => 0);45
}46
instance = await WebAssembly.instantiate(module, imports);47
const ex = instance.exports;48
if (typeof ex.sigil_wasm_native_start !== "function") {49
console.error("SETUP-FAILED: not a native-codegen wasm build (no sigil_wasm_native_start)");50
process.exit(2);51
}52
if (ex.sigil_wasm_init() !== 0) { console.error("SETUP-FAILED: sigil_wasm_init"); process.exit(2); }53
// A program that calls (exit n) reaches proc_exit, which throws above;54
// turn that back into the process exit status.55
let rc;56
try {57
rc = ex.sigil_wasm_start();58
} catch (e) {59
const m = /WASI proc_exit\((\d+)\)/.exec(String(e && e.message));60
if (!m) throw e;61
process.exit(Number(m[1]));62
}63
process.stderr.write(`sigil_wasm_start -> ${rc}\n`);64
process.exit(rc === 0 ? 0 : 1);