Skip to content

Commit 584d9d2

Browse files
authored
Implement stderr pwrite (#10)
* Implement stderr pwrite * Pin setup-zig until next ZLS release
1 parent 9f10793 commit 584d9d2

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
with:
2323
node-version: 22
2424

25-
- uses: mlugg/setup-zig@v2
25+
- uses: mlugg/setup-zig@v2.0.5
2626

2727
- name: Install binaryen
2828
run: |

src/utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-ignore
22
import zigTarGz from "../zig-out/zig.tar.gz?inline";
33
import { untar } from "@andrewbranch/untar.js";
4-
import { Directory, File } from "@bjorn3/browser_wasi_shim";
4+
import { Directory, File, ConsoleStdout, wasi as wasi_defs } from "@bjorn3/browser_wasi_shim";
55

66
export async function getLatestZigArchive() {
77
const ds = new DecompressionStream("gzip");
@@ -43,3 +43,14 @@ function convert(node: TreeNode): Directory {
4343
})
4444
)
4545
}
46+
47+
export function stderrOutput(): ConsoleStdout {
48+
const dec = new TextDecoder("utf-8", { fatal: false });
49+
const stderr = new ConsoleStdout((buffer) => {
50+
postMessage({ stderr: dec.decode(buffer, { stream: true }) });
51+
});
52+
stderr.fd_pwrite = (data, offset) => {
53+
return { ret: wasi_defs.ERRNO_SPIPE, nwritten: 0 };
54+
}
55+
return stderr;
56+
}

src/workers/runner.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
// Runs compiled Zig code
22

3-
import { WASI, PreopenDirectory, OpenFile, File, ConsoleStdout } from "@bjorn3/browser_wasi_shim";
4-
5-
function stderrOutput(): ConsoleStdout {
6-
const dec = new TextDecoder("utf-8", { fatal: false });
7-
return new ConsoleStdout((buffer) => {
8-
postMessage({ stderr: dec.decode(buffer, { stream: true }) });
9-
});
10-
}
3+
import { WASI, PreopenDirectory, OpenFile, File } from "@bjorn3/browser_wasi_shim";
4+
import { stderrOutput } from "../utils";
115

126
async function run(wasmData: Uint8Array) {
137
let args = ["main.wasm"];

src/workers/zig.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import { WASI, PreopenDirectory, Fd, File, ConsoleStdout, OpenFile, Inode } from "@bjorn3/browser_wasi_shim";
2-
import { getLatestZigArchive } from "../utils";
1+
import { WASI, PreopenDirectory, Fd, File, OpenFile, Inode } from "@bjorn3/browser_wasi_shim";
2+
import { getLatestZigArchive, stderrOutput } from "../utils";
33
// @ts-ignore
44
import zigWasm from "../../zig-out/bin/zig.wasm?url";
55

6-
function stderrOutput(): ConsoleStdout {
7-
const dec = new TextDecoder("utf-8", { fatal: false });
8-
return new ConsoleStdout((buffer) => {
9-
postMessage({ stderr: dec.decode(buffer, { stream: true }) });
10-
});
11-
}
12-
136
let currentlyRunning = false;
147
async function run(source: string) {
158
if (currentlyRunning) return;

0 commit comments

Comments
 (0)