Skip to content

bindgen: Emit component type in generated code #13190

@lann

Description

@lann

There are cases where it would be helpful to have access to the component type associated with bindgen!ed code, like allowing component type checking without separate WIT bookkeeping or the creation of an engine/component/linker/store to test instantiation.

Guest wit-bindgen embeds this information in the component-type custom section for use by wasm-component-ld; I'd like to do something similar for wasmtime bindgen, roughly:

let encoded = wit_component::metadata::encode(
    resolve,
    world_id,
    wit_component::StringEncoding::UTF8,
    None,
)?;
uwriteln!(self.src, "pub const COMPONENT_TYPE: &[u8] = &{encoded:?};");

which would allow the host to reconstruct the component type:

let wit_component::metadata::Bindgen {
    resolve,
    world,
    ..
} = wit_component::metadata::decode(generated::bindings::COMPONENT_TYPE)?;

If there is a compelling reason for it this could be gated by a bindgen opt but the const will disappear if it doesn't get used so I'm not sure if that is necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions