Skip to content

Commit

Permalink
fix(js-runner): Monkeypatch fs_write in browser environment (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Sep 3, 2021
1 parent 35cd957 commit 330b56f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions js-runner/src/core/grain-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ let bindings;

if (__RUNNER_BROWSER) {
const wasmFs = new WasmFs();
const decoder = new TextDecoder("utf-8");
// Monkeypatching the writeSync for stdout/stderr printing
const originalWriteSync = wasmFs.fs.writeSync;
wasmFs.fs.writeSync = (fd, buf, offset, length, position) => {
if (fd === 1) {
console.log(decoder.decode(buf));
return;
}
if (fd === 2) {
console.error(decoder.decode(buf));
return;
}

originalWriteSync(fd, buf, offset, length, position);
};
bindings = {
...wasiBindings.default,
fs: wasmFs.fs,
Expand Down

0 comments on commit 330b56f

Please sign in to comment.