feat: serialize DOMException per Web IDL [Serializable] - #2040
Draft
edusperoni wants to merge 1 commit into
Draft
Conversation
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: trueThanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
edusperoni
added this pull request to stack #2046
September 11, 2026 23:27
DOMException carries the [Serializable] slot in Web IDL, so it must survive
structuredClone and worker postMessage rather than degrading the way a custom
Error subclass does. Node reaches that with its JSTransferable protocol; this
is the same mechanism reduced to the one class.
The dom-exception builtin gains a native half: binding.markCloneable stamps
every instance with a per-isolate v8::Private held in the runtime's
RuntimeState, unforgeable and invisible from JS. Every GetExports call site for
that builtin now goes through serialization::GetDomExceptionExports, because
GetExports consults the binding factory only on the run that populates the
cache.
The serializer delegate claims host objects unconditionally and answers
IsHostObject from the brand. That claim replaces V8's own embedder-field
detection instead of extending it, so objects with internal fields — Java
proxies, URL, URLSearchParams, ObjectManager wrappers — are claimed first and
keep their existing behavior: a DataCloneError under structuredClone, an empty
object over postMessage.
V8 forbids JS execution while a value is being read, so the payload travels
out-of-band: WriteHostObject pushes {name, message, stack} onto the
SerializedValue and writes a tag plus an index, and Deserialize constructs every
instance through the real constructor before ReadValue starts — running the
builtin on demand on a worker isolate that never touched DOMException.
Construction re-brands, so a forwarded exception serializes on the next hop.
Host objects now start with a uint32 tag (0 = degraded native wrapper, 1 =
DOMException index); the bytes never outlive the process.
edusperoni
force-pushed
the
feat/dom-exception-serializable
branch
from
September 12, 2026 17:11
edc52be to
84fc2b6
Compare
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.
Implements the
[Serializable]slot DOMException was deliberately shipped without, so it survivesstructuredCloneand workerpostMessageinstead of degrading like a custom Error subclass. Mirrors NativeScript/ios#453.Mechanism (Node's JSTransferable protocol, reduced to one class)
binding.markCloneablestamps every instance with a per-isolatev8::Private(stored in the runtime'sRuntimeStateslot bag), unforgeable and invisible from JS. All threeGetExportscall sites for the builtin — the lazy-global row, theinternal/dom-exceptionregistry row andThrowDataCloneError— now share one binding factory (serialization::DomExceptionBinding), becauseGetExportsconsults the factory only on the run that populates the cache, so a site passing a different one would win or lose by init order.HasCustomHostObjectand answersIsHostObjectwith a private-symbol check, V8's escape hatch for treating a plain JS object as a host object. That claim replaces V8's own embedder-field detection rather than adding to it, soIsHostObjectclaims anything with internal fields first — Java proxies,URL/URLPattern/URLSearchParams, ObjectManager wrappers — and those keep raisingDataCloneErrorunderstructuredCloneand keep arriving as{}overpostMessage. Cost: one private-symbol lookup per plain JS object in a serialized graph, the same price Node pays.ReadHostObjectis aV8_Fatal), so this mirrors Node'shost_objects_design:WriteHostObjectpushes{name, message, stack}onto an out-of-band list on theSerializedValueand writes only a tag + index into the stream;Deserializeconstructs every instance through the real constructor beforeReadValuestarts, andReadHostObjecthands them out by index. Construction re-brands the instance, so a forwarded exception serializes again on the next hop — and on a worker isolate that never touched DOMException, the pre-construction step runs the builtin on demand.Deserializethrows aDataCloneErrorrather than returning empty with nothing pending when the builtin cannot load, sostructuredClonenever yieldsundefinedsilently. The main-thread worker message read (WorkerWrapper::FireMessageOnParentWorkerObject) now runs under aTryCatch, so a failed read is logged instead of left pending on the isolate; the worker-side read inDrainPendingTaskswas already inside one.Wire format
Host objects now start with a uint32 tag:
0= degraded native wrapper (writes nothing else; the reader returnsObject::New, keeping today's empty-object shape),1= a uint32 index into the out-of-band DOMException payload list. The bytes never outlive the process (structuredCloneround-trips in one isolate, worker messages cross isolates in the same binary), so the format is free to evolve with the file.Policies
DOMException serializes under both
kReject(structuredClone) andkDegrade(workerpostMessage): the reject policy exists to refuse objects whose native half would be left behind, and a DOMException has none. Graph identity is preserved by V8's object-id machinery — one payload per distinct instance.Claiming is unconditional: V8 samples
HasCustomHostObjectonce perValueSerializerand never re-checks it, so a gate on "this isolate holds a DOMException" would lose the type of the isolate's first instance when a getter creates it during the very clone that carries it.Tests
postMessagein both directions — main→worker exercises the on-demand builtin run in a fresh isolate. Those specs probe whetherstructuredCloneactually carries a DOMException rather than assuming it from presence, so they self-gate on runtimes without the slot. The commit also pins the "Throw error in onerror" forward count on whetherWorkeris anEventTarget; Android's is not yet, so the legacy count of 2 stays pinned and the worker error path is untouched here.tests/testRuntimeImplementedAPIs.js: an unguardedstructuredClonecanary; a worker (tests/domExceptionFirstCloneWorker.js) whose first DOMException is born inside a getter during the clone that carries it; and a spec cloningnew java.lang.Object()andnew URL("https://example.com/")after a DOMException exists, pinning that native wrappers still raiseDataCloneError.