mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-01 23:59:41 +08:00
binfmt_misc hands a wasm module to kwwasi (kernel/guest/kwwasi.c), which connects to the host over AF_VSOCK port 1021 and serves the program's file I/O with its own descriptors, cwd and credentials. The host (js/core/wasi-exec.ts, serveWasiPrograms(), the CLI's --wasi) compiles the module once and runs each program in a worker of its own (js/core/wasi-program.ts) with WASI preview 1, or preview 0 adapted. Requests cross through a SharedArrayBuffer where one can be used, else with JSPI and messages. kwwasi sends a module only when the host has not compiled it for that file, resolves paths against the cwd or / by what exists, and maps open flags from their asm-generic values to the guest's. alpine.sh builds kwwasi and kw-binfmt.sh registers it; no image with them has been built yet, so test/wasi-binfmt.ts brings kwwasi in through a mount.
15 lines
584 B
TypeScript
15 lines
584 B
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// A guest WASI program's worker thread on Node (started by wasi.ts): run
|
|
// the program the first message brings (core/wasi-program.ts).
|
|
|
|
import { parentPort } from "node:worker_threads";
|
|
import { serveWasiProgram } from "../core/wasi-program.ts";
|
|
|
|
const port = parentPort;
|
|
if (!port) throw new Error("unkernel: a WASI program runs only in a worker thread (wasi.ts)");
|
|
serveWasiProgram({
|
|
post: (msg, transfer) => port.postMessage(msg, transfer),
|
|
listen: (handler) => port.on("message", handler),
|
|
});
|