Skip to content

Commit bfda8b3

Browse files
committed
[TS] Fix fd_write to match the ABI, quiet environ call logging
We need to return the bytes written via an extra argument that we were leaving potentially-uninitialized.
1 parent a37d4e6 commit bfda8b3

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

typescript_strings.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
4646
var getRandomValues: Function;
4747
4848
imports.wasi_snapshot_preview1 = {
49-
"fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => {
49+
"fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number, bytes_written_ptr: number) => {
5050
// This should generally only be used to print panic messages
51-
console.log("FD_WRITE to " + fd + " in " + iovec_array_len + " chunks.");
5251
const ptr_len_view = new Uint32Array(wasm.memory.buffer, iovec_array_ptr, iovec_array_len * 2);
52+
var bytes_written = 0;
5353
for (var i = 0; i < iovec_array_len; i++) {
5454
const bytes_view = new Uint8Array(wasm.memory.buffer, ptr_len_view[i*2], ptr_len_view[i*2+1]);
55-
console.log(String.fromCharCode(...bytes_view));
55+
console.log("[fd " + fd + "]: " + String.fromCharCode(...bytes_view));
56+
bytes_written += ptr_len_view[i*2+1];
5657
}
58+
const written_view = new Uint32Array(wasm.memory.buffer, bytes_written_ptr, 1);
59+
written_view[0] = bytes_written;
5760
return 0;
5861
},
5962
"fd_close": (_fd: number) => {
@@ -73,15 +76,15 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
7376
},
7477
"environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => {
7578
// This is called before fd_write to format + print panic messages
76-
console.log("wasi_snapshot_preview1:environ_sizes_get");
7779
const out_count_view = new Uint32Array(wasm.memory.buffer, environ_var_count_ptr, 1);
7880
out_count_view[0] = 0;
7981
const out_len_view = new Uint32Array(wasm.memory.buffer, environ_len_ptr, 1);
8082
out_len_view[0] = 0;
8183
return 0;
8284
},
8385
"environ_get": (environ_ptr: number, environ_buf_ptr: number) => {
84-
// This is called before fd_write to format + print panic messages
86+
// This is called before fd_write to format + print panic messages,
87+
// but only if we have variables in environ_sizes_get, so shouldn't ever actually happen!
8588
console.log("wasi_snapshot_preview1:environ_get");
8689
return 58; // Note supported - we said there were 0 environment entries!
8790
},

0 commit comments

Comments
 (0)