source: imaps-frontend/node_modules/arraybuffer.prototype.slice/implementation.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.6 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $ArrayBuffer = GetIntrinsic('%ArrayBuffer%', true);
6var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
7
8var IsDetachedBuffer = require('es-abstract/2023/IsDetachedBuffer');
9var IsSharedArrayBuffer = require('es-abstract/2023/IsSharedArrayBuffer');
10var max = require('es-abstract/2023/max');
11var min = require('es-abstract/2023/min');
12var SpeciesConstructor = require('es-abstract/2023/SpeciesConstructor');
13var ToIntegerOrInfinity = require('es-abstract/2023/ToIntegerOrInfinity');
14
15var arrayBufferByteLength = require('array-buffer-byte-length');
16var isArrayBuffer = require('is-array-buffer');
17var $TypeError = require('es-errors/type');
18
19module.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};
Note: See TracBrowser for help on using the repository browser.