Phase 19: Web Workers#
Implement the global object for dedicated worker threads, mirroring how Window is the global for the main thread.
What to implement#
Add crates/js/src/worker_global.rs:
-
WorkerGlobalScopebase struct providing:self— refers to the global itselflocation— aWorkerLocationstub (href, protocol, host, pathname, search, hash — read-only snapshot of the URL passed to the Worker constructor)navigator—WorkerNavigatorstub (userAgent,hardwareConcurrency)console— forward to the same Console implementation used on the main thread- Timer APIs:
setTimeout,setInterval,clearTimeout,clearInterval(reusecrates/js/src/timers.rs) fetch— reuse existing fetch implementationatob/btoaqueueMicrotaskclose()— terminates the worker (signals the run-loop to exit)
-
DedicatedWorkerGlobalScopeextending the above:postMessage(data, transfer?)— serialize via Structured Clone, send to the main thread channelonmessage/onmessageerror— event handler properties- Fires a
MessageEventwhen a message arrives from the main thread
-
Worker script execution: after spawning the thread (from the previous issue), fetch the script URL (using the
netcrate), parse and execute it in a freshJsContextwhose global is aDedicatedWorkerGlobalScope.
Acceptance criteria#
- Worker script can call
self.postMessage('hello')and the value arrives on the main thread. close()inside a worker terminates the thread cleanly.- Timer callbacks fire inside the worker run-loop.
cargo test -p we-jspasses.