bytecodealliance / bytecodealliance/wasmtime

Add embedder APIs for defining rec groups and types within rec groups

Open
#10,176 3 comments 0 reactions 0 assignees View on GitHub
wasm-proposal:gc wasmtime:api
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 16h
Merged PRs (30d)
135

Description

While we provide embedder APIs for creating one-off function/array/struct types, we don't currently have a way to define a set of types inside a rec group. This means the embedder cannot define a type that has a reference to itself, or any mutually recursive types. It also means that if a Wasm module uses such a type in its imports or exports, the embedder must find it and pluck it out via `Module::{imports,exports}` rather than simply create it themselves when they are trying to define functions that take/return those types.

This shouldn't be hard to implement, but it will require a bit of API design work that we want to make sure we are happy with, and that we think will extend well for Wasm's likely future type system extensions, before we commit to anything.

Random, half-baked thoughts:

* I think we will probably want to use builder-style APIs.
* Would be nice if the same Rust builder types could be reused for both defining types in a custom rec group and defining one-off types.
* Would be nice to re-implement our existing `StructType::new` et al Wasm type constructors in terms of these new builders.
* Representing a "forward reference to a type in this rec group I haven't defined yet" will be interesting. Especially since we need to eventually validate that the type did get defined.
* Rather than using rec-group-local indices (which makes sense at the wasm binary/validation level, but doesn't provide fantastic ergonomics at the embedder API level) we could perhaps try out an API similar to working with labels in a compiler, where you declare (but do not yet define) a type and can use that to define fields in Wasm structs and all that, and then later define the type and bind that definition to the earlier declaration. Finishing the rec group builder with unbound declarations would fail to validate and return an error.
* Perhaps, for API convenience and to consolidate validation, we should wait to validate everything until the whole rec group is finished (i.e. `RecGroupBuilder::finish` or whatever is called) rather than making ~every individual builder method fallible, since some things only can be validated when the rec group is complete.

Contributor guide

Open the contributing guide

Research direction

Start by reading the existing StructType::new and related one-off Wasm type constructors, then inspect Module::{imports,exports} to understand current embedder workflows. Compare builder-style options for RecGroupBuilder::finish, forward references, and deferred validation. Done means the API can define recursive and mutually recursive types while preserving a coherent path for existing one-off type construction.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.