Skip to main content

inject_js_into_component

Function inject_js_into_component 

Source
pub fn inject_js_into_component(
    input: &Utf8Path,
    output: &Utf8Path,
    js_sources: &[&str],
) -> Result<()>
Expand description

Injects JavaScript source code into a compiled WASM component that was built with EmbeddingMode::BinarySlot.

This structurally rewrites the WASM component using wasmparser + wasm-encoder:

  1. Finds data segments containing 40-byte markers and records their module indices.
  2. Grows the core module’s memory to fit all new JS data segments.
  3. Adds new active data segments at the end of memory containing the JS for each module.
  4. Updates the DataCount section to account for the extra segments.

There is no capacity limit — each JS source can be any size.

The js_sources slice maps by position to marker MODULE_INDEX: the first entry is injected into the marker with MODULE_INDEX=0, the second into MODULE_INDEX=1, etc.