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]
24 , RangeError = global.RangeError
25 , Infinity = global.Infinity
26 , BaseBuffer = $ArrayBuffer
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;
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
45 , rt = mLen === 23 ? pow(2, -24) - pow(2, -77) : 0
47 , s = value < 0 || value === 0 && 1 / value < 0 ? 1 : 0
50 if(value != value || value === Infinity){
51 m = value != value ? 1 : 0;
54 e = floor(log(value) / LN2);
55 if(value * (c = pow(2, -e)) < 1){
62 value += rt * pow(2, 1 - eBias);
68 if(e + eBias >= eMax){
71 } else if(e + eBias >= 1){
72 m = (value * c - 1) * pow(2, mLen);
75 m = value * pow(2, eBias - 1) * pow(2, mLen);
79 for(; mLen >= 8; buffer[i++] = m & 255, m /= 256, mLen -= 8);
82 for(; eLen > 0; buffer[i++] = e & 255, e /= 256, eLen -= 8);
83 buffer[--i] |= s * 128;
86 var unpackIEEE754 = function(buffer, mLen, nBytes){
87 var eLen = nBytes * 8 - mLen - 1
88 , eMax = (1 << eLen) - 1
96 for(; nBits > 0; e = e * 256 + buffer[i], i--, nBits -= 8);
97 m = e & (1 << -nBits) - 1;
100 for(; nBits > 0; m = m * 256 + buffer[i], i--, nBits -= 8);
103 } else if(e === eMax){
104 return m ? NaN : s ? -Infinity : Infinity;
106 m = m + pow(2, mLen);
108 } return (s ? -1 : 1) * m * pow(2, e - mLen);
111 var unpackI32 = function(bytes){
112 return bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
114 var packI8 = function(it){
117 var packI16 = function(it){
118 return [it & 0xff, it >> 8 & 0xff];
120 var packI32 = function(it){
121 return [it & 0xff, it >> 8 & 0xff, it >> 16 & 0xff, it >> 24 & 0xff];
123 var packF64 = function(it){
124 return packIEEE754(it, 52, 8);
126 var packF32 = function(it){
127 return packIEEE754(it, 23, 4);
130 var addGetter = function(C, key, internal){
131 dP(C[PROTOTYPE], key, {get: function(){ return this[internal]; }});
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();
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];
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);
162 $ArrayBuffer = function ArrayBuffer(length){
163 var byteLength = validateArrayBufferArguments(this, length);
164 this._b = arrayFill.call(Array(byteLength), 0);
165 this[$LENGTH] = byteLength;
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;
182 addGetter($ArrayBuffer, BYTE_LENGTH, '_l');
183 addGetter($DataView, BUFFER, '_b');
184 addGetter($DataView, BYTE_LENGTH, '_l');
185 addGetter($DataView, BYTE_OFFSET, '_o');
188 redefineAll($DataView[PROTOTYPE], {
189 getInt8: function getInt8(byteOffset){
190 return get(this, 1, byteOffset)[0] << 24 >> 24;
192 getUint8: function getUint8(byteOffset){
193 return get(this, 1, byteOffset)[0];
195 getInt16: function getInt16(byteOffset /*, littleEndian */){
196 var bytes = get(this, 2, byteOffset, arguments[1]);
197 return (bytes[1] << 8 | bytes[0]) << 16 >> 16;
199 getUint16: function getUint16(byteOffset /*, littleEndian */){
200 var bytes = get(this, 2, byteOffset, arguments[1]);
201 return bytes[1] << 8 | bytes[0];
203 getInt32: function getInt32(byteOffset /*, littleEndian */){
204 return unpackI32(get(this, 4, byteOffset, arguments[1]));
206 getUint32: function getUint32(byteOffset /*, littleEndian */){
207 return unpackI32(get(this, 4, byteOffset, arguments[1])) >>> 0;
209 getFloat32: function getFloat32(byteOffset /*, littleEndian */){
210 return unpackIEEE754(get(this, 4, byteOffset, arguments[1]), 23, 4);
212 getFloat64: function getFloat64(byteOffset /*, littleEndian */){
213 return unpackIEEE754(get(this, 8, byteOffset, arguments[1]), 52, 8);
215 setInt8: function setInt8(byteOffset, value){
216 set(this, 1, byteOffset, packI8, value);
218 setUint8: function setUint8(byteOffset, value){
219 set(this, 1, byteOffset, packI8, value);
221 setInt16: function setInt16(byteOffset, value /*, littleEndian */){
222 set(this, 2, byteOffset, packI16, value, arguments[2]);
224 setUint16: function setUint16(byteOffset, value /*, littleEndian */){
225 set(this, 2, byteOffset, packI16, value, arguments[2]);
227 setInt32: function setInt32(byteOffset, value /*, littleEndian */){
228 set(this, 4, byteOffset, packI32, value, arguments[2]);
230 setUint32: function setUint32(byteOffset, value /*, littleEndian */){
231 set(this, 4, byteOffset, packI32, value, arguments[2]);
233 setFloat32: function setFloat32(byteOffset, value /*, littleEndian */){
234 set(this, 4, byteOffset, packF32, value, arguments[2]);
236 setFloat64: function setFloat64(byteOffset, value /*, littleEndian */){
237 set(this, 8, byteOffset, packF64, value, arguments[2]);
241 if(!fails(function(){
242 new $ArrayBuffer; // eslint-disable-line no-new
243 }) || !fails(function(){
244 new $ArrayBuffer(.5); // eslint-disable-line no-new
246 $ArrayBuffer = function ArrayBuffer(length){
247 return new BaseBuffer(validateArrayBufferArguments(this, length));
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]);
253 if(!LIBRARY)ArrayBufferProto.constructor = $ArrayBuffer;
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);
264 setUint8: function setUint8(byteOffset, value){
265 $setInt8.call(this, byteOffset, value << 24 >> 24);
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;