25.1.2.14 ArrayBufferCopyAndDetach ( arrayBuffer, newLength, preserveResizability )
The abstract operation ArrayBufferCopyAndDetach takes arguments arrayBuffer (an ECMAScript language value), newLength (an ECMAScript language value), and preserveResizability (preserve-resizability or fixed-length) and returns either a normal completion containing an ArrayBuffer or a throw completion. It performs the following steps when called:
- Perform ? RequireInternalSlot(arrayBuffer, [[ArrayBufferData]]).
- If IsSharedArrayBuffer(arrayBuffer) is true, throw a TypeError exception.
- If newLength is undefined, then
- Let newByteLength be arrayBuffer.[[ArrayBufferByteLength]].
- Else,
- Let newByteLength be ? ToIndex(newLength).
- If IsDetachedBuffer(arrayBuffer) is true, throw a TypeError exception.
- If preserveResizability is preserve-resizability and IsResizableArrayBuffer(arrayBuffer) is true, then
- Let newMaxByteLength be arrayBuffer.[[ArrayBufferMaxByteLength]].
- Else,
- Let newMaxByteLength be empty.
- If arrayBuffer.[[ArrayBufferDetachKey]] is not undefined, throw a TypeError exception.
- Let newBuffer be ? AllocateArrayBuffer(%ArrayBuffer%, newByteLength, newMaxByteLength).
- Let copyLength be min(newByteLength, arrayBuffer.[[ArrayBufferByteLength]]).
- Let fromBlock be arrayBuffer.[[ArrayBufferData]].
- Let toBlock be newBuffer.[[ArrayBufferData]].
- Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength).
- 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
. - Perform ! DetachArrayBuffer(arrayBuffer).
- Return newBuffer.