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.

importScripts() and SharedArrayBuffer/Atomics for Web Workers #181

open opened by pierrelf.com

Phase 19: Web Workers#

Complete the Web Workers implementation with script importing and shared memory primitives.

Part 1: importScripts()#

Implement importScripts(...urls) on DedicatedWorkerGlobalScope:

  • Accept one or more URL strings (resolved relative to the worker script URL)
  • Fetch each script synchronously (blocking the worker thread — this is spec-correct for importScripts)
  • Parse and execute each script in the current worker JsContext in order
  • Throw a NetworkError DOMException if any fetch fails; stop execution at that point

Part 2: SharedArrayBuffer#

  • Add SharedArrayBuffer to the JS built-ins in crates/js/src/builtins.rs
  • Backed by Arc<Vec<u8>> (shared ownership, no copy on postMessage)
  • new SharedArrayBuffer(length) — allocates zeroed bytes
  • byteLength property
  • Structured Clone serializes it by cloning the Arc (not the bytes), so both threads see the same memory

Part 3: Atomics#

Add an Atomics built-in object (non-constructable) with methods operating on integer TypedArray views over a SharedArrayBuffer:

  • Atomics.load(typedArray, index)
  • Atomics.store(typedArray, index, value)
  • Atomics.add, sub, and, or, xor, exchange, compareExchange
  • Atomics.wait(typedArray, index, value, timeout?) — block the calling thread (only valid in workers)
  • Atomics.notify(typedArray, index, count?) — wake waiting threads
  • Implement using std::sync::atomic (Ordering::SeqCst for correctness)

Test milestone#

A worker script uses importScripts('/lib.js') to load a utility library, then uses SharedArrayBuffer + Atomics to synchronize with the main thread. A unit test verifies that a counter incremented from a worker thread is visible on the main thread.

Acceptance criteria#

  • importScripts loads and executes external scripts in the worker context
  • SharedArrayBuffer is correctly transferred (Arc-cloned) via postMessage
  • Atomics.load/store/add produce correct results under concurrent access
  • Atomics.wait/Atomics.notify synchronize two threads correctly
  • 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/3mlxppbdyi32k