Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

structuredClone #984

Merged
merged 73 commits into from
Dec 9, 2021
Merged

structuredClone #984

merged 73 commits into from
Dec 9, 2021

Conversation

BasixKOR
Copy link
Contributor

@BasixKOR BasixKOR commented Sep 14, 2021

Fixes #969

This is my naive approach to implement the structured cloning algorithm. It doesn't actually serialize and deserialize as the specification suggests, but it will give the closest clone I can replicate.

Clones

  • Primitives
  • Primitive wrappers
  • Date
  • RegExp
  • Blob (Web, Deno)
  • File (Web, Deno)
  • FileList (Web)
  • ArrayBuffer
  • SharedArrayBuffer*
  • DataView
  • TypedArray
  • ImageBitmap (Web)
  • ImageData (Web)
  • Array
  • Object
  • Map
  • Set
  • Error
  • DOMException
  • Geometry types
  • WebAssembly.Module
  • CryptoKey
  • AudioData
  • VideoFrame
  • GPUCompilationMessage
  • GPUCompilationInfo
  • RTCCertificate
  • *will not share the same memory space

NB: https://gist.github.com/petamoriken/3802602b8e93d89e5b4c21e36683cadb#gistcomment-3899514

Concerns

  • Could we get rid of WeakMap dependency?
  • Should structuredClone polyfill ES6 Map and Set for the environments that don't have them?
  • All of instanceof calls should've been internal slot checks.
  • Error.prototype.stack wildly varies between JS engines, and probably we should clone them too. Some notes can be found here.
  • Blob, File and other web APIs are platform objects and their cloning behavior is defined by their IDL? Not very sure we should include this as well.

@BasixKOR
Copy link
Contributor Author

There is a nice test suite for this algorithm in web-platform-tests, but I'm unsure the license is compatible.

@zloirock
Copy link
Owner

zloirock commented Sep 14, 2021

Could we get rid of WeakMap dependency?

Yes, I wrote above how to do it.

Should structuredClone polyfill ES6 Map and Set for the environments that don't have them?

The same as with WeakMap.

All of instanceof calls should've been internal slot checks.

It's possible to use .prototype.valuOf to check it in most cases. See below.

@zloirock
Copy link
Owner

There is a nice test suite for this algorithm in web-platform-tests, but I'm unsure the license is compatible.

You could copy those tests and add a copyright note to those tests files.

@zloirock
Copy link
Owner

zloirock commented Sep 14, 2021

All of instanceof calls should've been internal slot checks.

I think that for this case internals/classof is enough. Like:

switch (classof(value)) {
  case 'Boolean': ...
  case 'BigInt': ...
  case 'Number': ...
  case 'String': ...
  case 'Date': ...
  case 'RegExp': ...
  case 'Map': ...
}

tests/compat/tests.js Outdated Show resolved Hide resolved
@BasixKOR BasixKOR force-pushed the structured-clone branch 2 times, most recently from 1bfdd07 to 422802c Compare September 16, 2021 12:49
tests/compat/tests.js Outdated Show resolved Hide resolved
zloirock and others added 27 commits December 9, 2021 22:12
Co-authored-by: Kenta Moriuchi <moriken@kimamass.com>
native `structuredClone` is already available in unstable versions of all modern engines, but no one of them still not passes new (related to html/5749) errors cloning tests
…non-serializable instead of `DOMException`
@zloirock zloirock merged commit bc4771c into zloirock:master Dec 9, 2021
@zloirock
Copy link
Owner

Published.

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
5 participants