Skip to content

Commit

Permalink
Serializing and deserializing SharedArrayBuffer
Browse files Browse the repository at this point in the history
Fixes #2260 (by being the last in a set of fixes). Closes
tc39/proposal-ecmascript-sharedmem#144 and closes
tc39/proposal-ecmascript-sharedmem#65.
  • Loading branch information
domenic authored and annevk committed Apr 16, 2017
1 parent 5bf0bba commit 8fa5288
Showing 1 changed file with 56 additions and 15 deletions.
71 changes: 56 additions & 15 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,13 @@
be thought of as completely serializing the execution of all scripts in all <span data-x="browsing
context">browsing contexts</span>.</p>

<p>The exception to this general design principle is the JavaScript <code>SharedArrayBuffer</code>
class. Using <code>SharedArrayBuffer</code>s, it can in fact be observed that scripts in other
<span data-x="event loop">event loops</span> are executing simultaneously. Furthermore, due to the
JavaScript memory model, there are situations which not only are un-representable via serialized
<em>script</em> execution, but also un-representable via serialized <em>statement</em> execution
among those scripts.</p>

</div>


Expand Down Expand Up @@ -2912,6 +2919,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li>The <dfn data-x-href="https://tc39.github.io/ecma262/#sec-isconstructor">IsConstructor</dfn> abstract operation</li>
<li>The <dfn data-x-href="https://tc39.github.io/ecma262/#sec-isdatadescriptor">IsDataDescriptor</dfn> abstract operation</li>
<li>The <dfn data-x-href="https://tc39.github.io/ecma262/#sec-isdetachedbuffer">IsDetachedBuffer</dfn> abstract operation</li>
<li>The <dfn data-x-href="https://tc39.github.io/ecma262/#sec-issharedarraybuffer">IsSharedArrayBuffer</dfn> abstract operation</li>
<li>The <dfn data-x="js-NewObjectEnvironment" data-x-href="https://tc39.github.io/ecma262/#sec-newobjectenvironment">NewObjectEnvironment</dfn> abstract operation</li>
<!-- the next set of Ordinary* abstract operations are in order of appearance -->
<li>The <dfn data-x-href="https://tc39.github.io/ecma262/#sec-ordinarygetprototypeof">OrdinaryGetPrototypeOf</dfn> abstract operation</li>
Expand Down Expand Up @@ -7858,23 +7866,36 @@ interface <dfn>DOMStringList</dfn> {
<p>Otherwise, if <var>value</var> has an [[ArrayBufferData]] internal slot, then:</p>

<ol>
<li><p>If <span>IsDetachedBuffer</span>(<var>value</var>) is true, then throw a
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>

<li><p>Let <var>size</var> be <var>value</var>.[[ArrayBufferByteLength]].</p></li>

<li><p>If <span>IsSharedArrayBuffer</span>(<var>value</var>) is true, then set
<var>serialized</var> to { [[Type]]: "SharedArrayBuffer", [[ArrayBufferData]]:
<var>value</var>.[[ArrayBufferData]], [[ArrayBufferByteLength]]: <var>size</var>,
[[EventLoop]]: <span>current Realm Record</span>'s <span
data-x="concept-realm-settings-object">settings object</span>'s <span>responsible event
loop</span> }.</p></li>

<li>
<p>Let <var>dataCopy</var> be ? <span>CreateByteDataBlock</span>(<var>size</var>).</p>
<p>Otherwise:</p>

<p class="note">This can throw a <code data-x="js-RangeError">RangeError</code> exception upon
allocation failure.</p>
</li>
<ol>
<li><p>If <span>IsDetachedBuffer</span>(<var>value</var>) is true, then throw a
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>

<li>
<p>Let <var>dataCopy</var> be ? <span>CreateByteDataBlock</span>(<var>size</var>).</p>

<li><p>Perform ! <span>CopyDataBlockBytes</span>(<var>dataCopy</var>, 0,
<var>value</var>.[[ArrayBufferData]], 0, <var>size</var>).</p></li>
<p class="note">This can throw a <code data-x="js-RangeError">RangeError</code> exception
upon allocation failure.</p>
</li>

<li><p>Perform ! <span>CopyDataBlockBytes</span>(<var>dataCopy</var>, 0,
<var>value</var>.[[ArrayBufferData]], 0, <var>size</var>).</p></li>

<li><p>Set <var>serialized</var> to { [[Type]]: "ArrayBuffer", [[ArrayBufferData]]:
<var>dataCopy</var>, [[ArrayBufferByteLength]]: <var>size</var> }.</p></li>
<li><p>Set <var>serialized</var> to { [[Type]]: "ArrayBuffer", [[ArrayBufferData]]:
<var>dataCopy</var>, [[ArrayBufferByteLength]]: <var>size</var> }.</p></li>
</ol>
</li>
</ol>
</li>

Expand Down Expand Up @@ -8261,11 +8282,30 @@ o.myself = o;</pre>
<var>serialized</var>.[[OriginalFlags]].</p></li>

<li>
<p>Otherwise, if <var>serialized</var>.[[Type]] is "ArrayBuffer", then set <var>value</var> to a new ArrayBuffer
object in <var>targetRealm</var> whose [[ArrayBufferData]] internal slot value is
<var>serialized</var>.[[ArrayBufferData]], and whose [[ArrayBufferByteLength]] internal slot
<p>Otherwise, if <var>serialized</var>.[[Type]] is "SharedArrayBuffer", then:</p>

<ol>
<li><p>If <var>targetRealm</var>'s <span data-x="concept-realm-settings-object">settings
object</span>'s <span>responsible event loop</span> is not in the same <span>unit of
share-within-able event loops</span> as <var>serialized</var>.[[EventLoop]], then then throw a
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>

<li><p>Otherwise, set <var>value</var> to a new SharedArrayBuffer object in
<var>targetRealm</var> whose [[ArrayBufferData]] internal slot value is
<var>serialized</var>.[[ArrayBufferData]] and whose [[ArrayBufferByteLength]] internal slot
value is <var>serialized</var>.[[ArrayBufferByteLength]].</p></li>
</ol>
</li>

<li>
<p>Otherwise, if <var>serialized</var>.[[Type]] is "ArrayBuffer", then set <var>value</var> to a
new ArrayBuffer object in <var>targetRealm</var> whose [[ArrayBufferData]] internal slot value
is <var>serialized</var>.[[ArrayBufferData]], and whose [[ArrayBufferByteLength]] internal slot
value is <var>serialized</var>.[[ArrayBufferByteLength]].</p>

<p>If this throws an exception, then throw a <span>"<code>DataCloneError</code>"</span>
<code>DOMException</code>.</p>

<p class="note">This step might throw an exception if there is not enough memory available to
create such an ArrayBuffer object.</p>
</li>
Expand Down Expand Up @@ -8474,7 +8514,8 @@ o.myself = o;</pre>
<span>[[Detached]]</span> internal slot, then throw a
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>

<li><p>If <var>transferable</var> has an [[ArrayBufferData]] internal slot and !
<li><p>If <var>transferable</var> has an [[ArrayBufferData]] internal slot and either !
<span>IsSharedArrayBuffer</span>(<var>transferable</var>) is true or !
<span>IsDetachedBuffer</span>(<var>transferable</var>) is true, then throw a
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>

Expand Down

0 comments on commit 8fa5288

Please sign in to comment.