| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
|---|
| 4 |
|
|---|
| 5 | var min = require('math-intrinsics/min');
|
|---|
| 6 | var $TypeError = require('es-errors/type');
|
|---|
| 7 | var $ArrayBuffer = GetIntrinsic('%ArrayBuffer%', true);
|
|---|
| 8 | var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
|
|---|
| 9 |
|
|---|
| 10 | var callBound = require('call-bound');
|
|---|
| 11 |
|
|---|
| 12 | var byteLength = require('array-buffer-byte-length');
|
|---|
| 13 | var $maxByteLength = callBound('%ArrayBuffer.prototype.maxByteLength%', true);
|
|---|
| 14 | var copy = function copyAB(src, start, end) {
|
|---|
| 15 | var that = new $Uint8Array(src);
|
|---|
| 16 | if (typeof end === 'undefined') {
|
|---|
| 17 | end = that.length; // eslint-disable-line no-param-reassign
|
|---|
| 18 | }
|
|---|
| 19 | var result = new $ArrayBuffer(end - start);
|
|---|
| 20 | var resultArray = new $Uint8Array(result);
|
|---|
| 21 | for (var i = 0; i < resultArray.length; i++) {
|
|---|
| 22 | resultArray[i] = that[i + start];
|
|---|
| 23 | }
|
|---|
| 24 | return result;
|
|---|
| 25 | };
|
|---|
| 26 | var $abSlice = callBound('%ArrayBuffer.prototype.slice%', true)
|
|---|
| 27 | || function slice(ab, a, b) { // in node < 0.11, slice is an own nonconfigurable property
|
|---|
| 28 | return ab.slice ? ab.slice(a, b) : copy(ab, a, b); // node 0.8 lacks `slice`
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | var DetachArrayBuffer = require('./DetachArrayBuffer');
|
|---|
| 32 | var IsDetachedBuffer = require('./IsDetachedBuffer');
|
|---|
| 33 | var IsFixedLengthArrayBuffer = require('./IsFixedLengthArrayBuffer');
|
|---|
| 34 | var ToIndex = require('./ToIndex');
|
|---|
| 35 |
|
|---|
| 36 | var isArrayBuffer = require('is-array-buffer');
|
|---|
| 37 | var isSharedArrayBuffer = require('is-shared-array-buffer');
|
|---|
| 38 |
|
|---|
| 39 | // https://262.ecma-international.org/15.0/#sec-arraybuffercopyanddetach
|
|---|
| 40 |
|
|---|
| 41 | module.exports = function ArrayBufferCopyAndDetach(arrayBuffer, newLength, preserveResizability) {
|
|---|
| 42 | if (preserveResizability !== 'PRESERVE-RESIZABILITY' && preserveResizability !== 'FIXED-LENGTH') {
|
|---|
| 43 | throw new $TypeError('`preserveResizability` must be ~PRESERVE-RESIZABILITY~ or ~FIXED-LENGTH~');
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | if (!isArrayBuffer(arrayBuffer) || isSharedArrayBuffer(arrayBuffer)) {
|
|---|
| 47 | throw new $TypeError('`arrayBuffer` must be a non-shared ArrayBuffer'); // steps 1 - 2
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | var abByteLength;
|
|---|
| 51 |
|
|---|
| 52 | var newByteLength;
|
|---|
| 53 | if (typeof newLength === 'undefined') { // step 3
|
|---|
| 54 | newByteLength = byteLength(arrayBuffer); // step 3.a
|
|---|
| 55 | abByteLength = newByteLength;
|
|---|
| 56 | } else { // step 4
|
|---|
| 57 | newByteLength = ToIndex(newLength); // step 4.a
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | if (IsDetachedBuffer(arrayBuffer)) {
|
|---|
| 61 | throw new $TypeError('`arrayBuffer` must not be detached'); // step 5
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | var newMaxByteLength;
|
|---|
| 65 | if (preserveResizability === 'PRESERVE-RESIZABILITY' && !IsFixedLengthArrayBuffer(arrayBuffer)) { // step 6
|
|---|
| 66 | newMaxByteLength = $maxByteLength(arrayBuffer); // step 6.a
|
|---|
| 67 | } else { // step 7
|
|---|
| 68 | newMaxByteLength = 'EMPTY'; // step 7.a
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // commented out since there's no way to set or access this key
|
|---|
| 72 |
|
|---|
| 73 | // 8. If arrayBuffer.[[ArrayBufferDetachKey]] is not undefined, throw a TypeError exception.
|
|---|
| 74 |
|
|---|
| 75 | // 9. Let newBuffer be ? AllocateArrayBuffer(%ArrayBuffer%, newByteLength, newMaxByteLength).
|
|---|
| 76 | var newBuffer = newMaxByteLength === 'EMPTY' ? new $ArrayBuffer(newByteLength) : new $ArrayBuffer(newByteLength, { maxByteLength: newMaxByteLength });
|
|---|
| 77 |
|
|---|
| 78 | if (typeof abByteLength !== 'number') {
|
|---|
| 79 | abByteLength = byteLength(arrayBuffer);
|
|---|
| 80 | }
|
|---|
| 81 | var copyLength = min(newByteLength, abByteLength); // step 10
|
|---|
| 82 | if (newByteLength > copyLength || newMaxByteLength !== 'EMPTY') {
|
|---|
| 83 | var taNew = new $Uint8Array(newBuffer);
|
|---|
| 84 | var taOld = new $Uint8Array(arrayBuffer);
|
|---|
| 85 | for (var i = 0; i < copyLength; i++) {
|
|---|
| 86 | taNew[i] = taOld[i];
|
|---|
| 87 | }
|
|---|
| 88 | } else {
|
|---|
| 89 | newBuffer = $abSlice(arrayBuffer, 0, copyLength); // ? optimization for when the new buffer will not be larger than the old one
|
|---|
| 90 | }
|
|---|
| 91 | /*
|
|---|
| 92 | 11. Let fromBlock be arrayBuffer.[[ArrayBufferData]].
|
|---|
| 93 | 12. Let toBlock be newBuffer.[[ArrayBufferData]].
|
|---|
| 94 | 13. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength).
|
|---|
| 95 | 14. 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.
|
|---|
| 96 | */
|
|---|
| 97 |
|
|---|
| 98 | DetachArrayBuffer(arrayBuffer); // step 15
|
|---|
| 99 |
|
|---|
| 100 | return newBuffer; // step 16
|
|---|
| 101 | };
|
|---|