Version 1
[yaffs-website] / node_modules / core-js / library / modules / _typed-buffer.js
1 'use strict';
2 var global         = require('./_global')
3   , DESCRIPTORS    = require('./_descriptors')
4   , LIBRARY        = require('./_library')
5   , $typed         = require('./_typed')
6   , hide           = require('./_hide')
7   , redefineAll    = require('./_redefine-all')
8   , fails          = require('./_fails')
9   , anInstance     = require('./_an-instance')
10   , toInteger      = require('./_to-integer')
11   , toLength       = require('./_to-length')
12   , gOPN           = require('./_object-gopn').f
13   , dP             = require('./_object-dp').f
14   , arrayFill      = require('./_array-fill')
15   , setToStringTag = require('./_set-to-string-tag')
16   , ARRAY_BUFFER   = 'ArrayBuffer'
17   , DATA_VIEW      = 'DataView'
18   , PROTOTYPE      = 'prototype'
19   , WRONG_LENGTH   = 'Wrong length!'
20   , WRONG_INDEX    = 'Wrong index!'
21   , $ArrayBuffer   = global[ARRAY_BUFFER]
22   , $DataView      = global[DATA_VIEW]
23   , Math           = global.Math
24   , RangeError     = global.RangeError
25   , Infinity       = global.Infinity
26   , BaseBuffer     = $ArrayBuffer
27   , abs            = Math.abs
28   , pow            = Math.pow
29   , floor          = Math.floor
30   , log            = Math.log
31   , LN2            = Math.LN2
32   , BUFFER         = 'buffer'
33   , BYTE_LENGTH    = 'byteLength'
34   , BYTE_OFFSET    = 'byteOffset'
35   , $BUFFER        = DESCRIPTORS ? '_b' : BUFFER
36   , $LENGTH        = DESCRIPTORS ? '_l' : BYTE_LENGTH
37   , $OFFSET        = DESCRIPTORS ? '_o' : BYTE_OFFSET;
38
39 // IEEE754 conversions based on https://github.com/feross/ieee754
40 var packIEEE754 = function(value, mLen, nBytes){
41   var buffer = Array(nBytes)
42     , eLen   = nBytes * 8 - mLen - 1
43     , eMax   = (1 << eLen) - 1
44     , eBias  = eMax >> 1
45     , rt     = mLen === 23 ? pow(2, -24) - pow(2, -77) : 0
46     , i      = 0
47     , s      = value < 0 || value === 0 && 1 / value < 0 ? 1 : 0
48     , e, m, c;
49   value = abs(value)
50   if(value != value || value === Infinity){
51     m = value != value ? 1 : 0;
52     e = eMax;
53   } else {
54     e = floor(log(value) / LN2);
55     if(value * (c = pow(2, -e)) < 1){
56       e--;
57       c *= 2;
58     }
59     if(e + eBias >= 1){
60       value += rt / c;
61     } else {
62       value += rt * pow(2, 1 - eBias);
63     }
64     if(value * c >= 2){
65       e++;
66       c /= 2;
67     }
68     if(e + eBias >= eMax){
69       m = 0;
70       e = eMax;
71     } else if(e + eBias >= 1){
72       m = (value * c - 1) * pow(2, mLen);
73       e = e + eBias;
74     } else {
75       m = value * pow(2, eBias - 1) * pow(2, mLen);
76       e = 0;
77     }
78   }
79   for(; mLen >= 8; buffer[i++] = m & 255, m /= 256, mLen -= 8);
80   e = e << mLen | m;
81   eLen += mLen;
82   for(; eLen > 0; buffer[i++] = e & 255, e /= 256, eLen -= 8);
83   buffer[--i] |= s * 128;
84   return buffer;
85 };
86 var unpackIEEE754 = function(buffer, mLen, nBytes){
87   var eLen  = nBytes * 8 - mLen - 1
88     , eMax  = (1 << eLen) - 1
89     , eBias = eMax >> 1
90     , nBits = eLen - 7
91     , i     = nBytes - 1
92     , s     = buffer[i--]
93     , e     = s & 127
94     , m;
95   s >>= 7;
96   for(; nBits > 0; e = e * 256 + buffer[i], i--, nBits -= 8);
97   m = e & (1 << -nBits) - 1;
98   e >>= -nBits;
99   nBits += mLen;
100   for(; nBits > 0; m = m * 256 + buffer[i], i--, nBits -= 8);
101   if(e === 0){
102     e = 1 - eBias;
103   } else if(e === eMax){
104     return m ? NaN : s ? -Infinity : Infinity;
105   } else {
106     m = m + pow(2, mLen);
107     e = e - eBias;
108   } return (s ? -1 : 1) * m * pow(2, e - mLen);
109 };
110
111 var unpackI32 = function(bytes){
112   return bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
113 };
114 var packI8 = function(it){
115   return [it & 0xff];
116 };
117 var packI16 = function(it){
118   return [it & 0xff, it >> 8 & 0xff];
119 };
120 var packI32 = function(it){
121   return [it & 0xff, it >> 8 & 0xff, it >> 16 & 0xff, it >> 24 & 0xff];
122 };
123 var packF64 = function(it){
124   return packIEEE754(it, 52, 8);
125 };
126 var packF32 = function(it){
127   return packIEEE754(it, 23, 4);
128 };
129
130 var addGetter = function(C, key, internal){
131   dP(C[PROTOTYPE], key, {get: function(){ return this[internal]; }});
132 };
133
134 var get = function(view, bytes, index, isLittleEndian){
135   var numIndex = +index
136     , intIndex = toInteger(numIndex);
137   if(numIndex != intIndex || intIndex < 0 || intIndex + bytes > view[$LENGTH])throw RangeError(WRONG_INDEX);
138   var store = view[$BUFFER]._b
139     , start = intIndex + view[$OFFSET]
140     , pack  = store.slice(start, start + bytes);
141   return isLittleEndian ? pack : pack.reverse();
142 };
143 var set = function(view, bytes, index, conversion, value, isLittleEndian){
144   var numIndex = +index
145     , intIndex = toInteger(numIndex);
146   if(numIndex != intIndex || intIndex < 0 || intIndex + bytes > view[$LENGTH])throw RangeError(WRONG_INDEX);
147   var store = view[$BUFFER]._b
148     , start = intIndex + view[$OFFSET]
149     , pack  = conversion(+value);
150   for(var i = 0; i < bytes; i++)store[start + i] = pack[isLittleEndian ? i : bytes - i - 1];
151 };
152
153 var validateArrayBufferArguments = function(that, length){
154   anInstance(that, $ArrayBuffer, ARRAY_BUFFER);
155   var numberLength = +length
156     , byteLength   = toLength(numberLength);
157   if(numberLength != byteLength)throw RangeError(WRONG_LENGTH);
158   return byteLength;
159 };
160
161 if(!$typed.ABV){
162   $ArrayBuffer = function ArrayBuffer(length){
163     var byteLength = validateArrayBufferArguments(this, length);
164     this._b       = arrayFill.call(Array(byteLength), 0);
165     this[$LENGTH] = byteLength;
166   };
167
168   $DataView = function DataView(buffer, byteOffset, byteLength){
169     anInstance(this, $DataView, DATA_VIEW);
170     anInstance(buffer, $ArrayBuffer, DATA_VIEW);
171     var bufferLength = buffer[$LENGTH]
172       , offset       = toInteger(byteOffset);
173     if(offset < 0 || offset > bufferLength)throw RangeError('Wrong offset!');
174     byteLength = byteLength === undefined ? bufferLength - offset : toLength(byteLength);
175     if(offset + byteLength > bufferLength)throw RangeError(WRONG_LENGTH);
176     this[$BUFFER] = buffer;
177     this[$OFFSET] = offset;
178     this[$LENGTH] = byteLength;
179   };
180
181   if(DESCRIPTORS){
182     addGetter($ArrayBuffer, BYTE_LENGTH, '_l');
183     addGetter($DataView, BUFFER, '_b');
184     addGetter($DataView, BYTE_LENGTH, '_l');
185     addGetter($DataView, BYTE_OFFSET, '_o');
186   }
187
188   redefineAll($DataView[PROTOTYPE], {
189     getInt8: function getInt8(byteOffset){
190       return get(this, 1, byteOffset)[0] << 24 >> 24;
191     },
192     getUint8: function getUint8(byteOffset){
193       return get(this, 1, byteOffset)[0];
194     },
195     getInt16: function getInt16(byteOffset /*, littleEndian */){
196       var bytes = get(this, 2, byteOffset, arguments[1]);
197       return (bytes[1] << 8 | bytes[0]) << 16 >> 16;
198     },
199     getUint16: function getUint16(byteOffset /*, littleEndian */){
200       var bytes = get(this, 2, byteOffset, arguments[1]);
201       return bytes[1] << 8 | bytes[0];
202     },
203     getInt32: function getInt32(byteOffset /*, littleEndian */){
204       return unpackI32(get(this, 4, byteOffset, arguments[1]));
205     },
206     getUint32: function getUint32(byteOffset /*, littleEndian */){
207       return unpackI32(get(this, 4, byteOffset, arguments[1])) >>> 0;
208     },
209     getFloat32: function getFloat32(byteOffset /*, littleEndian */){
210       return unpackIEEE754(get(this, 4, byteOffset, arguments[1]), 23, 4);
211     },
212     getFloat64: function getFloat64(byteOffset /*, littleEndian */){
213       return unpackIEEE754(get(this, 8, byteOffset, arguments[1]), 52, 8);
214     },
215     setInt8: function setInt8(byteOffset, value){
216       set(this, 1, byteOffset, packI8, value);
217     },
218     setUint8: function setUint8(byteOffset, value){
219       set(this, 1, byteOffset, packI8, value);
220     },
221     setInt16: function setInt16(byteOffset, value /*, littleEndian */){
222       set(this, 2, byteOffset, packI16, value, arguments[2]);
223     },
224     setUint16: function setUint16(byteOffset, value /*, littleEndian */){
225       set(this, 2, byteOffset, packI16, value, arguments[2]);
226     },
227     setInt32: function setInt32(byteOffset, value /*, littleEndian */){
228       set(this, 4, byteOffset, packI32, value, arguments[2]);
229     },
230     setUint32: function setUint32(byteOffset, value /*, littleEndian */){
231       set(this, 4, byteOffset, packI32, value, arguments[2]);
232     },
233     setFloat32: function setFloat32(byteOffset, value /*, littleEndian */){
234       set(this, 4, byteOffset, packF32, value, arguments[2]);
235     },
236     setFloat64: function setFloat64(byteOffset, value /*, littleEndian */){
237       set(this, 8, byteOffset, packF64, value, arguments[2]);
238     }
239   });
240 } else {
241   if(!fails(function(){
242     new $ArrayBuffer;     // eslint-disable-line no-new
243   }) || !fails(function(){
244     new $ArrayBuffer(.5); // eslint-disable-line no-new
245   })){
246     $ArrayBuffer = function ArrayBuffer(length){
247       return new BaseBuffer(validateArrayBufferArguments(this, length));
248     };
249     var ArrayBufferProto = $ArrayBuffer[PROTOTYPE] = BaseBuffer[PROTOTYPE];
250     for(var keys = gOPN(BaseBuffer), j = 0, key; keys.length > j; ){
251       if(!((key = keys[j++]) in $ArrayBuffer))hide($ArrayBuffer, key, BaseBuffer[key]);
252     };
253     if(!LIBRARY)ArrayBufferProto.constructor = $ArrayBuffer;
254   }
255   // iOS Safari 7.x bug
256   var view = new $DataView(new $ArrayBuffer(2))
257     , $setInt8 = $DataView[PROTOTYPE].setInt8;
258   view.setInt8(0, 2147483648);
259   view.setInt8(1, 2147483649);
260   if(view.getInt8(0) || !view.getInt8(1))redefineAll($DataView[PROTOTYPE], {
261     setInt8: function setInt8(byteOffset, value){
262       $setInt8.call(this, byteOffset, value << 24 >> 24);
263     },
264     setUint8: function setUint8(byteOffset, value){
265       $setInt8.call(this, byteOffset, value << 24 >> 24);
266     }
267   }, true);
268 }
269 setToStringTag($ArrayBuffer, ARRAY_BUFFER);
270 setToStringTag($DataView, DATA_VIEW);
271 hide($DataView[PROTOTYPE], $typed.VIEW, true);
272 exports[ARRAY_BUFFER] = $ArrayBuffer;
273 exports[DATA_VIEW] = $DataView;