Skip to content

Commit 0947339

Browse files
surmadomenic
authored andcommitted
Define self.structuredClone()
Closes #793.
1 parent 0d63ebc commit 0947339

File tree

1 file changed

+69
-23
lines changed

1 file changed

+69
-23
lines changed

source

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7967,13 +7967,24 @@ interface DOMStringList {
79677967
79687968

79697969

7970-
7971-
79727970

Safe passing of structured data

79737971

7972+

To support passing JavaScript objects,

7973+
including platform objects, across
7974+
realm">realm boundaries, this specification defines the following
7975+
infrastructure for serializing and deserializing objects, including in some cases
7976+
transferring the underlying data instead of copying it. Collectively this
7977+
serialization/deserialization process is known as "structured cloning", although most APIs perform
7978+
separate serialization and deserialization steps. (With the notable exception being the
7979+
data-x="dom-structuredClone">structuredClone() method.)

7980+
7981+
7982+
79747983

This section uses the terminology and typographic conventions from the JavaScript

79757984
specification.

79767985

7986+
7987+
79777988

Serializable objects

79787989

79797990

Serializable objects support being serialized, and later deserialized, in a way

@@ -7984,6 +7995,8 @@ interface DOMStringList {
79847995

Not all objects are serializable objects, and not all aspects of objects that are

79857996
serializable objects are necessarily preserved when they are serialized.

79867997

7998+
7999+
79878000

Platform objects can be serializable objects

79888001
if their primary interface is decorated with the
79898002
data-lt="Serializable" data-x="Serializable">[Serializable] IDL extended
@@ -8091,6 +8104,8 @@ interface DOMStringList {
80918104
However, to better specify the behavior of certain more complex situations, the model was updated
80928105
to make the serialization and deserialization explicit.

80938106

8107+
8108+
80948109

Transferable objects

80958110

80968111

Transferable objects support being transferred across

@@ -8103,6 +8118,8 @@ interface DOMStringList {
81038118

Transferring is an irreversible and non-idempotent operation. Once an object has

81048119
been transferred, it cannot be transferred, or indeed used, again.

81058120

8121+
8122+
81068123

Platform objects can be transferable objects

81078124
if their primary interface is decorated with the
81088125
data-lt="Transferable" data-x="Transferable">[Transferable] IDL extended
@@ -9138,11 +9155,6 @@ o.myself = o;
91389155
understood to perform an implicit conversion to the
91399156
JavaScript value before invoking these algorithms.

91409157

9141-

This specification used

9142-
to define a "structured clone" algorithm, and more recently a StructuredClone abstract operation.
9143-
However, in practice all known uses of it were better served by separate serialization and
9144-
deserialization steps, so it was removed.

9145-
91469158

91479159

91489160

Call sites that are not invoked as a result of author code synchronously calling into a user

@@ -9170,6 +9182,38 @@ o.myself = o;
91709182

91719183
91729184

9185+

Structured cloning API

9186+
9187+
9188+
result = self.structuredClone(value[, { transfer }])
9189+
9190+

Takes the input value and returns a deep copy by performing the structured clone algorithm.

9191+
Transferable objects listed in the
9192+
data-x="dom-StructuredSerializeOptions-transfer">transfer array are transferred, not
9193+
just cloned, meaning that they are no longer usable in the input value.

9194+
9195+

Throws a "DataCloneError" DOMException if any part of

9196+
the input value is not serializable.

9197+
9198+
9199+
9200+
9201+
9202+

The

9203+
data-x="dom-structuredClone">structuredClone(value,
9204+
options) method steps are:

9205+
9206+
    9207+
  1. Let serialized be ?

  2. 9208+
    StructuredSerializeWithTransfer(value, options["
    9209+
    data-x="dom-StructuredSerializeOptions-transfer">transfer"]).

    9210+
    9211+
  3. Return ? StructuredDeserialize(serialized, this's

  4. 9212+
    relevant Realm).

    9213+
    9214+
    9215+
    9216+
    91739217

    91749218

    Semantics, structure, and APIs of HTML documents

    91759219

    @@ -80416,7 +80460,7 @@ interface Window : EventTarget {
    8041680460
    Window includes GlobalEventHandlers;
    8041780461
    Window includes WindowEventHandlers;
    8041880462

    80419-
    dictionary WindowPostMessageOptions : PostMessageOptions {
    80463+
    dictionary WindowPostMessageOptions : StructuredSerializeOptions {
    8042080464
    USVString targetOrigin = "/";
    8042180465
    };
    8042280466

    @@ -94809,6 +94853,9 @@ interface mixin WindowOrWorkerGlobalScope {
    9480994853
    // ImageBitmap
    9481094854
    Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, optional ImageBitmapOptions options = {});
    9481194855
    Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, long sx, long sy, long sw, long sh, optional ImageBitmapOptions options = {});
    94856+
    94857+
    // structured cloning
    94858+
    any structuredClone(any value, optional StructuredSerializeOptions options = {});
    9481294859
    };
    9481394860
    Window includes WindowOrWorkerGlobalScope;
    9481494861
    WorkerGlobalScope includes WindowOrWorkerGlobalScope;
    @@ -99740,7 +99787,7 @@ function receiver(e) {
    9974099787
    and can contain certain data objects such as File Blob,
    9974199788
    FileList, and ArrayBuffer objects.

    9974299789

    99743-

    Objects listed in the PostMessageOptions-transfer">transfer member

    99790+

    Objects listed in the StructuredSerializeOptions-transfer">transfer member

    9974499791
    of options are transferred, not just cloned, meaning that they are no longer usable
    9974599792
    on the sending side.

    9974699793

    @@ -99816,7 +99863,7 @@ function receiver(e) {
    9981699863
    9981799864

    9981899865
  5. Let transfer be options["

  6. 99819-
    data-x="dom-PostMessageOptions-transfer">transfer"].

    99866+
    data-x="dom-StructuredSerializeOptions-transfer">transfer"].

    9982099867

    9982199868
  7. Let serializeWithTransferResult be

  8. 9982299869
    StructuredSerializeWithTransfer(message, transfer). Rethrow
    @@ -99892,7 +99939,7 @@ function receiver(e) {
    9989299939

    9989399940
  9. Let options be «[ "

  10. 9989499941
    data-x="dom-WindowPostMessageOptions-targetOrigin">targetOrigin" →
    99895-
    targetOrigin, "PostMessageOptions-transfer">transfer" →
    99942+
    targetOrigin, "StructuredSerializeOptions-transfer">transfer" →
    9989699943
    transfer ]».

    9989799944

    9989899945
  11. Run the window post message steps providing targetWindow,

  12. @@ -100160,7 +100207,7 @@ interface MessageChannel {
    100160100207
    [Exposed=(Window,Worker,AudioWorklet), Transferable]
    100161100208
    interface MessagePort : EventTarget {
    100162100209
    undefined postMessage(any message, sequence<object> transfer);
    100163-
    undefined postMessage(any message, optional PostMessageOptions options = {});
    100210+
    undefined postMessage(any message, optional StructuredSerializeOptions options = {});
    100164100211
    undefined start();
    100165100212
    undefined close();
    100166100213

    @@ -100169,8 +100216,8 @@ interface MessagePort : EventTarget {
    100169100216
    attribute EventHandler onmessageerror;
    100170100217
    };
    100171100218

    100172-
    dictionary PostMessageOptions {
    100173-
    sequence<object> PostMessageOptions" data-x="dom-PostMessageOptions-transfer">transfer = [];
    100219+
    dictionary StructuredSerializeOptions {
    100220+
    sequence<object> StructuredSerializeOptions" data-x="dom-StructuredSerializeOptions-transfer">transfer = [];
    100174100221
    };
    100175100222

    100176100223
    @@ -100313,7 +100360,7 @@ dictionary PostMessageOptions {
    100313100360

    100314100361
    100315100362
  13. Let transfer be options["

  14. 100316-
    data-x="dom-PostMessageOptions-transfer">transfer"].

    100363+
    data-x="dom-StructuredSerializeOptions-transfer">transfer"].

    100317100364

    100318100365
  15. If transfer contains this

  16. 100319100366
    MessagePort, then throw a "DataCloneError"
    @@ -100403,7 +100450,8 @@ dictionary PostMessageOptions {
    100403100450
    entangled, if any; otherwise let it be null.

    100404100451

    100405100452
  17. Let options be «[ "

  18. 100406-
    data-x="dom-PostMessageOptions-transfer">transfer" → transfer ]».

    100453+
    data-x="dom-StructuredSerializeOptions-transfer">transfer" →
    100454+
    transfer ]».

    100407100455

    100408100456
  19. Run the message port post message steps providing targetPort,

  20. 100409100457
    message and options.

    @@ -101272,7 +101320,7 @@ interface DedicatedWorkerGlobalScope : WorkerGlobalSc
    101272101320
    [Replaceable] readonly attribute DOMString name;
    101273101321

    101274101322
    undefined postMessage(any message, sequence<object> transfer);
    101275-
    undefined postMessage(any message, optional PostMessageOptions options = {});
    101323+
    undefined postMessage(any message, optional StructuredSerializeOptions options = {});
    101276101324

    101277101325
    undefined close();
    101278101326

    @@ -101294,10 +101342,8 @@ interface DedicatedWorkerGlobalScope : WorkerGlobalSc
    101294101342
    data-x="concept-WorkerGlobalScope-name">name, i.e. the value given to the
    101295101343
    Worker constructor. Primarily useful for debugging.

    101296101344

    101297-
    dedicatedWorkerGlobal.postMessage(message [,
    101298-
    transfer ])
    101299-
    dedicatedWorkerGlobal.postMessage(message
    101300-
    [, { transfer } ])
    101345+
    dedicatedWorkerGlobal.postMessage(message [, transfer ])
    101346+
    dedicatedWorkerGlobal.postMessage(message [, { transfer } ])
    101301101347

    Clones message and transmits it to the Worker object associated

    101302101348
    with dedicatedWorkerGlobal. transfer can be passed as a list of objects
    101303101349
    that are to be transferred rather than cloned.

    @@ -102007,7 +102053,7 @@ interface Worker : EventTarget {
    102007102053
    undefined terminate();
    102008102054

    102009102055
    undefined postMessage(any message, sequence<object> transfer);
    102010-
    undefined postMessage(any message, optional PostMessageOptions options = {});
    102056+
    undefined postMessage(any message, optional StructuredSerializeOptions options = {});
    102011102057
    attribute EventHandler onmessage;
    102012102058
    attribute EventHandler onmessageerror;
    102013102059
    };
    @@ -102037,7 +102083,7 @@ enum WorkerType { "classic", "module" };
    102037102083
    Aborts worker's associated global environment.
    102038102084

    102039102085
    worker.postMessage(message [, transfer ])
    102040-
    worker.postMessage(message [, { PostMessageOptions-transfer">transfer } ])
    102086+
    worker.postMessage(message [, { StructuredSerializeOptions-transfer">transfer } ])
    102041102087

    Clones message and transmits it to worker's global environment.

    102042102088
    transfer can be passed as a list of objects that are to be transferred rather than
    102043102089
    cloned.

    0 commit comments

    Comments
     (0)