wiggle: choose between &mut self and &self (#5428)

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.
This commit is contained in:
Andrew Brown
2022-12-13 14:38:47 -08:00
committed by GitHub
parent df923f18ca
commit 3ce896f69d
6 changed files with 53 additions and 10 deletions

View File

@@ -77,6 +77,11 @@ fn _define_func(
function = #func_name
);
);
let ctx_type = if settings.mutable {
quote!(&'a mut)
} else {
quote!(&'a)
};
if settings.get_async(&module, &func).is_sync() {
let traced_body = if settings.tracing.enabled_for(&mod_name, &func_name) {
quote!(
@@ -91,8 +96,8 @@ fn _define_func(
(
quote!(
#[allow(unreachable_code)] // deals with warnings in noreturn functions
pub fn #ident(
ctx: &mut (impl #(#bounds)+*),
pub fn #ident<'a>(
ctx: #ctx_type (impl #(#bounds)+*),
memory: &dyn wiggle::GuestMemory,
#(#abi_params),*
) -> wiggle::anyhow::Result<#abi_ret> {
@@ -122,7 +127,7 @@ fn _define_func(
quote!(
#[allow(unreachable_code)] // deals with warnings in noreturn functions
pub fn #ident<'a>(
ctx: &'a mut (impl #(#bounds)+*),
ctx: #ctx_type (impl #(#bounds)+*),
memory: &'a dyn wiggle::GuestMemory,
#(#abi_params),*
) -> impl std::future::Future<Output = wiggle::anyhow::Result<#abi_ret>> + 'a {