-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathconsole.ts
135 lines (119 loc) · 4.08 KB
/
console.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Create a console printer that can be used as an overlay of WASI imports.
* See the example below for how to use it.
*
* ```javascript
* const imports = {
* "wasi_snapshot_preview1": wasi.wasiImport,
* }
* const printer = consolePrinter();
* printer.addToImports(imports);
*
* const instance = await WebAssembly.instantiate(module, imports);
* printer.setMemory(instance.exports.memory);
* ```
*
* Note that the `stdout` and `stderr` functions are called with text, not
* bytes. This means that bytes written to stdout/stderr will be decoded as
* UTF-8 and then passed to the `stdout`/`stderr` functions every time a write
* occurs without buffering.
*
* @param stdout A function that will be called when stdout is written to.
* Defaults to `console.log`.
* @param stderr A function that will be called when stderr is written to.
* Defaults to `console.warn`.
* @returns An object that can be used as an overlay of WASI imports.
*/
export function consolePrinter(
{
stdout,
stderr,
}: {
stdout: (str: string) => void;
stderr: (str: string) => void;
} = {
stdout: console.log,
stderr: console.warn,
},
) {
let memory: WebAssembly.Memory | undefined = undefined;
let _view: DataView | undefined = undefined;
function getMemoryView(): DataView {
if (typeof memory === "undefined") {
throw new Error("Memory is not set");
}
if (_view === undefined || _view.buffer.byteLength === 0) {
_view = new DataView(memory.buffer);
}
return _view;
}
const decoder = new TextDecoder();
return {
addToImports(imports: WebAssembly.Imports): void {
const wasiImport = imports.wasi_snapshot_preview1 as {
fd_write: (
fd: number,
iovs: number,
iovsLen: number,
nwritten: number,
) => number;
fd_filestat_get: (fd: number, filestat: number) => number;
fd_fdstat_get: (fd: number, fdstat: number) => number;
};
const original_fd_write = wasiImport.fd_write;
wasiImport.fd_write = (fd, iovs, iovsLen, nwritten): number => {
if (fd !== 1 && fd !== 2) {
return original_fd_write(fd, iovs, iovsLen, nwritten);
}
const view = getMemoryView();
const buffers = Array.from({ length: iovsLen }, (_, i) => {
const ptr = iovs + i * 8;
const buf = view.getUint32(ptr, true);
const bufLen = view.getUint32(ptr + 4, true);
return new Uint8Array(memory.buffer, buf, bufLen);
});
let written = 0;
let str = "";
for (const buffer of buffers) {
str += decoder.decode(buffer);
written += buffer.byteLength;
}
view.setUint32(nwritten, written, true);
const log = fd === 1 ? stdout : stderr;
log(str);
return 0;
};
const original_fd_filestat_get = wasiImport.fd_filestat_get;
wasiImport.fd_filestat_get = (fd, filestat): number => {
if (fd !== 1 && fd !== 2) {
return original_fd_filestat_get(fd, filestat);
}
const view = getMemoryView();
const result = original_fd_filestat_get(fd, filestat);
if (result !== 0) {
return result;
}
const filetypePtr = filestat + 0;
view.setUint8(filetypePtr, 2); // FILETYPE_CHARACTER_DEVICE
return 0;
};
const original_fd_fdstat_get = wasiImport.fd_fdstat_get;
wasiImport.fd_fdstat_get = (fd, fdstat): number => {
if (fd !== 1 && fd !== 2) {
return original_fd_fdstat_get(fd, fdstat);
}
const view = getMemoryView();
const fs_filetypePtr = fdstat + 0;
view.setUint8(fs_filetypePtr, 2); // FILETYPE_CHARACTER_DEVICE
const fs_rights_basePtr = fdstat + 8;
// See https://github.com/WebAssembly/WASI/blob/v0.2.0/legacy/preview1/docs.md#record-members
const RIGHTS_FD_WRITE = 1 << 6;
view.setBigUint64(fs_rights_basePtr, BigInt(RIGHTS_FD_WRITE), true);
return 0;
};
},
setMemory(m: WebAssembly.Memory) {
memory = m;
},
};
}