we (web engine): Experimental web browser project to understand the limits of Claude
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

WorkerGlobalScope and DedicatedWorkerGlobalScope #179

open opened by pierrelf.com

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:

  • WorkerGlobalScope base struct providing:

    • self — refers to the global itself
    • location — a WorkerLocation stub (href, protocol, host, pathname, search, hash — read-only snapshot of the URL passed to the Worker constructor)
    • navigatorWorkerNavigator stub (userAgent, hardwareConcurrency)
    • console — forward to the same Console implementation used on the main thread
    • Timer APIs: setTimeout, setInterval, clearTimeout, clearInterval (reuse crates/js/src/timers.rs)
    • fetch — reuse existing fetch implementation
    • atob / btoa
    • queueMicrotask
    • close() — terminates the worker (signals the run-loop to exit)
  • DedicatedWorkerGlobalScope extending the above:

    • postMessage(data, transfer?) — serialize via Structured Clone, send to the main thread channel
    • onmessage / onmessageerror — event handler properties
    • Fires a MessageEvent when a message arrives from the main thread
  • Worker script execution: after spawning the thread (from the previous issue), fetch the script URL (using the net crate), parse and execute it in a fresh JsContext whose global is a DedicatedWorkerGlobalScope.

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-js passes.
sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mlxpogbd6s2v