diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 88a678a..831f55c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -22,7 +22,7 @@ jobs: with: node-version: 22 - - uses: mlugg/setup-zig@v2 + - uses: mlugg/setup-zig@v2.0.5 - name: Install binaryen run: | diff --git a/src/utils.ts b/src/utils.ts index 0874949..29348c5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ // @ts-ignore import zigTarGz from "../zig-out/zig.tar.gz?inline"; import { untar } from "@andrewbranch/untar.js"; -import { Directory, File } from "@bjorn3/browser_wasi_shim"; +import { Directory, File, ConsoleStdout, wasi as wasi_defs } from "@bjorn3/browser_wasi_shim"; export async function getLatestZigArchive() { const ds = new DecompressionStream("gzip"); @@ -43,3 +43,14 @@ function convert(node: TreeNode): Directory { }) ) } + +export function stderrOutput(): ConsoleStdout { + const dec = new TextDecoder("utf-8", { fatal: false }); + const stderr = new ConsoleStdout((buffer) => { + postMessage({ stderr: dec.decode(buffer, { stream: true }) }); + }); + stderr.fd_pwrite = (data, offset) => { + return { ret: wasi_defs.ERRNO_SPIPE, nwritten: 0 }; + } + return stderr; +} diff --git a/src/workers/runner.ts b/src/workers/runner.ts index a46b55e..52973c3 100644 --- a/src/workers/runner.ts +++ b/src/workers/runner.ts @@ -1,13 +1,7 @@ // Runs compiled Zig code -import { WASI, PreopenDirectory, OpenFile, File, ConsoleStdout } from "@bjorn3/browser_wasi_shim"; - -function stderrOutput(): ConsoleStdout { - const dec = new TextDecoder("utf-8", { fatal: false }); - return new ConsoleStdout((buffer) => { - postMessage({ stderr: dec.decode(buffer, { stream: true }) }); - }); -} +import { WASI, PreopenDirectory, OpenFile, File } from "@bjorn3/browser_wasi_shim"; +import { stderrOutput } from "../utils"; async function run(wasmData: Uint8Array) { let args = ["main.wasm"]; diff --git a/src/workers/zig.ts b/src/workers/zig.ts index 4c89afe..f4905c4 100644 --- a/src/workers/zig.ts +++ b/src/workers/zig.ts @@ -1,15 +1,8 @@ -import { WASI, PreopenDirectory, Fd, File, ConsoleStdout, OpenFile, Inode } from "@bjorn3/browser_wasi_shim"; -import { getLatestZigArchive } from "../utils"; +import { WASI, PreopenDirectory, Fd, File, OpenFile, Inode } from "@bjorn3/browser_wasi_shim"; +import { getLatestZigArchive, stderrOutput } from "../utils"; // @ts-ignore import zigWasm from "../../zig-out/bin/zig.wasm?url"; -function stderrOutput(): ConsoleStdout { - const dec = new TextDecoder("utf-8", { fatal: false }); - return new ConsoleStdout((buffer) => { - postMessage({ stderr: dec.decode(buffer, { stream: true }) }); - }); -} - let currentlyRunning = false; async function run(source: string) { if (currentlyRunning) return;