source: imaps-frontend/node_modules/es-abstract/2023/CloneArrayBuffer.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var GetPrototypeFromConstructor = require('./GetPrototypeFromConstructor');
6var IsConstructor = require('./IsConstructor');
7var IsDetachedBuffer = require('./IsDetachedBuffer');
8var OrdinarySetPrototypeOf = require('./OrdinarySetPrototypeOf');
9
[79a0317]10var isInteger = require('math-intrinsics/isInteger');
[d565449]11var isArrayBuffer = require('is-array-buffer');
12var arrayBufferSlice = require('arraybuffer.prototype.slice');
13
14// https://262.ecma-international.org/12.0/#sec-clonearraybuffer
15
16module.exports = function CloneArrayBuffer(srcBuffer, srcByteOffset, srcLength, cloneConstructor) {
17 if (!isArrayBuffer(srcBuffer)) {
18 throw new $TypeError('Assertion failed: `srcBuffer` must be an ArrayBuffer instance');
19 }
20 if (!isInteger(srcByteOffset) || srcByteOffset < 0) {
21 throw new $TypeError('Assertion failed: `srcByteOffset` must be a non-negative integer');
22 }
23 if (!isInteger(srcLength) || srcLength < 0) {
24 throw new $TypeError('Assertion failed: `srcLength` must be a non-negative integer');
25 }
26 if (!IsConstructor(cloneConstructor)) {
27 throw new $TypeError('Assertion failed: `cloneConstructor` must be a constructor');
28 }
29
30 // 3. Let targetBuffer be ? AllocateArrayBuffer(cloneConstructor, srcLength).
31 var proto = GetPrototypeFromConstructor(cloneConstructor, '%ArrayBufferPrototype%'); // step 3, kinda
32
33 if (IsDetachedBuffer(srcBuffer)) {
34 throw new $TypeError('`srcBuffer` must not be a detached ArrayBuffer'); // step 4
35 }
36
37 /*
38 5. Let srcBlock be srcBuffer.[[ArrayBufferData]].
39 6. Let targetBlock be targetBuffer.[[ArrayBufferData]].
40 7. Perform CopyDataBlockBytes(targetBlock, 0, srcBlock, srcByteOffset, srcLength).
41 */
42 var targetBuffer = arrayBufferSlice(srcBuffer, srcByteOffset, srcByteOffset + srcLength); // steps 5-7
43 OrdinarySetPrototypeOf(targetBuffer, proto); // step 3
44
45 return targetBuffer; // step 8
46};
Note: See TracBrowser for help on using the repository browser.