Skip to content

Loosely integrate implements into bindgen!#13497

Open
alexcrichton wants to merge 1 commit into
bytecodealliance:mainfrom
alexcrichton:implements-bindgen
Open

Loosely integrate implements into bindgen!#13497
alexcrichton wants to merge 1 commit into
bytecodealliance:mainfrom
alexcrichton:implements-bindgen

Conversation

@alexcrichton
Copy link
Copy Markdown
Member

This commit is the next step in fleshing out #12698 and following in the footsteps of #13361 to integrate the (implements ...) directive into bindgen!. Notably this world:

world foo {
    import a: thing;
    import b: thing;
}

will only generate a single trait for thing::Host as opposed to the previous 2 traits that were generated. This enables hosts to use the same trait implementation for both, or customize as appropriate.

Additionally the add_to_linker-generated methods for interfaces are now refactored to additionally have an add_to_linker_instance entrypoint. This new entrypoint takes a LinkerInstance instead of a Linker and fills in the provided instance directly. This is in-turn used to fill out a linker for the foo world above.

Notably, however, embedders can call add_to_linker_instance with distinct closures accessing different parts of T, the store's state, meaning that it's possible to have two distinct implementations for a and b above.

Many golden tests were updated with this new add_to_linker_instance method, and codegen/runtime tests were added for a simple world as well showcasing the bindings modes that are possible. The (rarely used) top-level world add_to_linker only uses a single trait implementation, but invoking add_to_linker_instance enables passing in two separate implementations.

This commit is the next step in fleshing out bytecodealliance#12698 and following in the
footsteps of bytecodealliance#13361 to integrate the `(implements ...)` directive into
`bindgen!`. Notably this world:

    world foo {
        import a: thing;
        import b: thing;
    }

will only generate a single trait for `thing::Host` as opposed to the
previous 2 traits that were generated. This enables hosts to use the
same trait implementation for both, or customize as appropriate.

Additionally the `add_to_linker`-generated methods for interfaces are
now refactored to additionally have an `add_to_linker_instance`
entrypoint. This new entrypoint takes a `LinkerInstance` instead of a
`Linker` and fills in the provided instance directly. This is in-turn
used to fill out a linker for the `foo` world above.

Notably, however, embedders can call `add_to_linker_instance` with
distinct closures accessing different parts of `T`, the store's state,
meaning that it's possible to have two distinct implementations for `a`
and `b` above.

Many golden tests were updated with this new `add_to_linker_instance`
method, and codegen/runtime tests were added for a simple world as well
showcasing the bindings modes that are possible. The (rarely used)
top-level world `add_to_linker` only uses a single trait implementation,
but invoking `add_to_linker_instance` enables passing in two separate
implementations.
@alexcrichton alexcrichton requested a review from a team as a code owner May 27, 2026 17:46
@alexcrichton alexcrichton requested review from dicej and removed request for a team May 27, 2026 17:46
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.

1 participant