Version 1
[yaffs-website] / node_modules / core-js / modules / _typed-array.js
1 'use strict';
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')
41     , dP                  = $DP.f
42     , gOPD                = $GOPD.f
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
78     , VIEW                = $typed.VIEW
79     , WRONG_LENGTH        = 'Wrong length!';
80
81   var $map = createArrayMethod(1, function(O, length){
82     return allocate(speciesConstructor(O, O[DEF_CONSTRUCTOR]), length);
83   });
84
85   var LITTLE_ENDIAN = fails(function(){
86     return new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
87   });
88
89   var FORCED_SET = !!Uint8Array && !!Uint8Array[PROTOTYPE].set && fails(function(){
90     new Uint8Array(1).set({});
91   });
92
93   var strictToLength = function(it, SAME){
94     if(it === undefined)throw TypeError(WRONG_LENGTH);
95     var number = +it
96       , length = toLength(it);
97     if(SAME && !same(number, length))throw RangeError(WRONG_LENGTH);
98     return length;
99   };
100
101   var toOffset = function(it, BYTES){
102     var offset = toInteger(it);
103     if(offset < 0 || offset % BYTES)throw RangeError('Wrong offset!');
104     return offset;
105   };
106
107   var validate = function(it){
108     if(isObject(it) && TYPED_ARRAY in it)return it;
109     throw TypeError(it + ' is not a typed array!');
110   };
111
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);
116   };
117
118   var speciesFromList = function(O, list){
119     return fromList(speciesConstructor(O, O[DEF_CONSTRUCTOR]), list);
120   };
121
122   var fromList = function(C, list){
123     var index  = 0
124       , length = list.length
125       , result = allocate(C, length);
126     while(length > index)result[index] = list[index++];
127     return result;
128   };
129
130   var addGetter = function(it, key, internal){
131     dP(it, key, {get: function(){ return this._d[internal]; }});
132   };
133
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);
144       } O = values;
145     }
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];
149     }
150     return result;
151   };
152
153   var $of = function of(/*...items*/){
154     var index  = 0
155       , length = arguments.length
156       , result = allocate(this, length);
157     while(length > index)result[index] = arguments[index++];
158     return result;
159   };
160
161   // iOS Safari 6.x fails here
162   var TO_LOCALE_BUG = !!Uint8Array && fails(function(){ arrayToLocaleString.call(new Uint8Array(1)); });
163
164   var $toLocaleString = function toLocaleString(){
165     return arrayToLocaleString.apply(TO_LOCALE_BUG ? arraySlice.call(validate(this)) : validate(this), arguments);
166   };
167
168   var proto = {
169     copyWithin: function copyWithin(target, start /*, end */){
170       return arrayCopyWithin.call(validate(this), target, start, arguments.length > 2 ? arguments[2] : undefined);
171     },
172     every: function every(callbackfn /*, thisArg */){
173       return arrayEvery(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
174     },
175     fill: function fill(value /*, start, end */){ // eslint-disable-line no-unused-vars
176       return arrayFill.apply(validate(this), arguments);
177     },
178     filter: function filter(callbackfn /*, thisArg */){
179       return speciesFromList(this, arrayFilter(validate(this), callbackfn,
180         arguments.length > 1 ? arguments[1] : undefined));
181     },
182     find: function find(predicate /*, thisArg */){
183       return arrayFind(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
184     },
185     findIndex: function findIndex(predicate /*, thisArg */){
186       return arrayFindIndex(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
187     },
188     forEach: function forEach(callbackfn /*, thisArg */){
189       arrayForEach(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
190     },
191     indexOf: function indexOf(searchElement /*, fromIndex */){
192       return arrayIndexOf(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
193     },
194     includes: function includes(searchElement /*, fromIndex */){
195       return arrayIncludes(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
196     },
197     join: function join(separator){ // eslint-disable-line no-unused-vars
198       return arrayJoin.apply(validate(this), arguments);
199     },
200     lastIndexOf: function lastIndexOf(searchElement /*, fromIndex */){ // eslint-disable-line no-unused-vars
201       return arrayLastIndexOf.apply(validate(this), arguments);
202     },
203     map: function map(mapfn /*, thisArg */){
204       return $map(validate(this), mapfn, arguments.length > 1 ? arguments[1] : undefined);
205     },
206     reduce: function reduce(callbackfn /*, initialValue */){ // eslint-disable-line no-unused-vars
207       return arrayReduce.apply(validate(this), arguments);
208     },
209     reduceRight: function reduceRight(callbackfn /*, initialValue */){ // eslint-disable-line no-unused-vars
210       return arrayReduceRight.apply(validate(this), arguments);
211     },
212     reverse: function reverse(){
213       var that   = this
214         , length = validate(that).length
215         , middle = Math.floor(length / 2)
216         , index  = 0
217         , value;
218       while(index < middle){
219         value         = that[index];
220         that[index++] = that[--length];
221         that[length]  = value;
222       } return that;
223     },
224     some: function some(callbackfn /*, thisArg */){
225       return arraySome(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
226     },
227     sort: function sort(comparefn){
228       return arraySort.call(validate(this), comparefn);
229     },
230     subarray: function subarray(begin, end){
231       var O      = validate(this)
232         , length = O.length
233         , $begin = toIndex(begin, length);
234       return new (speciesConstructor(O, O[DEF_CONSTRUCTOR]))(
235         O.buffer,
236         O.byteOffset + $begin * O.BYTES_PER_ELEMENT,
237         toLength((end === undefined ? length : toIndex(end, length)) - $begin)
238       );
239     }
240   };
241
242   var $slice = function slice(start, end){
243     return speciesFromList(this, arraySlice.call(validate(this), start, end));
244   };
245
246   var $set = function set(arrayLike /*, offset */){
247     validate(this);
248     var offset = toOffset(arguments[1], 1)
249       , length = this.length
250       , src    = toObject(arrayLike)
251       , len    = toLength(src.length)
252       , index  = 0;
253     if(len + offset > length)throw RangeError(WRONG_LENGTH);
254     while(index < len)this[offset + index] = src[index++];
255   };
256
257   var $iterators = {
258     entries: function entries(){
259       return arrayEntries.call(validate(this));
260     },
261     keys: function keys(){
262       return arrayKeys.call(validate(this));
263     },
264     values: function values(){
265       return arrayValues.call(validate(this));
266     }
267   };
268
269   var isTAIndex = function(target, key){
270     return isObject(target)
271       && target[TYPED_ARRAY]
272       && typeof key != 'symbol'
273       && key in target
274       && String(+key) == String(key);
275   };
276   var $getDesc = function getOwnPropertyDescriptor(target, key){
277     return isTAIndex(target, key = toPrimitive(key, true))
278       ? propertyDesc(2, target[key])
279       : gOPD(target, key);
280   };
281   var $setDesc = function defineProperty(target, key, desc){
282     if(isTAIndex(target, key = toPrimitive(key, true))
283       && isObject(desc)
284       && has(desc, 'value')
285       && !has(desc, 'get')
286       && !has(desc, 'set')
287       // TODO: add validation descriptor w/o calling accessors
288       && !desc.configurable
289       && (!has(desc, 'writable') || desc.writable)
290       && (!has(desc, 'enumerable') || desc.enumerable)
291     ){
292       target[key] = desc.value;
293       return target;
294     } else return dP(target, key, desc);
295   };
296
297   if(!ALL_CONSTRUCTORS){
298     $GOPD.f = $getDesc;
299     $DP.f   = $setDesc;
300   }
301
302   $export($export.S + $export.F * !ALL_CONSTRUCTORS, 'Object', {
303     getOwnPropertyDescriptor: $getDesc,
304     defineProperty:           $setDesc
305   });
306
307   if(fails(function(){ arrayToString.call({}); })){
308     arrayToString = arrayToLocaleString = function toString(){
309       return arrayJoin.call(this);
310     }
311   }
312
313   var $TypedArrayPrototype$ = redefineAll({}, proto);
314   redefineAll($TypedArrayPrototype$, $iterators);
315   hide($TypedArrayPrototype$, ITERATOR, $iterators.values);
316   redefineAll($TypedArrayPrototype$, {
317     slice:          $slice,
318     set:            $set,
319     constructor:    function(){ /* noop */ },
320     toString:       arrayToString,
321     toLocaleString: $toLocaleString
322   });
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]; }
329   });
330
331   module.exports = function(KEY, BYTES, wrapper, CLAMPED){
332     CLAMPED = !!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
341       , O          = {}
342       , TypedArrayPrototype = TypedArray && TypedArray[PROTOTYPE];
343     var getter = function(that, index){
344       var data = that._d;
345       return data.v[GETTER](index * BYTES + data.o, LITTLE_ENDIAN);
346     };
347     var setter = function(that, index, value){
348       var data = that._d;
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);
351     };
352     var addElement = function(that, index){
353       dP(that, index, {
354         get: function(){
355           return getter(this, index);
356         },
357         set: function(value){
358           return setter(this, index, value);
359         },
360         enumerable: true
361       });
362     };
363     if(FORCED){
364       TypedArray = wrapper(function(that, data, $offset, $length){
365         anInstance(that, TypedArray, NAME, '_d');
366         var index  = 0
367           , offset = 0
368           , buffer, byteLength, length, klass;
369         if(!isObject(data)){
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){
374           buffer = data;
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);
381           } else {
382             byteLength = toLength($length) * BYTES;
383             if(byteLength + offset > $len)throw RangeError(WRONG_LENGTH);
384           }
385           length = byteLength / BYTES;
386         } else if(TYPED_ARRAY in data){
387           return fromList(TypedArray, data);
388         } else {
389           return $from.call(TypedArray, data);
390         }
391         hide(that, '_d', {
392           b: buffer,
393           o: offset,
394           l: byteLength,
395           e: length,
396           v: new $DataView(buffer)
397         });
398         while(index < length)addElement(that, index++);
399       });
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
407     }, true)){
408       TypedArray = wrapper(function(that, data, $offset, $length){
409         anInstance(that, TypedArray, NAME);
410         var klass;
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))
419               : new Base(data);
420         }
421         if(TYPED_ARRAY in data)return fromList(TypedArray, data);
422         return $from.call(TypedArray, data);
423       });
424       arrayForEach(TAC !== Function.prototype ? gOPN(Base).concat(gOPN(TAC)) : gOPN(Base), function(key){
425         if(!(key in TypedArray))hide(TypedArray, key, Base[key]);
426       });
427       TypedArray[PROTOTYPE] = TypedArrayPrototype;
428       if(!LIBRARY)TypedArrayPrototype.constructor = TypedArray;
429     }
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);
437
438     if(CLAMPED ? new TypedArray(1)[TAG] != NAME : !(TAG in TypedArrayPrototype)){
439       dP(TypedArrayPrototype, TAG, {
440         get: function(){ return NAME; }
441       });
442     }
443
444     O[NAME] = TypedArray;
445
446     $export($export.G + $export.W + $export.F * (TypedArray != Base), O);
447
448     $export($export.S, NAME, {
449       BYTES_PER_ELEMENT: BYTES,
450       from: $from,
451       of: $of
452     });
453
454     if(!(BYTES_PER_ELEMENT in TypedArrayPrototype))hide(TypedArrayPrototype, BYTES_PER_ELEMENT, BYTES);
455
456     $export($export.P, NAME, proto);
457
458     setSpecies(NAME);
459
460     $export($export.P + $export.F * FORCED_SET, NAME, {set: $set});
461
462     $export($export.P + $export.F * !CORRECT_ITER_NAME, NAME, $iterators);
463
464     $export($export.P + $export.F * (TypedArrayPrototype.toString != arrayToString), NAME, {toString: arrayToString});
465
466     $export($export.P + $export.F * fails(function(){
467       new TypedArray(1).slice();
468     }), NAME, {slice: $slice});
469
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});
475
476     Iterators[NAME] = CORRECT_ITER_NAME ? $nativeIterator : $iterator;
477     if(!LIBRARY && !CORRECT_ITER_NAME)hide(TypedArrayPrototype, ITERATOR, $iterator);
478   };
479 } else module.exports = function(){ /* empty */ };