Skip to main content

Module patch

Module patch 

Source
Expand description

The V8 module-namespace patch — a first-class, tested, public part of the build contract.

Each page’s wasm-bindgen-emitted JS imports * as import1 from the shared islands-core URL and passes that namespace directly to WebAssembly.instantiate as the importObject value. V8 rejects module-namespace objects there with Import #N "<url>": module is not an object or function. We patch the emitted JS so the namespace is spread into a plain prototype-less object — same shape, V8 accepts it. Skipping this is fatal: the page WASM does not load.

Under a content-hashing build, the hashing pass rewrites basenames inside every JS file (e.g. islands_core.jsislands_core.a1b2c3d4.js); the patch token PATCH_TOKEN MUST survive that rewrite, and the importObject key for the runtime is surgically restored to its unhashed form (see rewrite_basenames_in_js) so it matches the WASM’s compiled import-module name.

Constants§

PATCH_TOKEN
Literal substring present in every patched page JS. Build verification and the content-hash pass both assert this token is present (and survives hashing).
RUNTIME_IMPORT_URL
The runtime’s fixed, never-hashed import URL — baked into every page WASM’s import section by wasm-bindgen’s raw_module = "...".

Functions§

assert_patched
Loud guard against wasm-bindgen output-shape drift: assert a freshly emitted page JS actually carries the patch. If neither the patched token nor the raw needle is present, wasm-bindgen changed its emitted shape and [NEEDLE] must be updated — fail with a clear message instead of shipping a bundle V8 will reject at runtime.
patch_page_js
Apply the V8 namespace patch to a page’s emitted JS file, in place.
patch_page_source
In-memory analogue of patch_page_js for unit tests.
patch_runtime_snippets
Apply the snippet-namespace patch to the runtime’s own JS, in place.
rewrite_basenames_in_js
In-memory analogue of a content-hashing build’s basename rewrite: replace every original → hashed basename, then surgically restore the importObject KEY for the runtime to its unhashed form (so the WASM’s two-level import still resolves). Mirrors crate::hashing’s rewrite step.
wrap_snippet_namespace_imports
In-memory analogue of patch_runtime_snippets: wrap every snippet importObject value line. Idempotent; returns the source unchanged when there are no snippet imports (no inline_js, or already patched).