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