Skip to content

Support WASI#43

Merged
jbourassa merged 4 commits intomainfrom
wasi
Nov 29, 2022
Merged

Support WASI#43
jbourassa merged 4 commits intomainfrom
wasi

Conversation

@jbourassa
Copy link
Copy Markdown
Collaborator

@jbourassa jbourassa commented Nov 24, 2022

Fixes #21.

Add APIs to support WASI:

  • Linker#define_wasi which... defines WASI for a linker. This is now a bool kwarg on Linker.new: Linker.new(engine, wasi: true)
  • WasiConfig: a builder object that holds a WASI configuration and does nothing else. The config can be changed at will -- even by mutating the underlying Ruby objects -- and and re-used across stores.
  • Store#configure_wasi to set the WASI context from WasiConfig. Once that's done, future changes to WasiConfig won't trickle to the store: the WASI state has been copied to the store. This is now a wasi_ctx keyword argument on Store.new
  • WasiExit: exception for WASI's proc_exit.

This more or less follows the pattern set by the C API, except:

  • No "inherit env" and "inherit args". I initially had it in, but it segfaulted when reading std::env::args(). I don't see a lot of value in that feature, it's ambiguous (should we use ARGV which can be mutated, or the original program's args?), and can easily be replicated by sending in ARGV / ENV.
  • In C, setting the WASI context is wasmtime_context_set_wasi(wasi_config_t). I chose Store#configure_wasi for symmetry with Linker#define_wasi. Also, configure_wasi materializes the Builder object into a WASI context, which can do file operation (opening files for stdin/out/err) -- not what I'd expect a "setter" to do.
  • On Linker#instantiate, we raise if the the store does not have a WASI context. This seems nicer than thread '<unnamed>' panicked at... in Ruby. I'm also not confident that catching the panic would leave the program in a good state (but probably?).

^ Feedback / thoughts welcome on everything, but on those last 2 points in particular.


In a future PR, I'll introduce reading writeable streams (stdout, stderr) as Ruby strings.

@jbourassa jbourassa force-pushed the wasi branch 3 times, most recently from 08d2c65 to f1b43b7 Compare November 24, 2022 23:47
use wasmtime::{AsContext, AsContextMut, Store as StoreImpl, StoreContext, StoreContextMut};
use wasmtime_wasi::{I32Exit, WasiCtx};

#[derive(Debug)]
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to drop this because WasiCtx does not implement Debug.

@jbourassa jbourassa marked this pull request as ready for review November 24, 2022 23:47
Add APIs to support WASI:
- `Linker#define_wasi` which... defines WASI for a linker.
- `WasiConfig`: a builder object that holds a WASI configuration
  and does nothing else. The config can be changed at will -- even by
  mutating the underlying Ruby objects -- and and re-used across
  stores.
- `Store#configure_wasi` to set the WASI context from `WasiConfig`.
  Once that's done, future changes to `WasiConfig` won't trickle to the
  store: the WASI state has been copied to the store.
- Introduce new exception for WASI's proc_exit.

This more or less follows the pattern set by the C API, except:
- No "inherit env" and "inherit args". I initially had it in,
  but it segfaulted when reading `std::env::args()`. I don't see a lot
  of value in that feature, it's ambiguous (should we use ARGV which can
  be mutated, or the original program's args?), and can easily be
  replicated by sending in `ARGV` / `ENV`.
- In C, it's `wasmtime_context_set_wasi(wasi_config_t)`. I chose
  `Store#configure_wasi` for symmetry with `Linker#define_wasi`. Also,
  `configure_wasi` materializes the Builder object into a WASI context, which
  can do file operation (opening files for stdin/out/err) -- not what
  I'd expect a "setter" to do.
- On `Linker#instantiate`, we raise if the the store does not have a
  WASI context. This seems nicer than "thread '<unnamed>' panicked at..."
  in Ruby. I'm also not confident that catching the panic would leave
  the program in a good state (but probably?).

----

In a future PR, I'll introduce reading writeable streams (stdout,
stderr) as Ruby strings.
@jbourassa jbourassa force-pushed the wasi branch 2 times, most recently from d4e3bb0 to 752ca63 Compare November 28, 2022 22:12
@jbourassa
Copy link
Copy Markdown
Collaborator Author

@saulecabrera I think all comments are addressed. I've made the changes as additional commits so they're easy to review. The simplest way to see the resulting API is the example or the tests. Let me know what you think!

- `WasiConfig` is now `WasiCtxBuilder`, mapping 1:1 to Wasmtime Rust
  API.
- `Linker#define_wasi` is now a bool kwarg on `.new`.
- `Store#configure_wasi` is now a bool kwarg on `.new`.
@jbourassa jbourassa merged commit f01583b into main Nov 29, 2022
@jbourassa jbourassa deleted the wasi branch November 29, 2022 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support WASI

2 participants