Phase 19: Web Workers#
Lay the low-level foundation for Web Workers in the js crate.
What to implement#
- Worker thread lifecycle: spawn a dedicated OS thread per
Worker, park it on a run-loop waiting for work, and shut it down cleanly onterminate(). - Message channel types:
MessagePort(a typed wrapper aroundstd::sync::mpsc) andMessageChannel(a pair of connected ports). Both ends must be sendable across threads. - WorkerHandle: an opaque struct held by the main thread that owns the send half of the channel and a
JoinHandle.terminate()drops the sender (closing the channel) then joins the thread. - Inbound message queue on the worker thread: the run-loop drains the channel, executing pending messages, and yields back to JS execution when idle.
- Thread-safe error propagation: worker-side panics/errors should be forwarded back to the main thread as an
ErrorRecordso the JS binding layer can fireonerror.
Acceptance criteria#
- A unit test spawns a worker thread, sends a string message, receives the echo on the main thread, then terminates cleanly.
- No
unsafeoutsideplatform/crypto/js(std thread primitives are fine). cargo test -p we-jspasses.