Wasm module serialization: structured clone + dynamic worker loader support - #6997
Open
guybedford wants to merge 4 commits into
Open
Wasm module serialization: structured clone + dynamic worker loader support#6997guybedford wants to merge 4 commits into
guybedford wants to merge 4 commits into
Conversation
Adds WasmModuleObject to the V8 value subclass list so that jsg::V8Ref<v8::WasmModuleObject> can be used as a parameter and struct field type, along with an rtti mapping.
Implements the GetWasmModuleTransferId / GetWasmModuleFromId serializer delegate hooks, sharing compiled code across the clone via v8::CompiledWasmModule per the WebAssembly Web API serialization steps. Support is opt-in via Serializer::Options::supportWasmModules since compiled code can only be shared in-process: structuredClone() enables it, while RPC and storage serialization continue to throw DataCloneError at serialization time.
A module in the worker loader modules bag may now be provided as a
WebAssembly.Module directly (e.g. obtained via a source phase import),
either as the module value itself or as { wasm: module }. The compiled
code is shared with the loaded worker via v8::CompiledWasmModule rather
than recompiling the wire bytes, in both the legacy and new module
registries. Inside the loaded worker the module can be imported with
import source, matching the ESM phase imports proposal semantics.
This comment was marked as outdated.
This comment was marked as outdated.
2 similar comments
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
eslint's parser does not yet support import source syntax, so the loader test using it moves to a dedicated wd-test with lint disabled, following the module-imports-test precedent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This implements serialization of
WebAssembly.Modulein two places: structured clone, and the dynamic worker loader modules bag — allowing compiled Wasm modules to be passed between workers without recompilation, per the WebAssembly Web API serialization steps.The motivating flow is source phase imports into dynamic workers:
What was implemented:
jsg::Serializer/Deserializernow implement theGetWasmModuleTransferId/GetWasmModuleFromIddelegate hooks, carryingv8::CompiledWasmModules inReleased::wasmModules. Support is opt-in (Options::supportWasmModules) since compiled code can only be shared in-process:structuredClone()enables it; RPC and storage serialization continue to throwDataCloneErrorat serialize time (matching the spec'sforStoragebehavior).modulesbag accepts aWebAssembly.Moduleas a module value, or{ wasm: module }alongside the existing{ wasm: bytes }form.WorkerSource::WasmModulecarries an optionalv8::CompiledWasmModule; both the legacy and new module registries useFromCompiledModulewhen present (the new registry seeds its existing compile cache), so the loaded worker shares compiled code with zero recompilation.jsg::V8Ref<v8::WasmModuleObject>is now unwrappable, with rtti and TypeScript type updates (Record<string, string | WebAssembly.Module | WorkerLoaderModule>).Note that
structuredClone(wasmModule)previously threwDataCloneErrorand now succeeds. This is intentionally not gated behind a compat flag as it is purely additive spec-conforming behavior, matching browsers (same-agent-cluster cloning).Test coverage: end-to-end loader tests (source phase import → loader →
import source+ default import in the child, against both module registries, covering both accepted forms), and structured clone tests including instantiation of the clone and object identity deduplication.In future, dynamic
import()could work directly against these module objects as well, per the ESM Phase Imports proposal.