91d2663b2daf49592587d6a78ff047a0518825b75defe09917fade023f0d6db01e5659198c40ca0d70e10dd40c20cc38e101fb67d971e456c77b6de11ece48 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. 'use strict';
  2. if (require('./_descriptors')) {
  3. var LIBRARY = require('./_library');
  4. var global = require('./_global');
  5. var fails = require('./_fails');
  6. var $export = require('./_export');
  7. var $typed = require('./_typed');
  8. var $buffer = require('./_typed-buffer');
  9. var ctx = require('./_ctx');
  10. var anInstance = require('./_an-instance');
  11. var propertyDesc = require('./_property-desc');
  12. var hide = require('./_hide');
  13. var redefineAll = require('./_redefine-all');
  14. var toInteger = require('./_to-integer');
  15. var toLength = require('./_to-length');
  16. var toIndex = require('./_to-index');
  17. var toAbsoluteIndex = require('./_to-absolute-index');
  18. var toPrimitive = require('./_to-primitive');
  19. var has = require('./_has');
  20. var classof = require('./_classof');
  21. var isObject = require('./_is-object');
  22. var toObject = require('./_to-object');
  23. var isArrayIter = require('./_is-array-iter');
  24. var create = require('./_object-create');
  25. var getPrototypeOf = require('./_object-gpo');
  26. var gOPN = require('./_object-gopn').f;
  27. var getIterFn = require('./core.get-iterator-method');
  28. var uid = require('./_uid');
  29. var wks = require('./_wks');
  30. var createArrayMethod = require('./_array-methods');
  31. var createArrayIncludes = require('./_array-includes');
  32. var speciesConstructor = require('./_species-constructor');
  33. var ArrayIterators = require('./es6.array.iterator');
  34. var Iterators = require('./_iterators');
  35. var $iterDetect = require('./_iter-detect');
  36. var setSpecies = require('./_set-species');
  37. var arrayFill = require('./_array-fill');
  38. var arrayCopyWithin = require('./_array-copy-within');
  39. var $DP = require('./_object-dp');
  40. var $GOPD = require('./_object-gopd');
  41. var dP = $DP.f;
  42. var gOPD = $GOPD.f;
  43. var RangeError = global.RangeError;
  44. var TypeError = global.TypeError;
  45. var Uint8Array = global.Uint8Array;
  46. var ARRAY_BUFFER = 'ArrayBuffer';
  47. var SHARED_BUFFER = 'Shared' + ARRAY_BUFFER;
  48. var BYTES_PER_ELEMENT = 'BYTES_PER_ELEMENT';
  49. var PROTOTYPE = 'prototype';
  50. var ArrayProto = Array[PROTOTYPE];
  51. var $ArrayBuffer = $buffer.ArrayBuffer;
  52. var $DataView = $buffer.DataView;
  53. var arrayForEach = createArrayMethod(0);
  54. var arrayFilter = createArrayMethod(2);
  55. var arraySome = createArrayMethod(3);
  56. var arrayEvery = createArrayMethod(4);
  57. var arrayFind = createArrayMethod(5);
  58. var arrayFindIndex = createArrayMethod(6);
  59. var arrayIncludes = createArrayIncludes(true);
  60. var arrayIndexOf = createArrayIncludes(false);
  61. var arrayValues = ArrayIterators.values;
  62. var arrayKeys = ArrayIterators.keys;
  63. var arrayEntries = ArrayIterators.entries;
  64. var arrayLastIndexOf = ArrayProto.lastIndexOf;
  65. var arrayReduce = ArrayProto.reduce;
  66. var arrayReduceRight = ArrayProto.reduceRight;
  67. var arrayJoin = ArrayProto.join;
  68. var arraySort = ArrayProto.sort;
  69. var arraySlice = ArrayProto.slice;
  70. var arrayToString = ArrayProto.toString;
  71. var arrayToLocaleString = ArrayProto.toLocaleString;
  72. var ITERATOR = wks('iterator');
  73. var TAG = wks('toStringTag');
  74. var TYPED_CONSTRUCTOR = uid('typed_constructor');
  75. var DEF_CONSTRUCTOR = uid('def_constructor');
  76. var ALL_CONSTRUCTORS = $typed.CONSTR;
  77. var TYPED_ARRAY = $typed.TYPED;
  78. var VIEW = $typed.VIEW;
  79. var WRONG_LENGTH = 'Wrong length!';
  80. var $map = createArrayMethod(1, function (O, length) {
  81. return allocate(speciesConstructor(O, O[DEF_CONSTRUCTOR]), length);
  82. });
  83. var LITTLE_ENDIAN = fails(function () {
  84. // eslint-disable-next-line no-undef
  85. return new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
  86. });
  87. var FORCED_SET = !!Uint8Array && !!Uint8Array[PROTOTYPE].set && fails(function () {
  88. new Uint8Array(1).set({});
  89. });
  90. var toOffset = function (it, BYTES) {
  91. var offset = toInteger(it);
  92. if (offset < 0 || offset % BYTES) throw RangeError('Wrong offset!');
  93. return offset;
  94. };
  95. var validate = function (it) {
  96. if (isObject(it) && TYPED_ARRAY in it) return it;
  97. throw TypeError(it + ' is not a typed array!');
  98. };
  99. var allocate = function (C, length) {
  100. if (!(isObject(C) && TYPED_CONSTRUCTOR in C)) {
  101. throw TypeError('It is not a typed array constructor!');
  102. } return new C(length);
  103. };
  104. var speciesFromList = function (O, list) {
  105. return fromList(speciesConstructor(O, O[DEF_CONSTRUCTOR]), list);
  106. };
  107. var fromList = function (C, list) {
  108. var index = 0;
  109. var length = list.length;
  110. var result = allocate(C, length);
  111. while (length > index) result[index] = list[index++];
  112. return result;
  113. };
  114. var addGetter = function (it, key, internal) {
  115. dP(it, key, { get: function () { return this._d[internal]; } });
  116. };
  117. var $from = function from(source /* , mapfn, thisArg */) {
  118. var O = toObject(source);
  119. var aLen = arguments.length;
  120. var mapfn = aLen > 1 ? arguments[1] : undefined;
  121. var mapping = mapfn !== undefined;
  122. var iterFn = getIterFn(O);
  123. var i, length, values, result, step, iterator;
  124. if (iterFn != undefined && !isArrayIter(iterFn)) {
  125. for (iterator = iterFn.call(O), values = [], i = 0; !(step = iterator.next()).done; i++) {
  126. values.push(step.value);
  127. } O = values;
  128. }
  129. if (mapping && aLen > 2) mapfn = ctx(mapfn, arguments[2], 2);
  130. for (i = 0, length = toLength(O.length), result = allocate(this, length); length > i; i++) {
  131. result[i] = mapping ? mapfn(O[i], i) : O[i];
  132. }
  133. return result;
  134. };
  135. var $of = function of(/* ...items */) {
  136. var index = 0;
  137. var length = arguments.length;
  138. var result = allocate(this, length);
  139. while (length > index) result[index] = arguments[index++];
  140. return result;
  141. };
  142. // iOS Safari 6.x fails here
  143. var TO_LOCALE_BUG = !!Uint8Array && fails(function () { arrayToLocaleString.call(new Uint8Array(1)); });
  144. var $toLocaleString = function toLocaleString() {
  145. return arrayToLocaleString.apply(TO_LOCALE_BUG ? arraySlice.call(validate(this)) : validate(this), arguments);
  146. };
  147. var proto = {
  148. copyWithin: function copyWithin(target, start /* , end */) {
  149. return arrayCopyWithin.call(validate(this), target, start, arguments.length > 2 ? arguments[2] : undefined);
  150. },
  151. every: function every(callbackfn /* , thisArg */) {
  152. return arrayEvery(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
  153. },
  154. fill: function fill(value /* , start, end */) { // eslint-disable-line no-unused-vars
  155. return arrayFill.apply(validate(this), arguments);
  156. },
  157. filter: function filter(callbackfn /* , thisArg */) {
  158. return speciesFromList(this, arrayFilter(validate(this), callbackfn,
  159. arguments.length > 1 ? arguments[1] : undefined));
  160. },
  161. find: function find(predicate /* , thisArg */) {
  162. return arrayFind(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
  163. },
  164. findIndex: function findIndex(predicate /* , thisArg */) {
  165. return arrayFindIndex(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
  166. },
  167. forEach: function forEach(callbackfn /* , thisArg */) {
  168. arrayForEach(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
  169. },
  170. indexOf: function indexOf(searchElement /* , fromIndex */) {
  171. return arrayIndexOf(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
  172. },
  173. includes: function includes(searchElement /* , fromIndex */) {
  174. return arrayIncludes(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
  175. },
  176. join: function join(separator) { // eslint-disable-line no-unused-vars
  177. return arrayJoin.apply(validate(this), arguments);
  178. },
  179. lastIndexOf: function lastIndexOf(searchElement /* , fromIndex */) { // eslint-disable-line no-unused-vars
  180. return arrayLastIndexOf.apply(validate(this), arguments);
  181. },
  182. map: function map(mapfn /* , thisArg */) {
  183. return $map(validate(this), mapfn, arguments.length > 1 ? arguments[1] : undefined);
  184. },
  185. reduce: function reduce(callbackfn /* , initialValue */) { // eslint-disable-line no-unused-vars
  186. return arrayReduce.apply(validate(this), arguments);
  187. },
  188. reduceRight: function reduceRight(callbackfn /* , initialValue */) { // eslint-disable-line no-unused-vars
  189. return arrayReduceRight.apply(validate(this), arguments);
  190. },
  191. reverse: function reverse() {
  192. var that = this;
  193. var length = validate(that).length;
  194. var middle = Math.floor(length / 2);
  195. var index = 0;
  196. var value;
  197. while (index < middle) {
  198. value = that[index];
  199. that[index++] = that[--length];
  200. that[length] = value;
  201. } return that;
  202. },
  203. some: function some(callbackfn /* , thisArg */) {
  204. return arraySome(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
  205. },
  206. sort: function sort(comparefn) {
  207. return arraySort.call(validate(this), comparefn);
  208. },
  209. subarray: function subarray(begin, end) {
  210. var O = validate(this);
  211. var length = O.length;
  212. var $begin = toAbsoluteIndex(begin, length);
  213. return new (speciesConstructor(O, O[DEF_CONSTRUCTOR]))(
  214. O.buffer,
  215. O.byteOffset + $begin * O.BYTES_PER_ELEMENT,
  216. toLength((end === undefined ? length : toAbsoluteIndex(end, length)) - $begin)
  217. );
  218. }
  219. };
  220. var $slice = function slice(start, end) {
  221. return speciesFromList(this, arraySlice.call(validate(this), start, end));
  222. };
  223. var $set = function set(arrayLike /* , offset */) {
  224. validate(this);
  225. var offset = toOffset(arguments[1], 1);
  226. var length = this.length;
  227. var src = toObject(arrayLike);
  228. var len = toLength(src.length);
  229. var index = 0;
  230. if (len + offset > length) throw RangeError(WRONG_LENGTH);
  231. while (index < len) this[offset + index] = src[index++];
  232. };
  233. var $iterators = {
  234. entries: function entries() {
  235. return arrayEntries.call(validate(this));
  236. },
  237. keys: function keys() {
  238. return arrayKeys.call(validate(this));
  239. },
  240. values: function values() {
  241. return arrayValues.call(validate(this));
  242. }
  243. };
  244. var isTAIndex = function (target, key) {
  245. return isObject(target)
  246. && target[TYPED_ARRAY]
  247. && typeof key != 'symbol'
  248. && key in target
  249. && String(+key) == String(key);
  250. };
  251. var $getDesc = function getOwnPropertyDescriptor(target, key) {
  252. return isTAIndex(target, key = toPrimitive(key, true))
  253. ? propertyDesc(2, target[key])
  254. : gOPD(target, key);
  255. };
  256. var $setDesc = function defineProperty(target, key, desc) {
  257. if (isTAIndex(target, key = toPrimitive(key, true))
  258. && isObject(desc)
  259. && has(desc, 'value')
  260. && !has(desc, 'get')
  261. && !has(desc, 'set')
  262. // TODO: add validation descriptor w/o calling accessors
  263. && !desc.configurable
  264. && (!has(desc, 'writable') || desc.writable)
  265. && (!has(desc, 'enumerable') || desc.enumerable)
  266. ) {
  267. target[key] = desc.value;
  268. return target;
  269. } return dP(target, key, desc);
  270. };
  271. if (!ALL_CONSTRUCTORS) {
  272. $GOPD.f = $getDesc;
  273. $DP.f = $setDesc;
  274. }
  275. $export($export.S + $export.F * !ALL_CONSTRUCTORS, 'Object', {
  276. getOwnPropertyDescriptor: $getDesc,
  277. defineProperty: $setDesc
  278. });
  279. if (fails(function () { arrayToString.call({}); })) {
  280. arrayToString = arrayToLocaleString = function toString() {
  281. return arrayJoin.call(this);
  282. };
  283. }
  284. var $TypedArrayPrototype$ = redefineAll({}, proto);
  285. redefineAll($TypedArrayPrototype$, $iterators);
  286. hide($TypedArrayPrototype$, ITERATOR, $iterators.values);
  287. redefineAll($TypedArrayPrototype$, {
  288. slice: $slice,
  289. set: $set,
  290. constructor: function () { /* noop */ },
  291. toString: arrayToString,
  292. toLocaleString: $toLocaleString
  293. });
  294. addGetter($TypedArrayPrototype$, 'buffer', 'b');
  295. addGetter($TypedArrayPrototype$, 'byteOffset', 'o');
  296. addGetter($TypedArrayPrototype$, 'byteLength', 'l');
  297. addGetter($TypedArrayPrototype$, 'length', 'e');
  298. dP($TypedArrayPrototype$, TAG, {
  299. get: function () { return this[TYPED_ARRAY]; }
  300. });
  301. // eslint-disable-next-line max-statements
  302. module.exports = function (KEY, BYTES, wrapper, CLAMPED) {
  303. CLAMPED = !!CLAMPED;
  304. var NAME = KEY + (CLAMPED ? 'Clamped' : '') + 'Array';
  305. var GETTER = 'get' + KEY;
  306. var SETTER = 'set' + KEY;
  307. var TypedArray = global[NAME];
  308. var Base = TypedArray || {};
  309. var TAC = TypedArray && getPrototypeOf(TypedArray);
  310. var FORCED = !TypedArray || !$typed.ABV;
  311. var O = {};
  312. var TypedArrayPrototype = TypedArray && TypedArray[PROTOTYPE];
  313. var getter = function (that, index) {
  314. var data = that._d;
  315. return data.v[GETTER](index * BYTES + data.o, LITTLE_ENDIAN);
  316. };
  317. var setter = function (that, index, value) {
  318. var data = that._d;
  319. if (CLAMPED) value = (value = Math.round(value)) < 0 ? 0 : value > 0xff ? 0xff : value & 0xff;
  320. data.v[SETTER](index * BYTES + data.o, value, LITTLE_ENDIAN);
  321. };
  322. var addElement = function (that, index) {
  323. dP(that, index, {
  324. get: function () {
  325. return getter(this, index);
  326. },
  327. set: function (value) {
  328. return setter(this, index, value);
  329. },
  330. enumerable: true
  331. });
  332. };
  333. if (FORCED) {
  334. TypedArray = wrapper(function (that, data, $offset, $length) {
  335. anInstance(that, TypedArray, NAME, '_d');
  336. var index = 0;
  337. var offset = 0;
  338. var buffer, byteLength, length, klass;
  339. if (!isObject(data)) {
  340. length = toIndex(data);
  341. byteLength = length * BYTES;
  342. buffer = new $ArrayBuffer(byteLength);
  343. } else if (data instanceof $ArrayBuffer || (klass = classof(data)) == ARRAY_BUFFER || klass == SHARED_BUFFER) {
  344. buffer = data;
  345. offset = toOffset($offset, BYTES);
  346. var $len = data.byteLength;
  347. if ($length === undefined) {
  348. if ($len % BYTES) throw RangeError(WRONG_LENGTH);
  349. byteLength = $len - offset;
  350. if (byteLength < 0) throw RangeError(WRONG_LENGTH);
  351. } else {
  352. byteLength = toLength($length) * BYTES;
  353. if (byteLength + offset > $len) throw RangeError(WRONG_LENGTH);
  354. }
  355. length = byteLength / BYTES;
  356. } else if (TYPED_ARRAY in data) {
  357. return fromList(TypedArray, data);
  358. } else {
  359. return $from.call(TypedArray, data);
  360. }
  361. hide(that, '_d', {
  362. b: buffer,
  363. o: offset,
  364. l: byteLength,
  365. e: length,
  366. v: new $DataView(buffer)
  367. });
  368. while (index < length) addElement(that, index++);
  369. });
  370. TypedArrayPrototype = TypedArray[PROTOTYPE] = create($TypedArrayPrototype$);
  371. hide(TypedArrayPrototype, 'constructor', TypedArray);
  372. } else if (!fails(function () {
  373. TypedArray(1);
  374. }) || !fails(function () {
  375. new TypedArray(-1); // eslint-disable-line no-new
  376. }) || !$iterDetect(function (iter) {
  377. new TypedArray(); // eslint-disable-line no-new
  378. new TypedArray(null); // eslint-disable-line no-new
  379. new TypedArray(1.5); // eslint-disable-line no-new
  380. new TypedArray(iter); // eslint-disable-line no-new
  381. }, true)) {
  382. TypedArray = wrapper(function (that, data, $offset, $length) {
  383. anInstance(that, TypedArray, NAME);
  384. var klass;
  385. // `ws` module bug, temporarily remove validation length for Uint8Array
  386. // https://github.com/websockets/ws/pull/645
  387. if (!isObject(data)) return new Base(toIndex(data));
  388. if (data instanceof $ArrayBuffer || (klass = classof(data)) == ARRAY_BUFFER || klass == SHARED_BUFFER) {
  389. return $length !== undefined
  390. ? new Base(data, toOffset($offset, BYTES), $length)
  391. : $offset !== undefined
  392. ? new Base(data, toOffset($offset, BYTES))
  393. : new Base(data);
  394. }
  395. if (TYPED_ARRAY in data) return fromList(TypedArray, data);
  396. return $from.call(TypedArray, data);
  397. });
  398. arrayForEach(TAC !== Function.prototype ? gOPN(Base).concat(gOPN(TAC)) : gOPN(Base), function (key) {
  399. if (!(key in TypedArray)) hide(TypedArray, key, Base[key]);
  400. });
  401. TypedArray[PROTOTYPE] = TypedArrayPrototype;
  402. if (!LIBRARY) TypedArrayPrototype.constructor = TypedArray;
  403. }
  404. var $nativeIterator = TypedArrayPrototype[ITERATOR];
  405. var CORRECT_ITER_NAME = !!$nativeIterator
  406. && ($nativeIterator.name == 'values' || $nativeIterator.name == undefined);
  407. var $iterator = $iterators.values;
  408. hide(TypedArray, TYPED_CONSTRUCTOR, true);
  409. hide(TypedArrayPrototype, TYPED_ARRAY, NAME);
  410. hide(TypedArrayPrototype, VIEW, true);
  411. hide(TypedArrayPrototype, DEF_CONSTRUCTOR, TypedArray);
  412. if (CLAMPED ? new TypedArray(1)[TAG] != NAME : !(TAG in TypedArrayPrototype)) {
  413. dP(TypedArrayPrototype, TAG, {
  414. get: function () { return NAME; }
  415. });
  416. }
  417. O[NAME] = TypedArray;
  418. $export($export.G + $export.W + $export.F * (TypedArray != Base), O);
  419. $export($export.S, NAME, {
  420. BYTES_PER_ELEMENT: BYTES
  421. });
  422. $export($export.S + $export.F * fails(function () { Base.of.call(TypedArray, 1); }), NAME, {
  423. from: $from,
  424. of: $of
  425. });
  426. if (!(BYTES_PER_ELEMENT in TypedArrayPrototype)) hide(TypedArrayPrototype, BYTES_PER_ELEMENT, BYTES);
  427. $export($export.P, NAME, proto);
  428. setSpecies(NAME);
  429. $export($export.P + $export.F * FORCED_SET, NAME, { set: $set });
  430. $export($export.P + $export.F * !CORRECT_ITER_NAME, NAME, $iterators);
  431. if (!LIBRARY && TypedArrayPrototype.toString != arrayToString) TypedArrayPrototype.toString = arrayToString;
  432. $export($export.P + $export.F * fails(function () {
  433. new TypedArray(1).slice();
  434. }), NAME, { slice: $slice });
  435. $export($export.P + $export.F * (fails(function () {
  436. return [1, 2].toLocaleString() != new TypedArray([1, 2]).toLocaleString();
  437. }) || !fails(function () {
  438. TypedArrayPrototype.toLocaleString.call([1, 2]);
  439. })), NAME, { toLocaleString: $toLocaleString });
  440. Iterators[NAME] = CORRECT_ITER_NAME ? $nativeIterator : $iterator;
  441. if (!LIBRARY && !CORRECT_ITER_NAME) hide(TypedArrayPrototype, ITERATOR, $iterator);
  442. };
  443. } else module.exports = function () { /* empty */ };