1 | 'use strict';
|
---|
2 |
|
---|
3 | var forEach = require('for-each');
|
---|
4 | var callBind = require('call-bind');
|
---|
5 | var gPO = require('reflect.getprototypeof/polyfill')();
|
---|
6 |
|
---|
7 | var typedArrays = require('available-typed-arrays')();
|
---|
8 |
|
---|
9 | /** @typedef {(x: import('.').TypedArray) => number} ByteOffsetGetter */
|
---|
10 |
|
---|
11 | /** @type {Record<import('.').TypedArrayName, ByteOffsetGetter>} */
|
---|
12 | var getters = {
|
---|
13 | // @ts-expect-error TS can't handle __proto__ or `satisfies` in jsdoc
|
---|
14 | __proto__: null
|
---|
15 | };
|
---|
16 |
|
---|
17 | var gOPD = require('gopd');
|
---|
18 | var oDP = Object.defineProperty;
|
---|
19 | if (gOPD) {
|
---|
20 | /** @type {ByteOffsetGetter} */
|
---|
21 | var getByteOffset = function (x) {
|
---|
22 | return x.byteOffset;
|
---|
23 | };
|
---|
24 | forEach(typedArrays, function (typedArray) {
|
---|
25 | // In Safari 7, Typed Array constructors are typeof object
|
---|
26 | if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
|
---|
27 | var Proto = global[typedArray].prototype;
|
---|
28 | // @ts-expect-error TS can't guarantee the callback is invoked sync
|
---|
29 | var descriptor = gOPD(Proto, 'byteOffset');
|
---|
30 | if (!descriptor) {
|
---|
31 | var superProto = gPO(Proto);
|
---|
32 | // @ts-expect-error TS can't guarantee the callback is invoked sync
|
---|
33 | descriptor = gOPD(superProto, 'byteOffset');
|
---|
34 | }
|
---|
35 | // Opera 12.16 has a magic byteOffset data property on instances AND on Proto
|
---|
36 | if (descriptor && descriptor.get) {
|
---|
37 | getters[typedArray] = callBind(descriptor.get);
|
---|
38 | } else if (oDP) {
|
---|
39 | // this is likely an engine where instances have a magic byteOffset data property
|
---|
40 | var arr = new global[typedArray](2);
|
---|
41 | // @ts-expect-error TS can't guarantee the callback is invoked sync
|
---|
42 | descriptor = gOPD(arr, 'byteOffset');
|
---|
43 | if (descriptor && descriptor.configurable) {
|
---|
44 | oDP(arr, 'length', { value: 3 });
|
---|
45 | }
|
---|
46 | if (arr.length === 2) {
|
---|
47 | getters[typedArray] = getByteOffset;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 | });
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** @type {ByteOffsetGetter} */
|
---|
55 | var tryTypedArrays = function tryAllTypedArrays(value) {
|
---|
56 | /** @type {number} */ var foundOffset;
|
---|
57 | forEach(getters, /** @type {(getter: ByteOffsetGetter) => void} */ function (getter) {
|
---|
58 | if (typeof foundOffset !== 'number') {
|
---|
59 | try {
|
---|
60 | var offset = getter(value);
|
---|
61 | if (typeof offset === 'number') {
|
---|
62 | foundOffset = offset;
|
---|
63 | }
|
---|
64 | } catch (e) {}
|
---|
65 | }
|
---|
66 | });
|
---|
67 | // @ts-expect-error TS can't guarantee the callback is invoked sync
|
---|
68 | return foundOffset;
|
---|
69 | };
|
---|
70 |
|
---|
71 | var isTypedArray = require('is-typed-array');
|
---|
72 |
|
---|
73 | /** @type {import('.')} */
|
---|
74 | module.exports = function typedArrayByteOffset(value) {
|
---|
75 | if (!isTypedArray(value)) {
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | return tryTypedArrays(value);
|
---|
79 | };
|
---|