Phase 19: Web Workers#
Implement the WHATWG Structured Clone algorithm so JS values can be safely transferred across worker thread boundaries via postMessage.
What to implement#
Add a structured_clone module in crates/js/src/:
- Serialization (
serialize(value: &JsValue) -> Result<SerializedData, CloneError>): walk the JS value graph and produce a self-contained, heap-allocation-free byte representation. Support:- Primitives: undefined, null, boolean, number, string, BigInt
- Plain objects and arrays (recursively)
- ArrayBuffer (copy bytes)
- TypedArrays (Uint8Array, Int32Array, Float64Array, etc.) and DataView
- Map, Set
- Date (serialize as milliseconds since epoch)
- Transferable placeholder for future ArrayBuffer transfer (mark as transferred, zero original)
- Deserialization (
deserialize(data: SerializedData, context: &mut JsContext) -> JsValue): reconstruct the value in the target context's heap. - Error handling: non-cloneable values (functions, DOM nodes, Symbols) return
CloneError::NotSerializablewhich maps to aDataCloneErrorJS exception.
Acceptance criteria#
- Round-trip test: serialize then deserialize a deeply nested object with arrays, numbers, strings, and an ArrayBuffer; assert structural equality.
- Functions and DOM nodes produce
CloneError::NotSerializable. cargo test -p we-jspasses.