Skip to content

Commit

Permalink
Add a section on performing structured clones from other specifications
Browse files Browse the repository at this point in the history
This closes #855 by adding explicit guidance for those using structured
clone to ensure they properly set up entry and incumbent globals. It
also adds some additional helpful scaffolding, such as adding an
implicit conversion between Web IDL types and JS types.
  • Loading branch information
domenic committed Jun 29, 2016
1 parent 341e1bb commit 363907f
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -8464,6 +8464,45 @@ http://software.hixie.ch/utilities/js/live-dom-viewer/?%3C%21DOCTYPE%20html%3E..
<li><p>Return ? <var>input</var>.[[<span>Transfer</span>]](<var>targetRealm</var>).</p></li>
</ol>

<h4>Performing structured clones from other specifications</h4>

<p>Other specifications may use the <span>StructuredClone</span>,
<span>StructuredCloneWithTransfer</span>, and <span>IsTransferable</span> abstract operations (but
should not use the <span>TransferHelper</span> abstract operation).</p>

<p>In general, call sites may pass in Web IDL values instead of JavaScript values; this is to be
understood to perform an implicit <span data-x="concept-idl-convert">conversion</span> to the
JavaScript value before invoking these algorithms.</p>

<p>Call sites that are not invoked as a result of author code synchronously calling into a user
agent method must take care to properly <span>prepare to run script</span> and <span>prepare to
run a callback</span> before invoking these abstract operations, if they are being performed on
arbitrary objects. This is necessary because the <span>StructuredClone</span> operation can invoke
author-defined accessors as part of its final deep-cloning steps, and these accessors could call
into operations that rely on the <span data-x="concept-entry-everything">entry</span> and <span
data-x="concept-incumbent-everything">incumbent</span> concepts being properly set up.</p>

<p class="example"><code data-x="dom-window-postMessage">postMessage</code> performs
<span>StructuredCloneWithTransfer</span> on its arguments, but is careful to do so immediately,
inside the synchronous portion of its algorithm. Thus it is able to use the structured cloning
algorithms without needing to <span>prepare to run script</span> and <span>prepare to run a
callback</span>.</p>

<p class="example"><span data-x="url parser">Parsing</span> URLs can happen at a variety of times
that are not the synchronous result of author code. However, the URL parser only applies
<span>StructuredClone</span> to <code>Blob</code> objects, so it is not in danger of running
author code with incorrect <span data-x="concept-entry-everything">entry</span> and <span
data-x="concept-incumbent-everything">incumbent</span> values, and thus it too does not need to
perform these preparation steps.</p>

<p class="example">In contrast, a hypothetical API that used <span>StructuredClone</span> to
serialize some author-supplied object periodically, directly from a <span
data-x="concept-task">task</span> on the <span>event loop</span>, would need to ensure it performs
the appropriate preparations before calling into the structured clone algorithms. As of this time,
we know of no such APIs on the platform; usually it is simpler to perform the clone ahead of time,
as a synchronous consequence of author code, like <code
data-x="dom-window-postMessage">postMessage</code>.</p>

<h4>Monkey patch for <code>Blob</code> and <code>FileList</code> objects</h4>

<p class="&#x0058;&#x0058;&#x0058;">This monkey patch will be removed in due course. See <a
Expand Down

0 comments on commit 363907f

Please sign in to comment.