1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var min = GetIntrinsic('%Math.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-bind/callBound');
|
---|
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 | module.exports = function ArrayBufferCopyAndDetach(arrayBuffer, newLength, preserveResizability) {
|
---|
40 | if (preserveResizability !== 'PRESERVE-RESIZABILITY' && preserveResizability !== 'FIXED-LENGTH') {
|
---|
41 | throw new $TypeError('`preserveResizability` must be ~PRESERVE-RESIZABILITY~ or ~FIXED-LENGTH~');
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (!isArrayBuffer(arrayBuffer) || isSharedArrayBuffer(arrayBuffer)) {
|
---|
45 | throw new $TypeError('`arrayBuffer` must be an ArrayBuffer'); // steps 1 - 2
|
---|
46 | }
|
---|
47 |
|
---|
48 | var abByteLength;
|
---|
49 |
|
---|
50 | var newByteLength;
|
---|
51 | if (typeof newLength === 'undefined') { // step 3
|
---|
52 | newByteLength = byteLength(arrayBuffer); // step 3.a
|
---|
53 | abByteLength = newByteLength;
|
---|
54 | } else { // step 4
|
---|
55 | newByteLength = ToIndex(newLength); // step 4.a
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (IsDetachedBuffer(arrayBuffer)) {
|
---|
59 | throw new $TypeError('`arrayBuffer` must not be detached'); // step 5
|
---|
60 | }
|
---|
61 |
|
---|
62 | var newMaxByteLength;
|
---|
63 | if (preserveResizability === 'PRESERVE-RESIZABILITY' && !IsFixedLengthArrayBuffer(arrayBuffer)) { // step 6
|
---|
64 | newMaxByteLength = $maxByteLength(arrayBuffer); // step 6.a
|
---|
65 | } else { // step 7
|
---|
66 | newMaxByteLength = 'EMPTY'; // step 7.a
|
---|
67 | }
|
---|
68 |
|
---|
69 | // commented out since there's no way to set or access this key
|
---|
70 |
|
---|
71 | // 8. If arrayBuffer.[[ArrayBufferDetachKey]] is not undefined, throw a TypeError exception.
|
---|
72 |
|
---|
73 | // 9. Let newBuffer be ? AllocateArrayBuffer(%ArrayBuffer%, newByteLength, newMaxByteLength).
|
---|
74 | var newBuffer = newMaxByteLength === 'EMPTY' ? new $ArrayBuffer(newByteLength) : new $ArrayBuffer(newByteLength, { maxByteLength: newMaxByteLength });
|
---|
75 |
|
---|
76 | if (typeof abByteLength !== 'number') {
|
---|
77 | abByteLength = byteLength(arrayBuffer);
|
---|
78 | }
|
---|
79 | var copyLength = min(newByteLength, abByteLength); // step 10
|
---|
80 | if (newByteLength > copyLength) {
|
---|
81 | var taNew = new $Uint8Array(newBuffer);
|
---|
82 | var taOld = new $Uint8Array(arrayBuffer);
|
---|
83 | for (var i = 0; i < copyLength; i++) {
|
---|
84 | taNew[i] = taOld[i];
|
---|
85 | }
|
---|
86 | } else {
|
---|
87 | newBuffer = $abSlice(arrayBuffer, 0, copyLength); // ? optimization for when the new buffer will not be larger than the old one
|
---|
88 | }
|
---|
89 | /*
|
---|
90 | 11. Let fromBlock be arrayBuffer.[[ArrayBufferData]].
|
---|
91 | 12. Let toBlock be newBuffer.[[ArrayBufferData]].
|
---|
92 | 13. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength).
|
---|
93 | 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.
|
---|
94 | */
|
---|
95 |
|
---|
96 | DetachArrayBuffer(arrayBuffer); // step 15
|
---|
97 |
|
---|
98 | return newBuffer; // step 16
|
---|
99 | };
|
---|