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