Commit 42a8bcf
committed
feat: MessagePort, MessageChannel, BroadcastChannel, and node:worker_threads
Adds HTML's messaging primitives - MessagePort, MessageChannel,
BroadcastChannel and MessageEvent, all lazy globals, so an app that never names
one pays nothing - a node:worker_threads module, and Worker plus the worker
global scope as real EventTargets.
The native core is Node's node_messaging design without libuv: an isolate-free
PortData (mutex-guarded queue, sibling-group entanglement) under a per-isolate
NativeMessagePort whose wake primitive is a coalesced EventLoop::PostInternal,
so a producer never takes a foreign isolate's Locker. Pairwise channels and
named broadcast groups share one SiblingGroup mechanism; the pairwise-vs-
broadcast close difference is a single guard, as in Node. Ports transfer
through postMessage (Worker.postMessage included) and structuredClone as
host-object tag 2: the index travels in the stream, the PortData out of band,
nothing is detached until the whole graph has written, and received ports are
constructed before ReadValue because no JS may run inside a read. A
transferred port carries its queued backlog and drains after adoption on a
later turn, per spec.
worker.onmessage and the worker scope's onmessage are HTML event-handler IDL
attributes now (defineEventHandler, position-fixed so a handler interleaves
with addEventListener registrations), and delivery dispatches real
MessageEvents with event.ports populated. A port starts on its first message
listener; receiveMessageOnPort does forced synchronous drains.
docs/worker-threads.md carries the full real-vs-shim table and every
documented deviation.
Fixed in passing:
- The worker error path forwarded twice. A scope onerror that throws now
replaces the error it was offered and reaches the parent once - in
CallWorkerScopeOnErrorHandle, in the entry-rejection reporter and in the
unhandled-rejection tracker alike - and a worker with no scope handler at
all still reaches the parent instead of dropping the error. Parent-side
delivery is a real cancelable ErrorEvent on the Worker EventTarget, so
worker.addEventListener("error") works in registration order; handled means
preventDefault() or a truthy onerror return. An error the Worker object
leaves unhandled is dispatched on the parent's global scope per HTML, and
logged if nothing handles it there.
- AbortSignal#onabort moved onto the shared defineEventHandler helper.
- EventLoop::Shutdown destroys the dropped lanes after releasing its mutex. A
dropped message carrying a transferred port sentinels the port's sibling,
which posts to that sibling's loop; when the sibling belonged to the isolate
shutting down, the post re-entered the held, non-recursive mutex.
- ConcurrentQueue::Terminate destroys dropped messages outside both locks and
a push racing it is turned away under the queue mutex, so ports and buffers
transferred to a worker terminated before its entry settled are released and
their siblings told.
Cross-runtime contract: the shared Workers suite pinned the double forward at
2 and expects 1 once Worker.prototype has an onmessage getter, which this
change gives it.1 parent 84fc2b6 commit 42a8bcf
41 files changed
Lines changed: 3948 additions & 306 deletions
File tree
- docs
- test-app
- app/src/main/assets/app
- tests
- messaging
- runtime
- src/main/cpp
- js
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
380 | 380 | | |
381 | 381 | | |
382 | 382 | | |
383 | | - | |
| 383 | + | |
384 | 384 | | |
385 | | - | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
386 | 389 | | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
391 | 394 | | |
392 | 395 | | |
393 | 396 | | |
| |||
411 | 414 | | |
412 | 415 | | |
413 | 416 | | |
| 417 | + | |
414 | 418 | | |
415 | 419 | | |
416 | 420 | | |
| |||
719 | 723 | | |
720 | 724 | | |
721 | 725 | | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
726 | | - | |
727 | | - | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
736 | | - | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
737 | 748 | | |
738 | 749 | | |
739 | 750 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
47 | | - | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | | - | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
0 commit comments