From 28d0028bdc4a9ebea2af13df4c55c80214aeaa3c Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 24 Dec 2024 22:45:48 -0500 Subject: [PATCH 1/4] Editorial: Introduce AllocateImmutableArrayBuffer --- spec.emu | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/spec.emu b/spec.emu index 1633fa5..785ac64 100644 --- a/spec.emu +++ b/spec.emu @@ -430,6 +430,33 @@ contributors: Mark S. Miller, Richard Gibson + + + +

+ AllocateImmutableArrayBuffer ( + _constructor_: a constructor, + _byteLength_: a non-negative integer, + _fromBlock_: a Data Block, + _fromIndex_: a non-negative integer, + _count_: a non-negative integer, + ): either a normal completion containing an ArrayBuffer or a throw completion +

+
+
description
+
It is used to create an immutable ArrayBuffer (i.e., an ArrayBuffer with a an [[ArrayBufferIsImmutable]] slot) with contents from _fromBlock_.
+
+ + 1. Assert: _constructor_ is %ArrayBuffer%. + 1. Assert: _count_ ≤ _byteLength_. + 1. Let _newBuffer_ be ? AllocateArrayBuffer(_constructor_, _byteLength_, ~immutable~). + 1. Let _toBlock_ be _newBuffer_.[[ArrayBufferData]]. + 1. Perform CopyDataBlockBytes(_toBlock_, 0, _fromBlock_, 0, _count_). + 1. Return _newBuffer_. + +
+
+

ArrayBufferCopyAndDetach ( @@ -449,15 +476,16 @@ contributors: Mark S. Miller, Richard Gibson 1. Let _newByteLength_ be ? ToIndex(_newLength_). 1. If IsDetachedBuffer(_arrayBuffer_) is *true*, throw a *TypeError* exception. 1. If IsImmutableBuffer(_arrayBuffer_) is *true*, throw a *TypeError* exception. + 1. Let _copyLength_ be min(_newByteLength_, _arrayBuffer_.[[ArrayBufferByteLength]]). + 1. If _preserveResizability_ is ~immutable~, then + 1. Return ? AllocateImmutableArrayBuffer(%ArrayBuffer%, _newByteLength_, _arrayBuffer_.[[ArrayBufferData]], 0, _copyLength_). 1. If _preserveResizability_ is ~preserve-resizability~ and IsFixedLengthArrayBuffer(_arrayBuffer_) is *false*, then 1. Let _newMaxByteLength_ be _arrayBuffer_.[[ArrayBufferMaxByteLength]]. - 1. Else if _preserveResizability_ is ~immutable~, then - 1. Let _newMaxByteLength_ be ~immutable~. 1. Else, 1. Let _newMaxByteLength_ be ~empty~. 1. If _arrayBuffer_.[[ArrayBufferDetachKey]] is not *undefined*, throw a *TypeError* exception. 1. Let _newBuffer_ be ? AllocateArrayBuffer(%ArrayBuffer%, _newByteLength_, _newMaxByteLength_). - 1. Let _copyLength_ be min(_newByteLength_, _arrayBuffer_.[[ArrayBufferByteLength]]). + 1. Let _copyLength_ be min(_newByteLength_, _arrayBuffer_.[[ArrayBufferByteLength]]). 1. Let _fromBlock_ be _arrayBuffer_.[[ArrayBufferData]]. 1. Let _toBlock_ be _newBuffer_.[[ArrayBufferData]]. 1. NOTE: This is the only step that can write into the Data Block of an immutable ArrayBuffer. @@ -636,7 +664,6 @@ contributors: Mark S. Miller, Richard Gibson -

ArrayBuffer.prototype.transferToImmutable ( [ _newLength_ ] )

This method performs the following steps when called:

From 246c8634789d43208f7edc6de0dc7fc31ff5b226 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 24 Dec 2024 23:00:25 -0500 Subject: [PATCH 2/4] Editorial: Introduce ResolveBounds --- spec.emu | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/spec.emu b/spec.emu index 785ac64..f1ddda8 100644 --- a/spec.emu +++ b/spec.emu @@ -12,6 +12,36 @@ shortname: proposal-immutable-arraybuffer contributors: Mark S. Miller, Richard Gibson + +

Operations on Objects

+ + + + +

+ ResolveBounds ( + _len_: an integer, + _start_: an ECMAScript language value, + _end_: an ECMAScript language value, + ): either a normal completion containing a Record with fields [[From]] (a non-negative integer) and [[To]] (a non-negative integer) or a throw completion +

+
+
+ + 1. Let _relativeStart_ be ? ToIntegerOrInfinity(_start_). + 1. If _relativeStart_ = -∞, let _from_ be 0. + 1. Else if _relativeStart_ < 0, let _from_ be max(_len_ + _relativeStart_, 0). + 1. Else, let _from_ be min(_relativeStart_, _len_). + 1. If _end_ is *undefined*, let _relativeEnd_ be _len_; else let _relativeEnd_ be ? ToIntegerOrInfinity(_end_). + 1. If _relativeEnd_ = -∞, let _to_ be 0. + 1. Else if _relativeEnd_ < 0, let _to_ be max(_len_ + _relativeEnd_, 0). + 1. Else, let _to_ be min(_relativeEnd_, _len_). + 1. Return the Record { [[From]]: _from_, [[To]]: _to_ }. + +
+
+
+

Ordinary and Exotic Objects Behaviours

@@ -487,8 +517,8 @@ contributors: Mark S. Miller, Richard Gibson 1. Let _newBuffer_ be ? AllocateArrayBuffer(%ArrayBuffer%, _newByteLength_, _newMaxByteLength_). 1. Let _copyLength_ be min(_newByteLength_, _arrayBuffer_.[[ArrayBufferByteLength]]). 1. Let _fromBlock_ be _arrayBuffer_.[[ArrayBufferData]]. - 1. Let _toBlock_ be _newBuffer_.[[ArrayBufferData]]. 1. NOTE: This is the only step that can write into the Data Block of an immutable ArrayBuffer. + 1. Let _toBlock_ be _newBuffer_.[[ArrayBufferData]]. 1. Perform CopyDataBlockBytes(_toBlock_, 0, _fromBlock_, 0, _copyLength_). 1. NOTE: Neither creation of the new Data Block nor copying from the old Data Block are observable. Implementations may implement this method as a zero-copy move or a `realloc`. 1. Perform ! DetachArrayBuffer(_arrayBuffer_). @@ -634,14 +664,17 @@ contributors: Mark S. Miller, Richard Gibson 1. If IsSharedArrayBuffer(_O_) is *true*, throw a *TypeError* exception. 1. If IsDetachedBuffer(_O_) is *true*, throw a *TypeError* exception. 1. Let _len_ be _O_.[[ArrayBufferByteLength]]. - 1. Let _relativeStart_ be ? ToIntegerOrInfinity(_start_). - 1. If _relativeStart_ = -∞, let _first_ be 0. - 1. Else if _relativeStart_ < 0, let _first_ be max(_len_ + _relativeStart_, 0). - 1. Else, let _first_ be min(_relativeStart_, _len_). - 1. If _end_ is *undefined*, let _relativeEnd_ be _len_; else let _relativeEnd_ be ? ToIntegerOrInfinity(_end_). - 1. If _relativeEnd_ = -∞, let _final_ be 0. - 1. Else if _relativeEnd_ < 0, let _final_ be max(_len_ + _relativeEnd_, 0). - 1. Else, let _final_ be min(_relativeEnd_, _len_). + 1. Let _relativeStart_ be ? ToIntegerOrInfinity(_start_). + 1. If _relativeStart_ = -∞, let _first_ be 0. + 1. Else if _relativeStart_ < 0, let _first_ be max(_len_ + _relativeStart_, 0). + 1. Else, let _first_ be min(_relativeStart_, _len_). + 1. If _end_ is *undefined*, let _relativeEnd_ be _len_; else let _relativeEnd_ be ? ToIntegerOrInfinity(_end_). + 1. If _relativeEnd_ = -∞, let _final_ be 0. + 1. Else if _relativeEnd_ < 0, let _final_ be max(_len_ + _relativeEnd_, 0). + 1. Else, let _final_ be min(_relativeEnd_, _len_). + 1. Let _bounds_ be ? ResolveBounds(_len_, _start_, _end_). + 1. Let _first_ be _bounds_.[[From]]. + 1. Let _final_ be _bounds_.[[To]]. 1. Let _newLen_ be max(_final_ - _first_, 0). 1. Let _ctor_ be ? SpeciesConstructor(_O_, %ArrayBuffer%). 1. Let _new_ be ? Construct(_ctor_, « 𝔽(_newLen_) »). From 378c739c4485aefff8ab5c3b8efa565fde281aac Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 24 Dec 2024 23:10:21 -0500 Subject: [PATCH 3/4] Normative: Introduce ArrayBuffer.prototype.sliceToImmutable Fixes #9 --- spec.emu | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec.emu b/spec.emu index f1ddda8..87e3f53 100644 --- a/spec.emu +++ b/spec.emu @@ -697,6 +697,34 @@ contributors: Mark S. Miller, Richard Gibson
+ + +

ArrayBuffer.prototype.sliceToImmutable ( _start_, _end_ )

+

This method performs the following steps when called:

+ + 1. Let _O_ be the *this* value. + 1. Perform ? RequireInternalSlot(_O_, [[ArrayBufferData]]). + 1. If IsSharedArrayBuffer(_O_) is *true*, throw a *TypeError* exception. + 1. TODO: Confirm inclusion of this redundant check. + 1. If IsDetachedBuffer(_O_) is *true*, throw a *TypeError* exception. + 1. Let _len_ be _O_.[[ArrayBufferByteLength]]. + 1. Let _bounds_ be ? ResolveBounds(_len_, _start_, _end_). + 1. Let _first_ be _bounds_.[[From]]. + 1. Let _final_ be _bounds_.[[To]]. + 1. TODO: Confirm this strictness vs. the conventional `max(_final_ - _first_, 0)`. + 1. Let _newLen_ be _final_ - _first_. + 1. If _newLen_ < 0, throw a *RangeError* exception. + 1. Let _copyLen_ be min(_newLen_, _len_). + 1. NOTE: Side-effects of the above steps may have detached or resized _O_. This algorithm proceeds only when _O_ is not detached, even if _newLen_ is 0. + 1. If IsDetachedBuffer(_O_) is *true*, throw a *TypeError* exception. + 1. Let _newBuffer_ be ? AllocateImmutableArrayBuffer(%ArrayBuffer%, _newLen_, _O_.[[ArrayBufferData]], _first_, _copyLen_). + 1. Return _newBuffer_. + +
+
+ + +

ArrayBuffer.prototype.transferToImmutable ( [ _newLength_ ] )

This method performs the following steps when called:

From c775957ccc26c6a577c0b51aba4abb2c7d37e136 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 24 Dec 2024 23:23:57 -0500 Subject: [PATCH 4/4] Editorial: Permit immutable Data Blocks with memory locations that subset other immutable Data Blocks --- spec.emu | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec.emu b/spec.emu index 87e3f53..08993a6 100644 --- a/spec.emu +++ b/spec.emu @@ -12,6 +12,15 @@ shortname: proposal-immutable-arraybuffer contributors: Mark S. Miller, Richard Gibson + +

ECMAScript Data Types and Values

+ + +

Data Blocks

+

A data block that resides in memory that can be referenced from multiple agents concurrently is designated a Shared Data Block. A Shared Data Block has an identity (for the purposes of equality testing Shared Data Block values) that is address-free: it is tied not to the virtual addresses the block is mapped to in any process, but to the set of locations in memory that the block represents. Two data blocks are equal only if the sets of the locations they contain are equal; otherwise, they are not equal. and the intersection of the sets of locations they contain is empty The intersection of the sets of locations contained by two non-equal data blocks may be non-empty only when both data blocks are immutable and either one is a strict subset of the other or both are strict subsets of an immutable common parent. Finally, Shared Data Blocks can be distinguished from Data Blocks.

+
+
+

Operations on Objects

@@ -484,6 +493,9 @@ contributors: Mark S. Miller, Richard Gibson 1. Perform CopyDataBlockBytes(_toBlock_, 0, _fromBlock_, 0, _count_). 1. Return _newBuffer_. + +

Because neither the identity of a Data Block nor the set of locations in memory represented by it are observable, implementations may implement this operation without allocating new memory locations when _fromBlock_ is the value of the [[ArrayBufferData]] slot for some other immutable ArrayBuffer (and therefore already immutable) and _count_ = _byteLength_.

+