diff --git a/Example/src/index.js b/Example/src/index.js index 9a77f576b..61e835d5b 100644 --- a/Example/src/index.js +++ b/Example/src/index.js @@ -6,6 +6,27 @@ import { WasmFs } from "@wasmer/wasmfs"; const swift = new SwiftRuntime(); // Instantiate a new WASI Instance const wasmFs = new WasmFs(); + +// Output stdout and stderr to console +const originalWriteSync = wasmFs.fs.writeSync; +wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => { + const text = new TextDecoder("utf-8").decode(buffer); + + // Filter out standalone "\n" added by every `print`, `console.log` + // always adds its own "\n" on top. + if (text !== "\n") { + switch (fd) { + case 1: + console.log(text); + break; + case 2: + console.error(text); + break; + } + } + return originalWriteSync(fd, buffer, offset, length, position); +}; + let wasi = new WASI({ args: [], env: {}, @@ -30,9 +51,5 @@ const startWasiTask = async () => { swift.setInstance(instance); // Start the WebAssembly WASI instance! wasi.start(instance); - - // Output what's inside of /dev/stdout! - const stdout = await wasmFs.getStdOut(); - console.log(stdout); }; startWasiTask();