wiggle: choose between &mut self and &self#5428
Merged
Merged
Conversation
Previously, all Wiggle-generated traits were generated with `&mut self` signatures. With the addition of the `mutable` configuration option to `from_witx!` and `wasmtime_integration!`, one can disable this, emitting instead traits that use `&self` (i.e., `mutable: false`). This change is helpful for implementing wasi-threads: WASI implementations with interior mutability will now be able to communitcate this to their Wiggle-generated code. The other side of this change is the `get_cx` closure passed to Wiggle's generated `add_to_linker` function. When `mutability` is set to `true` (default), the `get_cx` function takes a `&mut` data structure from the store and returns a corresponding `&mut` reference, usually to a field of the passed-in structure. When `mutability: false`, the `get_cx` closure will still take a `&mut` data structure but now will return a `&` reference.
alexcrichton
approved these changes
Dec 13, 2022
abrown
added a commit
to abrown/wasmtime
that referenced
this pull request
Dec 20, 2022
After bytecodealliance#5236, it is now easier to modify the `wasi-common` implementations (both `preview0` and `preview1`) to use `&self` instead of `&mut self` in their function signatures. This makes it possible to access and use a `WasiCtx` from an `Arc<Host>` (the top-level Wasmtime CLI structure holding the WASI implementations). An `Arc<Host>` will only give out immutable access to the WASI implementations it contains. This relies on bytecodealliance#5428 to configure Wiggle to emit `&self`-receiving traits. This change also wraps `wasi-common`'s source of randomness in `WasiCtx` with a `Mutex` to enable this `&mut self` to `&self` refactoring. One remaining issue to figure out with this change is the interaction with the `async` side of wiggle. E.g., `cargo build --all-features -p wasmtime-wasi` will fail due to the `tokyo` feature being enabled. The errors have to do with incorrect `Send` and `Sync` bounds on various parts of the Wiggle-generated API. We had previously discussed turning off these `async` parts when the `wasi-threads` feature is enabled, or at least prevent these features from being enabled at the same time, since as-is even `cargo test --all` is affected.
abrown
added a commit
to abrown/wasmtime
that referenced
this pull request
Jan 6, 2023
After bytecodealliance#5236, it is now easier to modify the `wasi-common` implementations (both `preview0` and `preview1`) to use `&self` instead of `&mut self` in their function signatures. This makes it possible to access and use a `WasiCtx` from an `Arc<Host>` (the top-level Wasmtime CLI structure holding the WASI implementations). An `Arc<Host>` will only give out immutable access to the WASI implementations it contains. This relies on bytecodealliance#5428 to configure Wiggle to emit `&self`-receiving traits. This change also wraps `wasi-common`'s source of randomness in `WasiCtx` with a `Mutex` to enable this `&mut self` to `&self` refactoring. One remaining issue to figure out with this change is the interaction with the `async` side of wiggle. E.g., `cargo build --all-features -p wasmtime-wasi` will fail due to the `tokyo` feature being enabled. The errors have to do with incorrect `Send` and `Sync` bounds on various parts of the Wiggle-generated API. We had previously discussed turning off these `async` parts when the `wasi-threads` feature is enabled, or at least prevent these features from being enabled at the same time, since as-is even `cargo test --all` is affected.
abrown
added a commit
to abrown/wasmtime
that referenced
this pull request
Jan 9, 2023
After bytecodealliance#5236, it is now easier to modify the `wasi-common` implementations (both `preview0` and `preview1`) to use `&self` instead of `&mut self` in their function signatures. This makes it possible to access and use a `WasiCtx` from an `Arc<Host>` (the top-level Wasmtime CLI structure holding the WASI implementations). An `Arc<Host>` will only give out immutable access to the WASI implementations it contains. This relies on bytecodealliance#5428 to configure Wiggle to emit `&self`-receiving traits. This change also wraps `wasi-common`'s source of randomness in `WasiCtx` with a `Mutex` to enable this `&mut self` to `&self` refactoring. One remaining issue to figure out with this change is the interaction with the `async` side of wiggle. E.g., `cargo build --all-features -p wasmtime-wasi` will fail due to the `tokyo` feature being enabled. The errors have to do with incorrect `Send` and `Sync` bounds on various parts of the Wiggle-generated API. We had previously discussed turning off these `async` parts when the `wasi-threads` feature is enabled, or at least prevent these features from being enabled at the same time, since as-is even `cargo test --all` is affected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Previously, all Wiggle-generated traits were generated with
&mut selfsignatures. With the addition of themutableconfiguration option tofrom_witx!andwasmtime_integration!, one can disable this, emitting instead traits that use&self(i.e.,mutable: false). This change is helpful for implementing wasi-threads: WASI implementations with interior mutability will now be able to communitcate this to their Wiggle-generated code.The other side of this change is the
get_cxclosure passed to Wiggle's generatedadd_to_linkerfunction. Whenmutabilityis set totrue(default), theget_cxfunction takes a&mutdata structure from the store and returns a corresponding&mutreference, usually to a field of the passed-in structure. Whenmutability: false, theget_cxclosure will still take a&mutdata structure but now will return a&reference.