1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $ArrayBuffer = GetIntrinsic('%ArrayBuffer%', true);
|
---|
6 | var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
|
---|
7 |
|
---|
8 | var IsDetachedBuffer = require('es-abstract/2023/IsDetachedBuffer');
|
---|
9 | var IsSharedArrayBuffer = require('es-abstract/2023/IsSharedArrayBuffer');
|
---|
10 | var max = require('es-abstract/2023/max');
|
---|
11 | var min = require('es-abstract/2023/min');
|
---|
12 | var SpeciesConstructor = require('es-abstract/2023/SpeciesConstructor');
|
---|
13 | var ToIntegerOrInfinity = require('es-abstract/2023/ToIntegerOrInfinity');
|
---|
14 |
|
---|
15 | var arrayBufferByteLength = require('array-buffer-byte-length');
|
---|
16 | var isArrayBuffer = require('is-array-buffer');
|
---|
17 | var $TypeError = require('es-errors/type');
|
---|
18 |
|
---|
19 | module.exports = function slice(start, end) {
|
---|
20 | var O = this; // step 1
|
---|
21 |
|
---|
22 | if (!isArrayBuffer(O) || IsSharedArrayBuffer(O) || IsDetachedBuffer(O)) {
|
---|
23 | throw new $TypeError('receiver must be a non-detached, non-shared ArrayBuffer'); // steps 2-4
|
---|
24 | }
|
---|
25 |
|
---|
26 | var len = arrayBufferByteLength(O); // step 5
|
---|
27 |
|
---|
28 | var relativeStart = ToIntegerOrInfinity(start); // step 6
|
---|
29 |
|
---|
30 | var first;
|
---|
31 | if (relativeStart === -Infinity) {
|
---|
32 | first = 0; // step 7
|
---|
33 | } else if (relativeStart < 0) {
|
---|
34 | first = max(len + relativeStart, 0); // step 8
|
---|
35 | } else {
|
---|
36 | first = min(relativeStart, len); // step 9
|
---|
37 | }
|
---|
38 |
|
---|
39 | var relativeEnd = typeof end === 'undefined' ? len : ToIntegerOrInfinity(end); // step 10
|
---|
40 |
|
---|
41 | var final;
|
---|
42 | if (relativeEnd === -Infinity) {
|
---|
43 | final = 0; // step 11
|
---|
44 | } else if (relativeEnd < 0) {
|
---|
45 | final = max(len + relativeEnd, 0); // step 12
|
---|
46 | } else {
|
---|
47 | final = min(relativeEnd, len); // step 13
|
---|
48 | }
|
---|
49 |
|
---|
50 | var newLen = max(final - first, 0); // step 14
|
---|
51 |
|
---|
52 | var Ctor = SpeciesConstructor(O, $ArrayBuffer); // step 15
|
---|
53 |
|
---|
54 | var new$ = new Ctor(newLen); // step 16
|
---|
55 |
|
---|
56 | if (!isArrayBuffer(new$) || IsSharedArrayBuffer(new$) || IsDetachedBuffer(new$)) {
|
---|
57 | throw new $TypeError('Species constructor must produce a non-detached, non-shared Array Buffer'); // steps 17-19
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (new$ === O) {
|
---|
61 | throw new $TypeError('new ArrayBuffer should not have been the same as the receiver'); // step 20
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (arrayBufferByteLength(new$) < newLen) {
|
---|
65 | throw new $TypeError('new ArrayBuffer\'s byteLength must be at least the requested length'); // step 21
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (IsDetachedBuffer(O)) {
|
---|
69 | throw new $TypeError('receiver became an detached ArrayBuffer'); // step 23
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | 24. Let fromBuf be O.[[ArrayBufferData]].
|
---|
74 | 25. Let toBuf be new.[[ArrayBufferData]].
|
---|
75 | 26. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
|
---|
76 | */
|
---|
77 | var sourceArr = new $Uint8Array(O);
|
---|
78 | var destArr = new $Uint8Array(new$);
|
---|
79 | for (var i = start, ii = 0; i < end; i++, ii++) {
|
---|
80 | destArr[ii] = sourceArr[i];
|
---|
81 | }
|
---|
82 |
|
---|
83 | return new$; // step 27
|
---|
84 | };
|
---|