UNPKG

10.4 kBJavaScriptView Raw
1'use strict';
2var global = require('../internals/global');
3var uncurryThis = require('../internals/function-uncurry-this');
4var DESCRIPTORS = require('../internals/descriptors');
5var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-basic-detection');
6var FunctionName = require('../internals/function-name');
7var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
8var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
9var defineBuiltIns = require('../internals/define-built-ins');
10var fails = require('../internals/fails');
11var anInstance = require('../internals/an-instance');
12var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
13var toLength = require('../internals/to-length');
14var toIndex = require('../internals/to-index');
15var fround = require('../internals/math-fround');
16var IEEE754 = require('../internals/ieee754');
17var getPrototypeOf = require('../internals/object-get-prototype-of');
18var setPrototypeOf = require('../internals/object-set-prototype-of');
19var arrayFill = require('../internals/array-fill');
20var arraySlice = require('../internals/array-slice');
21var inheritIfRequired = require('../internals/inherit-if-required');
22var copyConstructorProperties = require('../internals/copy-constructor-properties');
23var setToStringTag = require('../internals/set-to-string-tag');
24var InternalStateModule = require('../internals/internal-state');
25
26var PROPER_FUNCTION_NAME = FunctionName.PROPER;
27var CONFIGURABLE_FUNCTION_NAME = FunctionName.CONFIGURABLE;
28var ARRAY_BUFFER = 'ArrayBuffer';
29var DATA_VIEW = 'DataView';
30var PROTOTYPE = 'prototype';
31var WRONG_LENGTH = 'Wrong length';
32var WRONG_INDEX = 'Wrong index';
33var getInternalArrayBufferState = InternalStateModule.getterFor(ARRAY_BUFFER);
34var getInternalDataViewState = InternalStateModule.getterFor(DATA_VIEW);
35var setInternalState = InternalStateModule.set;
36var NativeArrayBuffer = global[ARRAY_BUFFER];
37var $ArrayBuffer = NativeArrayBuffer;
38var ArrayBufferPrototype = $ArrayBuffer && $ArrayBuffer[PROTOTYPE];
39var $DataView = global[DATA_VIEW];
40var DataViewPrototype = $DataView && $DataView[PROTOTYPE];
41var ObjectPrototype = Object.prototype;
42var Array = global.Array;
43var RangeError = global.RangeError;
44var fill = uncurryThis(arrayFill);
45var reverse = uncurryThis([].reverse);
46
47var packIEEE754 = IEEE754.pack;
48var unpackIEEE754 = IEEE754.unpack;
49
50var packInt8 = function (number) {
51 return [number & 0xFF];
52};
53
54var packInt16 = function (number) {
55 return [number & 0xFF, number >> 8 & 0xFF];
56};
57
58var packInt32 = function (number) {
59 return [number & 0xFF, number >> 8 & 0xFF, number >> 16 & 0xFF, number >> 24 & 0xFF];
60};
61
62var unpackInt32 = function (buffer) {
63 return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
64};
65
66var packFloat32 = function (number) {
67 return packIEEE754(fround(number), 23, 4);
68};
69
70var packFloat64 = function (number) {
71 return packIEEE754(number, 52, 8);
72};
73
74var addGetter = function (Constructor, key, getInternalState) {
75 defineBuiltInAccessor(Constructor[PROTOTYPE], key, {
76 configurable: true,
77 get: function () {
78 return getInternalState(this)[key];
79 }
80 });
81};
82
83var get = function (view, count, index, isLittleEndian) {
84 var store = getInternalDataViewState(view);
85 var intIndex = toIndex(index);
86 var boolIsLittleEndian = !!isLittleEndian;
87 if (intIndex + count > store.byteLength) throw new RangeError(WRONG_INDEX);
88 var bytes = store.bytes;
89 var start = intIndex + store.byteOffset;
90 var pack = arraySlice(bytes, start, start + count);
91 return boolIsLittleEndian ? pack : reverse(pack);
92};
93
94var set = function (view, count, index, conversion, value, isLittleEndian) {
95 var store = getInternalDataViewState(view);
96 var intIndex = toIndex(index);
97 var pack = conversion(+value);
98 var boolIsLittleEndian = !!isLittleEndian;
99 if (intIndex + count > store.byteLength) throw new RangeError(WRONG_INDEX);
100 var bytes = store.bytes;
101 var start = intIndex + store.byteOffset;
102 for (var i = 0; i < count; i++) bytes[start + i] = pack[boolIsLittleEndian ? i : count - i - 1];
103};
104
105if (!NATIVE_ARRAY_BUFFER) {
106 $ArrayBuffer = function ArrayBuffer(length) {
107 anInstance(this, ArrayBufferPrototype);
108 var byteLength = toIndex(length);
109 setInternalState(this, {
110 type: ARRAY_BUFFER,
111 bytes: fill(Array(byteLength), 0),
112 byteLength: byteLength
113 });
114 if (!DESCRIPTORS) {
115 this.byteLength = byteLength;
116 this.detached = false;
117 }
118 };
119
120 ArrayBufferPrototype = $ArrayBuffer[PROTOTYPE];
121
122 $DataView = function DataView(buffer, byteOffset, byteLength) {
123 anInstance(this, DataViewPrototype);
124 anInstance(buffer, ArrayBufferPrototype);
125 var bufferState = getInternalArrayBufferState(buffer);
126 var bufferLength = bufferState.byteLength;
127 var offset = toIntegerOrInfinity(byteOffset);
128 if (offset < 0 || offset > bufferLength) throw new RangeError('Wrong offset');
129 byteLength = byteLength === undefined ? bufferLength - offset : toLength(byteLength);
130 if (offset + byteLength > bufferLength) throw new RangeError(WRONG_LENGTH);
131 setInternalState(this, {
132 type: DATA_VIEW,
133 buffer: buffer,
134 byteLength: byteLength,
135 byteOffset: offset,
136 bytes: bufferState.bytes
137 });
138 if (!DESCRIPTORS) {
139 this.buffer = buffer;
140 this.byteLength = byteLength;
141 this.byteOffset = offset;
142 }
143 };
144
145 DataViewPrototype = $DataView[PROTOTYPE];
146
147 if (DESCRIPTORS) {
148 addGetter($ArrayBuffer, 'byteLength', getInternalArrayBufferState);
149 addGetter($DataView, 'buffer', getInternalDataViewState);
150 addGetter($DataView, 'byteLength', getInternalDataViewState);
151 addGetter($DataView, 'byteOffset', getInternalDataViewState);
152 }
153
154 defineBuiltIns(DataViewPrototype, {
155 getInt8: function getInt8(byteOffset) {
156 return get(this, 1, byteOffset)[0] << 24 >> 24;
157 },
158 getUint8: function getUint8(byteOffset) {
159 return get(this, 1, byteOffset)[0];
160 },
161 getInt16: function getInt16(byteOffset /* , littleEndian */) {
162 var bytes = get(this, 2, byteOffset, arguments.length > 1 ? arguments[1] : false);
163 return (bytes[1] << 8 | bytes[0]) << 16 >> 16;
164 },
165 getUint16: function getUint16(byteOffset /* , littleEndian */) {
166 var bytes = get(this, 2, byteOffset, arguments.length > 1 ? arguments[1] : false);
167 return bytes[1] << 8 | bytes[0];
168 },
169 getInt32: function getInt32(byteOffset /* , littleEndian */) {
170 return unpackInt32(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : false));
171 },
172 getUint32: function getUint32(byteOffset /* , littleEndian */) {
173 return unpackInt32(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : false)) >>> 0;
174 },
175 getFloat32: function getFloat32(byteOffset /* , littleEndian */) {
176 return unpackIEEE754(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : false), 23);
177 },
178 getFloat64: function getFloat64(byteOffset /* , littleEndian */) {
179 return unpackIEEE754(get(this, 8, byteOffset, arguments.length > 1 ? arguments[1] : false), 52);
180 },
181 setInt8: function setInt8(byteOffset, value) {
182 set(this, 1, byteOffset, packInt8, value);
183 },
184 setUint8: function setUint8(byteOffset, value) {
185 set(this, 1, byteOffset, packInt8, value);
186 },
187 setInt16: function setInt16(byteOffset, value /* , littleEndian */) {
188 set(this, 2, byteOffset, packInt16, value, arguments.length > 2 ? arguments[2] : false);
189 },
190 setUint16: function setUint16(byteOffset, value /* , littleEndian */) {
191 set(this, 2, byteOffset, packInt16, value, arguments.length > 2 ? arguments[2] : false);
192 },
193 setInt32: function setInt32(byteOffset, value /* , littleEndian */) {
194 set(this, 4, byteOffset, packInt32, value, arguments.length > 2 ? arguments[2] : false);
195 },
196 setUint32: function setUint32(byteOffset, value /* , littleEndian */) {
197 set(this, 4, byteOffset, packInt32, value, arguments.length > 2 ? arguments[2] : false);
198 },
199 setFloat32: function setFloat32(byteOffset, value /* , littleEndian */) {
200 set(this, 4, byteOffset, packFloat32, value, arguments.length > 2 ? arguments[2] : false);
201 },
202 setFloat64: function setFloat64(byteOffset, value /* , littleEndian */) {
203 set(this, 8, byteOffset, packFloat64, value, arguments.length > 2 ? arguments[2] : false);
204 }
205 });
206} else {
207 var INCORRECT_ARRAY_BUFFER_NAME = PROPER_FUNCTION_NAME && NativeArrayBuffer.name !== ARRAY_BUFFER;
208 /* eslint-disable no-new -- required for testing */
209 if (!fails(function () {
210 NativeArrayBuffer(1);
211 }) || !fails(function () {
212 new NativeArrayBuffer(-1);
213 }) || fails(function () {
214 new NativeArrayBuffer();
215 new NativeArrayBuffer(1.5);
216 new NativeArrayBuffer(NaN);
217 return NativeArrayBuffer.length !== 1 || INCORRECT_ARRAY_BUFFER_NAME && !CONFIGURABLE_FUNCTION_NAME;
218 })) {
219 /* eslint-enable no-new -- required for testing */
220 $ArrayBuffer = function ArrayBuffer(length) {
221 anInstance(this, ArrayBufferPrototype);
222 return inheritIfRequired(new NativeArrayBuffer(toIndex(length)), this, $ArrayBuffer);
223 };
224
225 $ArrayBuffer[PROTOTYPE] = ArrayBufferPrototype;
226
227 ArrayBufferPrototype.constructor = $ArrayBuffer;
228
229 copyConstructorProperties($ArrayBuffer, NativeArrayBuffer);
230 } else if (INCORRECT_ARRAY_BUFFER_NAME && CONFIGURABLE_FUNCTION_NAME) {
231 createNonEnumerableProperty(NativeArrayBuffer, 'name', ARRAY_BUFFER);
232 }
233
234 // WebKit bug - the same parent prototype for typed arrays and data view
235 if (setPrototypeOf && getPrototypeOf(DataViewPrototype) !== ObjectPrototype) {
236 setPrototypeOf(DataViewPrototype, ObjectPrototype);
237 }
238
239 // iOS Safari 7.x bug
240 var testView = new $DataView(new $ArrayBuffer(2));
241 var $setInt8 = uncurryThis(DataViewPrototype.setInt8);
242 testView.setInt8(0, 2147483648);
243 testView.setInt8(1, 2147483649);
244 if (testView.getInt8(0) || !testView.getInt8(1)) defineBuiltIns(DataViewPrototype, {
245 setInt8: function setInt8(byteOffset, value) {
246 $setInt8(this, byteOffset, value << 24 >> 24);
247 },
248 setUint8: function setUint8(byteOffset, value) {
249 $setInt8(this, byteOffset, value << 24 >> 24);
250 }
251 }, { unsafe: true });
252}
253
254setToStringTag($ArrayBuffer, ARRAY_BUFFER);
255setToStringTag($DataView, DATA_VIEW);
256
257module.exports = {
258 ArrayBuffer: $ArrayBuffer,
259 DataView: $DataView
260};