Skip to content
Permalink
Newer
Older
100644 66 lines (59 sloc) 1.81 KB
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var forEach = require('for-each');
4
var callBind = require('call-bind');
5
6
var typedArrays = require('available-typed-arrays')();
7
8
var getters = {};
9
var hasProto = require('has-proto')();
10
11
var gOPD = Object.getOwnPropertyDescriptor;
12
var oDP = Object.defineProperty;
13
if (gOPD) {
14
var getByteOffset = function (x) {
15
return x.byteOffset;
16
};
17
forEach(typedArrays, function (typedArray) {
18
// In Safari 7, Typed Array constructors are typeof object
19
if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
20
var Proto = global[typedArray].prototype;
21
var descriptor = gOPD(Proto, 'byteOffset');
22
if (!descriptor && hasProto) {
23
var superProto = Proto.__proto__; // eslint-disable-line no-proto
24
descriptor = gOPD(superProto, 'byteOffset');
25
}
26
// Opera 12.16 has a magic byteOffset data property on instances AND on Proto
27
if (descriptor && descriptor.get) {
28
getters[typedArray] = callBind(descriptor.get);
29
} else if (oDP) {
30
// this is likely an engine where instances have a magic byteOffset data property
31
var arr = new global[typedArray](2);
32
descriptor = gOPD(arr, 'byteOffset');
33
if (descriptor && descriptor.configurable) {
34
oDP(arr, 'length', { value: 3 });
35
}
36
if (arr.length === 2) {
37
getters[typedArray] = getByteOffset;
38
}
39
}
40
}
41
});
42
}
43
44
var tryTypedArrays = function tryAllTypedArrays(value) {
45
var foundOffset;
46
forEach(getters, function (getter) {
47
if (typeof foundOffset !== 'number') {
48
try {
49
var offset = getter(value);
50
if (typeof offset === 'number') {
51
foundOffset = offset;
52
}
53
} catch (e) {}
54
}
55
});
56
return foundOffset;
57
};
58
59
var isTypedArray = require('is-typed-array');
60
61
module.exports = function typedArrayByteOffset(value) {
62
if (!isTypedArray(value)) {
63
return false;
64
}
65
return tryTypedArrays(value);
66
};