Skip to content

polyfills: follow the structured clone algorithm in structuredClone() - #1221

Open
trackoor wants to merge 2 commits into
saghul:masterfrom
trackoor:structured-clone-algorithm
Open

trackoor wants to merge 2 commits into
saghul:masterfrom
trackoor:structured-clone-algorithm

Conversation

@trackoor

@trackoor trackoor commented Nov 8, 2026

Copy link
Copy Markdown

Fixes #1220.

structuredClone() round-tripped values through QuickJS's object serializer, which differs from the HTML structured clone algorithm in a few places. This replaces it with a small JS implementation of the algorithm:

  • getters are invoked and their values copied
  • Error objects are cloned (Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError; other names become Error), keeping the message and stack
  • arrays keep their holes and extra own properties
  • symbols, functions, WeakMap/WeakSet/WeakRef/FinalizationRegistry and promises throw DataCloneError
  • Blob and File are cloned as Blob/File sharing the same data
  • __proto__ keys are copied as data properties

Dates, RegExps, primitive wrappers, Map, Set, ArrayBuffer (including resizable ones), typed arrays, DataView, cycles and shared references keep working. A SharedArrayBuffer still goes through core.serialize, so the clone shares the same memory as before. The transfer-list checks are unchanged.

I added tests/test-structured-clone-algorithm.js, and the existing structured clone tests still pass. The output of the repro in #1220 now matches Node.js.

Clone values in JS instead of round-tripping them through QuickJS's
object serializer, which rejects getters and Error objects, accepts
symbols, makes arrays dense and drops their extra properties, and turns
a Blob into an empty object.

Fixes: saghul#1220
@saghul

saghul commented Nov 8, 2026

Copy link
Copy Markdown
Owner

Thank you!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

structuredClone rejects getters and Error objects, fills array holes, drops array properties, clones Symbol, breaks Blob

2 participants