Module wit_component::metadata

source ·
Expand description

Definition for encoding of custom sections within core wasm modules of component-model related data.

When creating a component from a source language the high-level process for doing this is that code will be generated into the source language by wit-bindgen or a similar tool which will be compiled down to core wasm. The core wasm file is then fed into wit-component and a component is created. This means that the componentization process is decoupled from the binding generation process and intentionally affords for linking together libraries into the main core wasm module that import different interfaces.

The purpose of this module is to define an intermediate format to reside in a custom section in the core wasm output. This intermediate format is carried through the wasm linker through a custom section whose name starts with component-type. This custom section is created per-language-binding-generation and consumed by slurping up all the sections during the component creation process.

Currently the encoding of this custom section is itself a component. The component has a single export which is a component type which represents the world that was bound during bindings generation. This single export is used to decode back into a Resolve with a WIT representation.

Currently the component additionally has a custom section named wit-component-encoding (see CUSTOM_SECTION_NAME). This section is currently defined as 2 bytes:

  • The first byte is CURRENT_VERSION to help protect against future and past changes.
  • The second byte indicates the string encoding used for imports/exports as part of the bindings process. The mapping is defined by encode_string_encoding.

This means that the top-level encode function takes a Resolve, a WorldId, and a StringEncoding. Note that the top-level decode function is slightly difference because it’s taking all custom sections in a core wasm binary, possibly from multiple invocations of bindgen, and unioning them all together. This means that the output is a Bindgen which represents the union of all previous bindings.

The dual of encode is the decode_custom_section fucntion which decodes the three arguments originally passed to encode.

Structs

  • The result of decoding binding information from a WebAssembly binary.
  • Module-level metadata that’s specific to one core WebAssembly module. This is extracted with a Bindgen.

Functions

  • This function will parse the wasm binary given as input and return a Bindgen which extracts the custom sections describing component-level types from within the binary itself.
  • Creates a component-type* custom section to be decoded by decode above.