Skip to content

Commit 8fa5288

Browse files
domenicannevk
authored andcommitted
Serializing and deserializing SharedArrayBuffer
Fixes #2260 (by being the last in a set of fixes). Closes tc39/proposal-ecmascript-sharedmem#144 and closes tc39/proposal-ecmascript-sharedmem#65.
1 parent 5bf0bba commit 8fa5288

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

source

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@
495495
be thought of as completely serializing the execution of all scripts in all <span data-x="browsing
496496
context">browsing contexts</span>.</p>
497497

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

500507

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

78607868
<ol>
7861-
<li><p>If <span>IsDetachedBuffer</span>(<var>value</var>) is true, then throw a
7862-
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>
7863-
78647869
<li><p>Let <var>size</var> be <var>value</var>.[[ArrayBufferByteLength]].</p></li>
78657870

7871+
<li><p>If <span>IsSharedArrayBuffer</span>(<var>value</var>) is true, then set
7872+
<var>serialized</var> to { [[Type]]: "SharedArrayBuffer", [[ArrayBufferData]]:
7873+
<var>value</var>.[[ArrayBufferData]], [[ArrayBufferByteLength]]: <var>size</var>,
7874+
[[EventLoop]]: <span>current Realm Record</span>'s <span
7875+
data-x="concept-realm-settings-object">settings object</span>'s <span>responsible event
7876+
loop</span> }.</p></li>
7877+
78667878
<li>
7867-
<p>Let <var>dataCopy</var> be ? <span>CreateByteDataBlock</span>(<var>size</var>).</p>
7879+
<p>Otherwise:</p>
78687880

7869-
<p class="note">This can throw a <code data-x="js-RangeError">RangeError</code> exception upon
7870-
allocation failure.</p>
7871-
</li>
7881+
<ol>
7882+
<li><p>If <span>IsDetachedBuffer</span>(<var>value</var>) is true, then throw a
7883+
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>
7884+
7885+
<li>
7886+
<p>Let <var>dataCopy</var> be ? <span>CreateByteDataBlock</span>(<var>size</var>).</p>
78727887

7873-
<li><p>Perform ! <span>CopyDataBlockBytes</span>(<var>dataCopy</var>, 0,
7874-
<var>value</var>.[[ArrayBufferData]], 0, <var>size</var>).</p></li>
7888+
<p class="note">This can throw a <code data-x="js-RangeError">RangeError</code> exception
7889+
upon allocation failure.</p>
7890+
</li>
7891+
7892+
<li><p>Perform ! <span>CopyDataBlockBytes</span>(<var>dataCopy</var>, 0,
7893+
<var>value</var>.[[ArrayBufferData]], 0, <var>size</var>).</p></li>
78757894

7876-
<li><p>Set <var>serialized</var> to { [[Type]]: "ArrayBuffer", [[ArrayBufferData]]:
7877-
<var>dataCopy</var>, [[ArrayBufferByteLength]]: <var>size</var> }.</p></li>
7895+
<li><p>Set <var>serialized</var> to { [[Type]]: "ArrayBuffer", [[ArrayBufferData]]:
7896+
<var>dataCopy</var>, [[ArrayBufferByteLength]]: <var>size</var> }.</p></li>
7897+
</ol>
7898+
</li>
78787899
</ol>
78797900
</li>
78807901

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

82638284
<li>
8264-
<p>Otherwise, if <var>serialized</var>.[[Type]] is "ArrayBuffer", then set <var>value</var> to a new ArrayBuffer
8265-
object in <var>targetRealm</var> whose [[ArrayBufferData]] internal slot value is
8266-
<var>serialized</var>.[[ArrayBufferData]], and whose [[ArrayBufferByteLength]] internal slot
8285+
<p>Otherwise, if <var>serialized</var>.[[Type]] is "SharedArrayBuffer", then:</p>
8286+
8287+
<ol>
8288+
<li><p>If <var>targetRealm</var>'s <span data-x="concept-realm-settings-object">settings
8289+
object</span>'s <span>responsible event loop</span> is not in the same <span>unit of
8290+
share-within-able event loops</span> as <var>serialized</var>.[[EventLoop]], then then throw a
8291+
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>
8292+
8293+
<li><p>Otherwise, set <var>value</var> to a new SharedArrayBuffer object in
8294+
<var>targetRealm</var> whose [[ArrayBufferData]] internal slot value is
8295+
<var>serialized</var>.[[ArrayBufferData]] and whose [[ArrayBufferByteLength]] internal slot
8296+
value is <var>serialized</var>.[[ArrayBufferByteLength]].</p></li>
8297+
</ol>
8298+
</li>
8299+
8300+
<li>
8301+
<p>Otherwise, if <var>serialized</var>.[[Type]] is "ArrayBuffer", then set <var>value</var> to a
8302+
new ArrayBuffer object in <var>targetRealm</var> whose [[ArrayBufferData]] internal slot value
8303+
is <var>serialized</var>.[[ArrayBufferData]], and whose [[ArrayBufferByteLength]] internal slot
82678304
value is <var>serialized</var>.[[ArrayBufferByteLength]].</p>
82688305

8306+
<p>If this throws an exception, then throw a <span>"<code>DataCloneError</code>"</span>
8307+
<code>DOMException</code>.</p>
8308+
82698309
<p class="note">This step might throw an exception if there is not enough memory available to
82708310
create such an ArrayBuffer object.</p>
82718311
</li>
@@ -8474,7 +8514,8 @@ o.myself = o;</pre>
84748514
<span>[[Detached]]</span> internal slot, then throw a
84758515
<span>"<code>DataCloneError</code>"</span> <code>DOMException</code>.</p></li>
84768516

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

0 commit comments

Comments
 (0)