Skip to content

Commit

Permalink
Merge pull request #67 from amesgen/console-stdout
Browse files Browse the repository at this point in the history
Add `ConsoleStdout`
  • Loading branch information
bjorn3 authored Jan 13, 2024
2 parents b18b2da + b150eba commit bea3870
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ npm install @bjorn3/browser_wasi_shim --save
```

```javascript
import { WASI, File, OpenFile, PreopenDirectory } from "@bjorn3/browser_wasi_shim";
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from "@bjorn3/browser_wasi_shim";

let args = ["bin", "arg1", "arg2"];
let env = ["FOO=bar"];
let fds = [
new OpenFile(new File([])), // stdin
new OpenFile(new File([])), // stdout
new OpenFile(new File([])), // stderr
ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
new PreopenDirectory(".", {
"example.c": new File(new TextEncoder("utf-8").encode(`#include "a"`)),
"hello.rs": new File(new TextEncoder("utf-8").encode(`fn main() { println!("Hello World!"); }`)),
Expand Down
52 changes: 52 additions & 0 deletions src/fs_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,55 @@ export class Directory {
return entry;
}
}

export class ConsoleStdout extends Fd {
write: (buffer: Uint8Array) => void;

constructor(write: (buffer: Uint8Array) => void) {
super();
this.write = write;
}

fd_filestat_get(): { ret: number; filestat: wasi.Filestat } {
const filestat = new wasi.Filestat(
wasi.FILETYPE_CHARACTER_DEVICE,
BigInt(0),
);
return { ret: 0, filestat };
}

fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } {
const fdstat = new wasi.Fdstat(wasi.FILETYPE_CHARACTER_DEVICE, 0);
fdstat.fs_rights_base = BigInt(wasi.RIGHTS_FD_WRITE);
return { ret: 0, fdstat };
}

fd_write(
view8: Uint8Array,
iovs: Array<wasi.Ciovec>,
): { ret: number; nwritten: number } {
let nwritten = 0;
for (const iovec of iovs) {
const buffer = view8.slice(iovec.buf, iovec.buf + iovec.buf_len);
this.write(buffer);
nwritten += iovec.buf_len;
}
return { ret: 0, nwritten };
}

static lineBuffered(write: (line: string) => void): ConsoleStdout {
const dec = new TextDecoder("utf-8", { fatal: false });
let line_buf = "";
return new ConsoleStdout((buffer) => {
line_buf += dec.decode(buffer, { stream: true });
const lines = line_buf.split("\n");
for (const [i, line] of lines.entries()) {
if (i < lines.length - 1) {
write(line);
} else {
line_buf = line;
}
}
});
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
OpenDirectory,
OpenSyncOPFSFile,
PreopenDirectory,
ConsoleStdout,
} from "./fs_core.js";
export { strace } from "./strace.js";
export * as wasi from "./wasi_defs.js";
32 changes: 1 addition & 31 deletions test/adapters/browser/run-test.html
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
<html>
<script type="module">
import { WASI, OpenFile, File, Fd, Directory, PreopenDirectory, wasi } from "/dist/index.js";
class ConsoleStdout extends Fd {
constructor(write) {
super();
this.write = write;
}

fd_filestat_get() {
const filestat = new wasi.Filestat(
wasi.FILETYPE_CHARACTER_DEVICE,
BigInt(0),
);
return { ret: 0, filestat };
}

fd_fdstat_get() {
const fdstat = new wasi.Fdstat(wasi.FILETYPE_CHARACTER_DEVICE, 0);
fdstat.fs_rights_base = BigInt(wasi.RIGHTS_FD_WRITE);
return { ret: 0, fdstat };
}

fd_write(view8, iovs) {
let nwritten = 0;
for (let iovec of iovs) {
let buffer = view8.slice(iovec.buf, iovec.buf + iovec.buf_len);
this.write(buffer);
nwritten += iovec.buf_len;
}
return { ret: 0, nwritten };
}
}
import { WASI, ConsoleStdout, OpenFile, File, Directory, PreopenDirectory } from "/dist/index.js";

async function derivePreopens(dirs) {
const rawPreopens = await window.bindingDerivePreopens(dirs)
Expand Down

0 comments on commit bea3870

Please sign in to comment.