bytecodealliance / bytecodealliance/wasmtime

Allow missing exports when using `bindgen!`

Open
#11,538 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 18h
Merged PRs (30d)
126

Description

#### Feature

Extend `wasmtime::component::bindgen!` or `wasmtime::component::Linker` to allow input components to miss some of the exports from WIT interfaces.

#### Benefit

On my project I have a user-implemented wasm component that I compile and call functions from at runtime.
With `component::Linker` I haven't found a way to add "default" **exports**. For imports this can be achieved with `allow_shadowing` and `func_wrap`.

So I have to force my users to define every single export with default values event when they don't need it. This implies their code breaks every time I add another possible export, since they would have it missing.

Benefit in my case is backward compatibility when adding new exports in WIT and not having to define unnecessary defaults by guests.

#### Implementation

Currently `bindgen!` generates code that looks like this for each WIT export:

```rust
impl WorldName {
fn foo(&self) -> &Guest;
}
```

I would like to have something like this:

```rust
// panics if `foo` export isn't present in component
fn foo(&self) -> &Guest;

// `None` if `foo` export isn't present in component
fn get_foo(&self) -> Option<&Guest>;
```

To maintain backward compatibility, this feature could be configured in `bindgen!` parameters (i.e. making exports not link-time verified).

#### Alternatives

- If there's a way to link a default-component to be partially shadowed by real implementations, I'd like an example similar to the existing one for linking modules ([this](https://docs.wasmtime.dev/examples-c-linking.html)).

- Add `component::Linker::define_missing_exports_as_default_values` similar to [define_unknown_imports_as_default_values](https://docs.rs/wasmtime/latest/wasmtime/struct.Linker.html#method.define_unknown_imports_as_default_values)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.