implementation.js 2.6 KB

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