2 if(require('./_descriptors')){
3 var LIBRARY = require('./_library')
4 , global = require('./_global')
5 , fails = require('./_fails')
6 , $export = require('./_export')
7 , $typed = require('./_typed')
8 , $buffer = require('./_typed-buffer')
9 , ctx = require('./_ctx')
10 , anInstance = require('./_an-instance')
11 , propertyDesc = require('./_property-desc')
12 , hide = require('./_hide')
13 , redefineAll = require('./_redefine-all')
14 , toInteger = require('./_to-integer')
15 , toLength = require('./_to-length')
16 , toIndex = require('./_to-index')
17 , toPrimitive = require('./_to-primitive')
18 , has = require('./_has')
19 , same = require('./_same-value')
20 , classof = require('./_classof')
21 , isObject = require('./_is-object')
22 , toObject = require('./_to-object')
23 , isArrayIter = require('./_is-array-iter')
24 , create = require('./_object-create')
25 , getPrototypeOf = require('./_object-gpo')
26 , gOPN = require('./_object-gopn').f
27 , getIterFn = require('./core.get-iterator-method')
28 , uid = require('./_uid')
29 , wks = require('./_wks')
30 , createArrayMethod = require('./_array-methods')
31 , createArrayIncludes = require('./_array-includes')
32 , speciesConstructor = require('./_species-constructor')
33 , ArrayIterators = require('./es6.array.iterator')
34 , Iterators = require('./_iterators')
35 , $iterDetect = require('./_iter-detect')
36 , setSpecies = require('./_set-species')
37 , arrayFill = require('./_array-fill')
38 , arrayCopyWithin = require('./_array-copy-within')
39 , $DP = require('./_object-dp')
40 , $GOPD = require('./_object-gopd')
43 , RangeError = global.RangeError
44 , TypeError = global.TypeError
45 , Uint8Array = global.Uint8Array
46 , ARRAY_BUFFER = 'ArrayBuffer'
47 , SHARED_BUFFER = 'Shared' + ARRAY_BUFFER
48 , BYTES_PER_ELEMENT = 'BYTES_PER_ELEMENT'
49 , PROTOTYPE = 'prototype'
50 , ArrayProto = Array[PROTOTYPE]
51 , $ArrayBuffer = $buffer.ArrayBuffer
52 , $DataView = $buffer.DataView
53 , arrayForEach = createArrayMethod(0)
54 , arrayFilter = createArrayMethod(2)
55 , arraySome = createArrayMethod(3)
56 , arrayEvery = createArrayMethod(4)
57 , arrayFind = createArrayMethod(5)
58 , arrayFindIndex = createArrayMethod(6)
59 , arrayIncludes = createArrayIncludes(true)
60 , arrayIndexOf = createArrayIncludes(false)
61 , arrayValues = ArrayIterators.values
62 , arrayKeys = ArrayIterators.keys
63 , arrayEntries = ArrayIterators.entries
64 , arrayLastIndexOf = ArrayProto.lastIndexOf
65 , arrayReduce = ArrayProto.reduce
66 , arrayReduceRight = ArrayProto.reduceRight
67 , arrayJoin = ArrayProto.join
68 , arraySort = ArrayProto.sort
69 , arraySlice = ArrayProto.slice
70 , arrayToString = ArrayProto.toString
71 , arrayToLocaleString = ArrayProto.toLocaleString
72 , ITERATOR = wks('iterator')
73 , TAG = wks('toStringTag')
74 , TYPED_CONSTRUCTOR = uid('typed_constructor')
75 , DEF_CONSTRUCTOR = uid('def_constructor')
76 , ALL_CONSTRUCTORS = $typed.CONSTR
77 , TYPED_ARRAY = $typed.TYPED
79 , WRONG_LENGTH = 'Wrong length!';
81 var $map = createArrayMethod(1, function(O, length){
82 return allocate(speciesConstructor(O, O[DEF_CONSTRUCTOR]), length);
85 var LITTLE_ENDIAN = fails(function(){
86 return new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
89 var FORCED_SET = !!Uint8Array && !!Uint8Array[PROTOTYPE].set && fails(function(){
90 new Uint8Array(1).set({});
93 var strictToLength = function(it, SAME){
94 if(it === undefined)throw TypeError(WRONG_LENGTH);
96 , length = toLength(it);
97 if(SAME && !same(number, length))throw RangeError(WRONG_LENGTH);
101 var toOffset = function(it, BYTES){
102 var offset = toInteger(it);
103 if(offset < 0 || offset % BYTES)throw RangeError('Wrong offset!');
107 var validate = function(it){
108 if(isObject(it) && TYPED_ARRAY in it)return it;
109 throw TypeError(it + ' is not a typed array!');
112 var allocate = function(C, length){
113 if(!(isObject(C) && TYPED_CONSTRUCTOR in C)){
114 throw TypeError('It is not a typed array constructor!');
115 } return new C(length);
118 var speciesFromList = function(O, list){
119 return fromList(speciesConstructor(O, O[DEF_CONSTRUCTOR]), list);
122 var fromList = function(C, list){
124 , length = list.length
125 , result = allocate(C, length);
126 while(length > index)result[index] = list[index++];
130 var addGetter = function(it, key, internal){
131 dP(it, key, {get: function(){ return this._d[internal]; }});
134 var $from = function from(source /*, mapfn, thisArg */){
135 var O = toObject(source)
136 , aLen = arguments.length
137 , mapfn = aLen > 1 ? arguments[1] : undefined
138 , mapping = mapfn !== undefined
139 , iterFn = getIterFn(O)
140 , i, length, values, result, step, iterator;
141 if(iterFn != undefined && !isArrayIter(iterFn)){
142 for(iterator = iterFn.call(O), values = [], i = 0; !(step = iterator.next()).done; i++){
143 values.push(step.value);
146 if(mapping && aLen > 2)mapfn = ctx(mapfn, arguments[2], 2);
147 for(i = 0, length = toLength(O.length), result = allocate(this, length); length > i; i++){
148 result[i] = mapping ? mapfn(O[i], i) : O[i];
153 var $of = function of(/*...items*/){
155 , length = arguments.length
156 , result = allocate(this, length);
157 while(length > index)result[index] = arguments[index++];
161 // iOS Safari 6.x fails here
162 var TO_LOCALE_BUG = !!Uint8Array && fails(function(){ arrayToLocaleString.call(new Uint8Array(1)); });
164 var $toLocaleString = function toLocaleString(){
165 return arrayToLocaleString.apply(TO_LOCALE_BUG ? arraySlice.call(validate(this)) : validate(this), arguments);
169 copyWithin: function copyWithin(target, start /*, end */){
170 return arrayCopyWithin.call(validate(this), target, start, arguments.length > 2 ? arguments[2] : undefined);
172 every: function every(callbackfn /*, thisArg */){
173 return arrayEvery(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
175 fill: function fill(value /*, start, end */){ // eslint-disable-line no-unused-vars
176 return arrayFill.apply(validate(this), arguments);
178 filter: function filter(callbackfn /*, thisArg */){
179 return speciesFromList(this, arrayFilter(validate(this), callbackfn,
180 arguments.length > 1 ? arguments[1] : undefined));
182 find: function find(predicate /*, thisArg */){
183 return arrayFind(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
185 findIndex: function findIndex(predicate /*, thisArg */){
186 return arrayFindIndex(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
188 forEach: function forEach(callbackfn /*, thisArg */){
189 arrayForEach(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
191 indexOf: function indexOf(searchElement /*, fromIndex */){
192 return arrayIndexOf(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
194 includes: function includes(searchElement /*, fromIndex */){
195 return arrayIncludes(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
197 join: function join(separator){ // eslint-disable-line no-unused-vars
198 return arrayJoin.apply(validate(this), arguments);
200 lastIndexOf: function lastIndexOf(searchElement /*, fromIndex */){ // eslint-disable-line no-unused-vars
201 return arrayLastIndexOf.apply(validate(this), arguments);
203 map: function map(mapfn /*, thisArg */){
204 return $map(validate(this), mapfn, arguments.length > 1 ? arguments[1] : undefined);
206 reduce: function reduce(callbackfn /*, initialValue */){ // eslint-disable-line no-unused-vars
207 return arrayReduce.apply(validate(this), arguments);
209 reduceRight: function reduceRight(callbackfn /*, initialValue */){ // eslint-disable-line no-unused-vars
210 return arrayReduceRight.apply(validate(this), arguments);
212 reverse: function reverse(){
214 , length = validate(that).length
215 , middle = Math.floor(length / 2)
218 while(index < middle){
220 that[index++] = that[--length];
221 that[length] = value;
224 some: function some(callbackfn /*, thisArg */){
225 return arraySome(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
227 sort: function sort(comparefn){
228 return arraySort.call(validate(this), comparefn);
230 subarray: function subarray(begin, end){
231 var O = validate(this)
233 , $begin = toIndex(begin, length);
234 return new (speciesConstructor(O, O[DEF_CONSTRUCTOR]))(
236 O.byteOffset + $begin * O.BYTES_PER_ELEMENT,
237 toLength((end === undefined ? length : toIndex(end, length)) - $begin)
242 var $slice = function slice(start, end){
243 return speciesFromList(this, arraySlice.call(validate(this), start, end));
246 var $set = function set(arrayLike /*, offset */){
248 var offset = toOffset(arguments[1], 1)
249 , length = this.length
250 , src = toObject(arrayLike)
251 , len = toLength(src.length)
253 if(len + offset > length)throw RangeError(WRONG_LENGTH);
254 while(index < len)this[offset + index] = src[index++];
258 entries: function entries(){
259 return arrayEntries.call(validate(this));
261 keys: function keys(){
262 return arrayKeys.call(validate(this));
264 values: function values(){
265 return arrayValues.call(validate(this));
269 var isTAIndex = function(target, key){
270 return isObject(target)
271 && target[TYPED_ARRAY]
272 && typeof key != 'symbol'
274 && String(+key) == String(key);
276 var $getDesc = function getOwnPropertyDescriptor(target, key){
277 return isTAIndex(target, key = toPrimitive(key, true))
278 ? propertyDesc(2, target[key])
281 var $setDesc = function defineProperty(target, key, desc){
282 if(isTAIndex(target, key = toPrimitive(key, true))
284 && has(desc, 'value')
287 // TODO: add validation descriptor w/o calling accessors
288 && !desc.configurable
289 && (!has(desc, 'writable') || desc.writable)
290 && (!has(desc, 'enumerable') || desc.enumerable)
292 target[key] = desc.value;
294 } else return dP(target, key, desc);
297 if(!ALL_CONSTRUCTORS){
302 $export($export.S + $export.F * !ALL_CONSTRUCTORS, 'Object', {
303 getOwnPropertyDescriptor: $getDesc,
304 defineProperty: $setDesc
307 if(fails(function(){ arrayToString.call({}); })){
308 arrayToString = arrayToLocaleString = function toString(){
309 return arrayJoin.call(this);
313 var $TypedArrayPrototype$ = redefineAll({}, proto);
314 redefineAll($TypedArrayPrototype$, $iterators);
315 hide($TypedArrayPrototype$, ITERATOR, $iterators.values);
316 redefineAll($TypedArrayPrototype$, {
319 constructor: function(){ /* noop */ },
320 toString: arrayToString,
321 toLocaleString: $toLocaleString
323 addGetter($TypedArrayPrototype$, 'buffer', 'b');
324 addGetter($TypedArrayPrototype$, 'byteOffset', 'o');
325 addGetter($TypedArrayPrototype$, 'byteLength', 'l');
326 addGetter($TypedArrayPrototype$, 'length', 'e');
327 dP($TypedArrayPrototype$, TAG, {
328 get: function(){ return this[TYPED_ARRAY]; }
331 module.exports = function(KEY, BYTES, wrapper, CLAMPED){
333 var NAME = KEY + (CLAMPED ? 'Clamped' : '') + 'Array'
334 , ISNT_UINT8 = NAME != 'Uint8Array'
335 , GETTER = 'get' + KEY
336 , SETTER = 'set' + KEY
337 , TypedArray = global[NAME]
338 , Base = TypedArray || {}
339 , TAC = TypedArray && getPrototypeOf(TypedArray)
340 , FORCED = !TypedArray || !$typed.ABV
342 , TypedArrayPrototype = TypedArray && TypedArray[PROTOTYPE];
343 var getter = function(that, index){
345 return data.v[GETTER](index * BYTES + data.o, LITTLE_ENDIAN);
347 var setter = function(that, index, value){
349 if(CLAMPED)value = (value = Math.round(value)) < 0 ? 0 : value > 0xff ? 0xff : value & 0xff;
350 data.v[SETTER](index * BYTES + data.o, value, LITTLE_ENDIAN);
352 var addElement = function(that, index){
355 return getter(this, index);
357 set: function(value){
358 return setter(this, index, value);
364 TypedArray = wrapper(function(that, data, $offset, $length){
365 anInstance(that, TypedArray, NAME, '_d');
368 , buffer, byteLength, length, klass;
370 length = strictToLength(data, true)
371 byteLength = length * BYTES;
372 buffer = new $ArrayBuffer(byteLength);
373 } else if(data instanceof $ArrayBuffer || (klass = classof(data)) == ARRAY_BUFFER || klass == SHARED_BUFFER){
375 offset = toOffset($offset, BYTES);
376 var $len = data.byteLength;
377 if($length === undefined){
378 if($len % BYTES)throw RangeError(WRONG_LENGTH);
379 byteLength = $len - offset;
380 if(byteLength < 0)throw RangeError(WRONG_LENGTH);
382 byteLength = toLength($length) * BYTES;
383 if(byteLength + offset > $len)throw RangeError(WRONG_LENGTH);
385 length = byteLength / BYTES;
386 } else if(TYPED_ARRAY in data){
387 return fromList(TypedArray, data);
389 return $from.call(TypedArray, data);
396 v: new $DataView(buffer)
398 while(index < length)addElement(that, index++);
400 TypedArrayPrototype = TypedArray[PROTOTYPE] = create($TypedArrayPrototype$);
401 hide(TypedArrayPrototype, 'constructor', TypedArray);
402 } else if(!$iterDetect(function(iter){
403 // V8 works with iterators, but fails in many other cases
404 // https://code.google.com/p/v8/issues/detail?id=4552
405 new TypedArray(null); // eslint-disable-line no-new
406 new TypedArray(iter); // eslint-disable-line no-new
408 TypedArray = wrapper(function(that, data, $offset, $length){
409 anInstance(that, TypedArray, NAME);
411 // `ws` module bug, temporarily remove validation length for Uint8Array
412 // https://github.com/websockets/ws/pull/645
413 if(!isObject(data))return new Base(strictToLength(data, ISNT_UINT8));
414 if(data instanceof $ArrayBuffer || (klass = classof(data)) == ARRAY_BUFFER || klass == SHARED_BUFFER){
415 return $length !== undefined
416 ? new Base(data, toOffset($offset, BYTES), $length)
417 : $offset !== undefined
418 ? new Base(data, toOffset($offset, BYTES))
421 if(TYPED_ARRAY in data)return fromList(TypedArray, data);
422 return $from.call(TypedArray, data);
424 arrayForEach(TAC !== Function.prototype ? gOPN(Base).concat(gOPN(TAC)) : gOPN(Base), function(key){
425 if(!(key in TypedArray))hide(TypedArray, key, Base[key]);
427 TypedArray[PROTOTYPE] = TypedArrayPrototype;
428 if(!LIBRARY)TypedArrayPrototype.constructor = TypedArray;
430 var $nativeIterator = TypedArrayPrototype[ITERATOR]
431 , CORRECT_ITER_NAME = !!$nativeIterator && ($nativeIterator.name == 'values' || $nativeIterator.name == undefined)
432 , $iterator = $iterators.values;
433 hide(TypedArray, TYPED_CONSTRUCTOR, true);
434 hide(TypedArrayPrototype, TYPED_ARRAY, NAME);
435 hide(TypedArrayPrototype, VIEW, true);
436 hide(TypedArrayPrototype, DEF_CONSTRUCTOR, TypedArray);
438 if(CLAMPED ? new TypedArray(1)[TAG] != NAME : !(TAG in TypedArrayPrototype)){
439 dP(TypedArrayPrototype, TAG, {
440 get: function(){ return NAME; }
444 O[NAME] = TypedArray;
446 $export($export.G + $export.W + $export.F * (TypedArray != Base), O);
448 $export($export.S, NAME, {
449 BYTES_PER_ELEMENT: BYTES,
454 if(!(BYTES_PER_ELEMENT in TypedArrayPrototype))hide(TypedArrayPrototype, BYTES_PER_ELEMENT, BYTES);
456 $export($export.P, NAME, proto);
460 $export($export.P + $export.F * FORCED_SET, NAME, {set: $set});
462 $export($export.P + $export.F * !CORRECT_ITER_NAME, NAME, $iterators);
464 $export($export.P + $export.F * (TypedArrayPrototype.toString != arrayToString), NAME, {toString: arrayToString});
466 $export($export.P + $export.F * fails(function(){
467 new TypedArray(1).slice();
468 }), NAME, {slice: $slice});
470 $export($export.P + $export.F * (fails(function(){
471 return [1, 2].toLocaleString() != new TypedArray([1, 2]).toLocaleString()
472 }) || !fails(function(){
473 TypedArrayPrototype.toLocaleString.call([1, 2]);
474 })), NAME, {toLocaleString: $toLocaleString});
476 Iterators[NAME] = CORRECT_ITER_NAME ? $nativeIterator : $iterator;
477 if(!LIBRARY && !CORRECT_ITER_NAME)hide(TypedArrayPrototype, ITERATOR, $iterator);
479 } else module.exports = function(){ /* empty */ };