feat(worker_threads): compiled main-thread receiveMessageOnPort drain + MessageChannel messageerror (#1077)#1253
Merged
Merged
Conversation
… + MessageChannel messageerror (#1077) Closes the two compiled-mode gaps documented during epic #996 for the main-thread MessageChannel/$MessagePort path (the worker-driven paths were already dual-mode). 1. Synchronous receiveMessageOnPort (#1000 follow-up) $Runtime.WorkerThreadsReceiveMessageOnPort was an unconditional undefined stub because $MessagePort is emitted (EmitMessageChannelTypes) AFTER EmitRuntimeClass, so the helper could not reference the port queue at emit time. Split it: define the method during EmitWorkerThreadsModuleHelpers (so callers bind the token) and emit its BODY afterward via a new EmitWorkerThreadsReceiveMessageOnPortBody, wired right after EmitMessageChannelTypes and before EmitRuntimeClassFinalize. The body drains one message from the port's own _pending queue and returns { message }, else undefined - matching SharpTSMessagePort.ReceiveMessageSync. 2. messageerror on postMessage of an uncloneable value (#1001 follow-up) The emitted structured clone (cloneCore) returns uncloneable values by reference and never throws, so a compiled $MessagePort had no clone-failure point. $MessagePort.PostMessage now detects the uncloneable categories up front (typeof "function"/"symbol") and enqueues a shared static _cloneError sentinel; Drain compares by reference and emits 'messageerror' (no args) instead of 'message'. This mirrors the interpreter's receiver-side model (ClonedMessage IsError: true) without touching the shared cloneCore, so structuredClone/$BroadcastChannel are unaffected. Both paths stay fully standalone (pure emitted IL, no SharpTS.dll dependency; --standalone --verify clean). Deeply nested uncloneables remain aliased, consistent with the emitted clone's existing shallow fallback. Tests: ReceiveMessageOnPort_QueuedMessage_ReturnsMessageThenUndefined (new, dual-mode) and MessageChannelPort_PostUncloneable_FiresMessageError (was interpreter-only Fact -> dual-mode Theory). xUnit 15428/0, Test262 22/0/4skip, TSConf 31/0.
This was referenced Jul 5, 2026
Owner
Author
|
Filed the two pre-existing divergences surfaced during this work as follow-ups:
Both are out of scope for #1077 and left as-is here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1077.
Fixes the two compiled-mode gaps documented during epic #996 for the main-thread
MessageChannel/$MessagePortpath. The worker-driven paths were already dual-mode; this brings the interpreter-onlyMessageChannelcases to parity.1. Synchronous
receiveMessageOnPort(from #1000)$Runtime.WorkerThreadsReceiveMessageOnPortwas an unconditionalundefinedstub:$MessagePortis emitted byEmitMessageChannelTypesafterEmitRuntimeClass, so the helper couldn't reference the port's_pendingqueue at emit time.Fix: the method is still defined during
EmitWorkerThreadsModuleHelpers(so call sites bind the token), but its body is now emitted afterward by a newEmitWorkerThreadsReceiveMessageOnPortBody, wired in right afterEmitMessageChannelTypesand beforeEmitRuntimeClassFinalize(which closes$Runtime). The body synchronously dequeues one message from the port's own_pendingqueue and returns{ message }(aDictionary<string, object?>), elseundefined— matchingSharpTSMessagePort.ReceiveMessageSync.2.
messageerroronpostMessageof an uncloneable value (from #1001)The emitted structured clone (
cloneCore) returns uncloneable values by reference and never throws, so a compiled$MessagePorthad no clone-failure point and always deliveredmessage.Fix (confined to
$MessagePort, per the issue's second option):PostMessagedetects the uncloneable categories up front (typeof"function"— covers functions/classes/bound-callable wrappers/delegates — and"symbol") and enqueues a shared static_cloneErrorsentinel instead of a clone.Draincompares by reference and emits'messageerror'(no args) instead of'message'. This mirrors the interpreter's receiver-side model (ClonedMessage(IsError: true)) without touching the sharedcloneCore, so compiledstructuredClone/$BroadcastChannelare unaffected.Notes
SharpTS.dlldependency; verified with--compile --verifyand--compile --standalone --verify(both clean, no SharpTS.dll co-located).{ fn: () => {} }) remain aliased, consistent with the emitted clone's existing shallow fallback — documented inline.$MessagePortRefs the event loop onStart()unconditionally, unlike the interpreter which only Refs cross-thread ports. The existing compiledMessageChanneltests already work around this by closing ports; the newmessageerrortest follows that convention.Tests
ReceiveMessageOnPort_QueuedMessage_ReturnsMessageThenUndefined— new, dual-mode ({ message }thenundefined).MessageChannelPort_PostUncloneable_FiresMessageError— was an interpreter-only[Fact], now a dual-mode[Theory].Green: xUnit 15428/0 · Test262 22/0/4skip (interp + compiled) · TSConf 31/0.