1 | 'use strict';
|
---|
2 | var globalThis = require('../internals/global-this');
|
---|
3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
4 | var uncurryThisAccessor = require('../internals/function-uncurry-this-accessor');
|
---|
5 | var toIndex = require('../internals/to-index');
|
---|
6 | var notDetached = require('../internals/array-buffer-not-detached');
|
---|
7 | var arrayBufferByteLength = require('../internals/array-buffer-byte-length');
|
---|
8 | var detachTransferable = require('../internals/detach-transferable');
|
---|
9 | var PROPER_STRUCTURED_CLONE_TRANSFER = require('../internals/structured-clone-proper-transfer');
|
---|
10 |
|
---|
11 | var structuredClone = globalThis.structuredClone;
|
---|
12 | var ArrayBuffer = globalThis.ArrayBuffer;
|
---|
13 | var DataView = globalThis.DataView;
|
---|
14 | var min = Math.min;
|
---|
15 | var ArrayBufferPrototype = ArrayBuffer.prototype;
|
---|
16 | var DataViewPrototype = DataView.prototype;
|
---|
17 | var slice = uncurryThis(ArrayBufferPrototype.slice);
|
---|
18 | var isResizable = uncurryThisAccessor(ArrayBufferPrototype, 'resizable', 'get');
|
---|
19 | var maxByteLength = uncurryThisAccessor(ArrayBufferPrototype, 'maxByteLength', 'get');
|
---|
20 | var getInt8 = uncurryThis(DataViewPrototype.getInt8);
|
---|
21 | var setInt8 = uncurryThis(DataViewPrototype.setInt8);
|
---|
22 |
|
---|
23 | module.exports = (PROPER_STRUCTURED_CLONE_TRANSFER || detachTransferable) && function (arrayBuffer, newLength, preserveResizability) {
|
---|
24 | var byteLength = arrayBufferByteLength(arrayBuffer);
|
---|
25 | var newByteLength = newLength === undefined ? byteLength : toIndex(newLength);
|
---|
26 | var fixedLength = !isResizable || !isResizable(arrayBuffer);
|
---|
27 | var newBuffer;
|
---|
28 | notDetached(arrayBuffer);
|
---|
29 | if (PROPER_STRUCTURED_CLONE_TRANSFER) {
|
---|
30 | arrayBuffer = structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
|
---|
31 | if (byteLength === newByteLength && (preserveResizability || fixedLength)) return arrayBuffer;
|
---|
32 | }
|
---|
33 | if (byteLength >= newByteLength && (!preserveResizability || fixedLength)) {
|
---|
34 | newBuffer = slice(arrayBuffer, 0, newByteLength);
|
---|
35 | } else {
|
---|
36 | var options = preserveResizability && !fixedLength && maxByteLength ? { maxByteLength: maxByteLength(arrayBuffer) } : undefined;
|
---|
37 | newBuffer = new ArrayBuffer(newByteLength, options);
|
---|
38 | var a = new DataView(arrayBuffer);
|
---|
39 | var b = new DataView(newBuffer);
|
---|
40 | var copyLength = min(newByteLength, byteLength);
|
---|
41 | for (var i = 0; i < copyLength; i++) setInt8(b, i, getInt8(a, i));
|
---|
42 | }
|
---|
43 | if (!PROPER_STRUCTURED_CLONE_TRANSFER) detachTransferable(arrayBuffer);
|
---|
44 | return newBuffer;
|
---|
45 | };
|
---|