WASM handles stdout and stderr (at last). #518
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
I'm not sure if it was a restriction of WASI-12 (maybe something around static initialisation), or if I just never expended the small amount of effort, but our WASM code has never been able to handle writing to stdout/stderr. It's basically why we had logstr.
This PR stubs the writes to stdout/stderr in the C++ code, such that the text is then forwarded to our logstr import.
This means that both bb.js and circuits benefit without needing to writing their own host imports.
An alternative would be to get rid of logstr, but its a simpler function that already exists, and saves us writing some fd_write imports in our TS code.
Bit of side note, I'll mention it here, but one caveat to this kind of wasi stubbing, is that we will need to rethink things a bit if we choose to run the WASM in a full WASI runtime. It's not an issue when we run tests in wasmtime, because we dont link in the "wasi" module where these stubs exist.
Eventually we maybe able to use something like wasmer in the browser to provide a full WASI runtime with threading. Word on the street is that they recently introduced pthread support. We would need to make sure we can also provide our get_data/set_data functions for handling memory swapping.