Skip to content

Commit 22e08f7

Browse files
committed
refactoring of typed arrays, extract some methods to separate modules, fix prototype chain
1 parent 343c128 commit 22e08f7

33 files changed

+156
-66
lines changed

packages/core-js-builder/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,17 @@ module.exports = {
155155
'es.typed-array.find',
156156
'es.typed-array.find-index',
157157
'es.typed-array.for-each',
158+
'es.typed-array.from',
158159
'es.typed-array.includes',
159160
'es.typed-array.index-of',
160161
'es.typed-array.iterator',
161162
'es.typed-array.join',
162163
'es.typed-array.last-index-of',
164+
'es.typed-array.of',
163165
'es.typed-array.reduce',
164166
'es.typed-array.reduce-right',
165167
'es.typed-array.reverse',
168+
'es.typed-array.set',
166169
'es.typed-array.some',
167170
'es.typed-array.sort',
168171
'es.typed-array.to-locale-string',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

packages/core-js/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,17 @@ require('./modules/es.typed-array.fill');
139139
require('./modules/es.typed-array.find');
140140
require('./modules/es.typed-array.find-index');
141141
require('./modules/es.typed-array.for-each');
142+
require('./modules/es.typed-array.from');
142143
require('./modules/es.typed-array.includes');
143144
require('./modules/es.typed-array.index-of');
144145
require('./modules/es.typed-array.iterator');
145146
require('./modules/es.typed-array.join');
146147
require('./modules/es.typed-array.last-index-of');
148+
require('./modules/es.typed-array.of');
147149
require('./modules/es.typed-array.reduce');
148150
require('./modules/es.typed-array.reduce-right');
149151
require('./modules/es.typed-array.reverse');
152+
require('./modules/es.typed-array.set');
150153
require('./modules/es.typed-array.some');
151154
require('./modules/es.typed-array.sort');
152155
require('./modules/es.typed-array.to-locale-string');

packages/core-js/internals/array-buffer-view-core.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var redefine = require('../internals/redefine');
77
var getPrototypeOf = require('../internals/object-get-prototype-of');
88
var setPrototypeOf = require('../internals/object-set-prototype-of');
99
var ObjectPrototype = Object.prototype;
10+
var isPrototypeOf = ObjectPrototype.isPrototypeOf;
1011

1112
var DataView = global.DataView;
1213
var Uint8Array = global.Uint8Array;
@@ -43,6 +44,15 @@ var aTypedArray = function (it) {
4344
throw TypeError('Target is not a typed array!');
4445
};
4546

47+
var aTypedArrayConstructor = function (C) {
48+
var ARRAY;
49+
if (CORRECT_PROTOTYPE_CHAIN) {
50+
if (isPrototypeOf.call(TypedArrayConstructor, C)) return C;
51+
} else for (ARRAY in TypedArrayConstructorsList) {
52+
if (isObject(global[ARRAY]) && C === global[ARRAY]) return C;
53+
} throw TypeError('It is not a typed array constructor!');
54+
};
55+
4656
var exportProto = function (KEY, property, forced) {
4757
if (!DESCRIPTORS) return;
4858
var ARRAY;
@@ -54,7 +64,9 @@ var exportProto = function (KEY, property, forced) {
5464
redefine(TypedArrayPrototype, KEY, forced ? property : Uint8ArrayPrototype[KEY] || property);
5565
}
5666
} else for (ARRAY in TypedArrayConstructorsList) {
57-
if (global[ARRAY]) redefine(global[ARRAY].prototype, KEY, forced ? property : Uint8ArrayPrototype[KEY] || property);
67+
if (global[ARRAY] && (!global[ARRAY].prototype[KEY] || forced)) {
68+
redefine(global[ARRAY].prototype, KEY, property);
69+
}
5870
}
5971
};
6072

@@ -69,7 +81,9 @@ var exportStatic = function (KEY, property, forced) {
6981
redefine(TypedArrayConstructor, KEY, forced ? property : Uint8Array[KEY] || property);
7082
}
7183
} else for (ARRAY in TypedArrayConstructorsList) {
72-
if (global[ARRAY]) redefine(global[ARRAY], KEY, forced ? property : Uint8Array[KEY] || property);
84+
if (global[ARRAY] && (!global[ARRAY][KEY] || forced)) {
85+
redefine(global[ARRAY], KEY, property);
86+
}
7387
}
7488
};
7589

@@ -81,7 +95,8 @@ for (NAME in TypedArrayConstructorsList) {
8195

8296
CORRECT_PROTOTYPE_CHAIN = !NATIVE_ARRAY_BUFFER_VIEWS || !!setPrototypeOf;
8397

84-
if (!TypedArrayConstructor || TypedArrayConstructor === Function.prototype) {
98+
// WebKit bug - typed arrays constructors prototype is Object.prototype
99+
if (typeof TypedArrayConstructor != 'function' || TypedArrayConstructor === Function.prototype) {
85100
TypedArrayConstructor = function TypedArray() {
86101
throw TypeError('Incorrect invocation!');
87102
};
@@ -92,7 +107,7 @@ if (!TypedArrayConstructor || TypedArrayConstructor === Function.prototype) {
92107
}
93108

94109
if (!TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
95-
TypedArrayPrototype = {};
110+
TypedArrayPrototype = TypedArrayConstructor.prototype;
96111

97112
if (Uint8ArrayPrototype && setPrototypeOf) for (NAME in TypedArrayConstructorsList) {
98113
if (global[NAME]) setPrototypeOf(global[NAME].prototype, TypedArrayPrototype);
@@ -109,6 +124,7 @@ module.exports = {
109124
NATIVE_ARRAY_BUFFER: NATIVE_ARRAY_BUFFER,
110125
NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
111126
aTypedArray: aTypedArray,
127+
aTypedArrayConstructor: aTypedArrayConstructor,
112128
exportProto: exportProto,
113129
exportStatic: exportStatic,
114130
isArrayBufferView: isArrayBufferView,

packages/core-js/internals/redefine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require('../internals/shared')('inspectSource', function (it) {
1515
(module.exports = function (O, key, value, unsafe) {
1616
if (typeof value == 'function') {
1717
if (!has(value, 'name')) hide(value, 'name', key);
18-
enforceInternalState(value).source = has(O, key) ? String(O[key]) : TEMPLATE.join(String(key));
18+
enforceInternalState(value).source = TEMPLATE.join(String(key));
1919
}
2020
if (O === global) {
2121
setGlobal(key, value);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var toInteger = require('../internals/to-integer');
2+
3+
module.exports = function (it, BYTES) {
4+
var offset = toInteger(it);
5+
if (offset < 0 || offset % BYTES) throw RangeError('Wrong offset!');
6+
return offset;
7+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var toObject = require('../internals/to-object');
2+
var toLength = require('../internals/to-length');
3+
var getIteratorMethod = require('../internals/get-iterator-method');
4+
var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
5+
var bind = require('../internals/bind-context');
6+
var aTypedArrayConstructor = require('../internals/array-buffer-view-core').aTypedArrayConstructor;
7+
8+
module.exports = function from(source /* , mapfn, thisArg */) {
9+
var O = toObject(source);
10+
var aLen = arguments.length;
11+
var mapfn = aLen > 1 ? arguments[1] : undefined;
12+
var mapping = mapfn !== undefined;
13+
var iterFn = getIteratorMethod(O);
14+
var i, length, values, result, step, iterator;
15+
if (iterFn != undefined && !isArrayIteratorMethod(iterFn)) {
16+
for (iterator = iterFn.call(O), values = [], i = 0; !(step = iterator.next()).done; i++) {
17+
values.push(step.value);
18+
} O = values;
19+
}
20+
if (mapping && aLen > 2) mapfn = bind(mapfn, arguments[2], 2);
21+
for (i = 0, length = toLength(O.length), result = new (aTypedArrayConstructor(this))(length); length > i; i++) {
22+
result[i] = mapping ? mapfn(O[i], i) : O[i];
23+
}
24+
return result;
25+
};

packages/core-js/internals/typed-array.js

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,25 @@ if (require('../internals/descriptors')) {
55
var $export = require('../internals/export');
66
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
77
var TypedBufferModule = require('../internals/typed-buffer');
8-
var bind = require('../internals/bind-context');
98
var anInstance = require('../internals/an-instance');
109
var createPropertyDescriptor = require('../internals/create-property-descriptor');
1110
var hide = require('../internals/hide');
1211
var redefineAll = require('../internals/redefine-all');
13-
var toInteger = require('../internals/to-integer');
1412
var toLength = require('../internals/to-length');
1513
var toIndex = require('../internals/to-index');
14+
var toOffset = require('../internals/to-offset');
1615
var toAbsoluteIndex = require('../internals/to-absolute-index');
1716
var toPrimitive = require('../internals/to-primitive');
1817
var has = require('../internals/has');
1918
var classof = require('../internals/classof');
2019
var isObject = require('../internals/is-object');
21-
var toObject = require('../internals/to-object');
22-
var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
2320
var create = require('../internals/object-create');
21+
var setPrototypeOf = require('../internals/object-set-prototype-of');
2422
var getPrototypeOf = require('../internals/object-get-prototype-of');
2523
var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
26-
var getIteratorMethod = require('../internals/get-iterator-method');
2724
var TAG = require('../internals/well-known-symbol')('toStringTag');
2825
var uid = require('../internals/uid');
26+
var typedArrayFrom = require('../internals/typed-array-from');
2927
var createArrayMethod = require('../internals/array-methods');
3028
var speciesConstructor = require('../internals/species-constructor');
3129
var checkCorrectnessOfIteration = require('../internals/check-correctness-of-iteration');
@@ -53,6 +51,7 @@ if (require('../internals/descriptors')) {
5351
var DEF_CONSTRUCTOR = uid('def_constructor');
5452
var TYPED_ARRAY = uid('typed_array');
5553
var NATIVE_ARRAY_BUFFER_VIEWS = ArrayBufferViewCore.NATIVE_ARRAY_BUFFER_VIEWS;
54+
var TypedArrayConstructor = ArrayBufferViewCore.TypedArray;
5655
var aTypedArray = ArrayBufferViewCore.aTypedArray;
5756
var isTypedArray = ArrayBufferViewCore.isTypedArray;
5857
var WRONG_LENGTH = 'Wrong length!';
@@ -66,16 +65,6 @@ if (require('../internals/descriptors')) {
6665
return new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
6766
});
6867

69-
var FORCED_SET = !!Uint8Array && !!Uint8Array[PROTOTYPE].set && fails(function () {
70-
new Uint8Array(1).set({});
71-
});
72-
73-
var toOffset = function (it, BYTES) {
74-
var offset = toInteger(it);
75-
if (offset < 0 || offset % BYTES) throw RangeError('Wrong offset!');
76-
return offset;
77-
};
78-
7968
var allocateTypedArray = function (C, length) {
8069
if (!(isObject(C) && TYPED_CONSTRUCTOR in C)) {
8170
throw TypeError('It is not a typed array constructor!');
@@ -98,33 +87,6 @@ if (require('../internals/descriptors')) {
9887
nativeDefineProperty(it, key, { get: function () { return getInternalState(this)[key]; } });
9988
};
10089

101-
var typedArrayFrom = function from(source /* , mapfn, thisArg */) {
102-
var O = toObject(source);
103-
var aLen = arguments.length;
104-
var mapfn = aLen > 1 ? arguments[1] : undefined;
105-
var mapping = mapfn !== undefined;
106-
var iterFn = getIteratorMethod(O);
107-
var i, length, values, result, step, iterator;
108-
if (iterFn != undefined && !isArrayIteratorMethod(iterFn)) {
109-
for (iterator = iterFn.call(O), values = [], i = 0; !(step = iterator.next()).done; i++) {
110-
values.push(step.value);
111-
} O = values;
112-
}
113-
if (mapping && aLen > 2) mapfn = bind(mapfn, arguments[2], 2);
114-
for (i = 0, length = toLength(O.length), result = allocateTypedArray(this, length); length > i; i++) {
115-
result[i] = mapping ? mapfn(O[i], i) : O[i];
116-
}
117-
return result;
118-
};
119-
120-
var typedArrayOf = function of(/* ...items */) {
121-
var index = 0;
122-
var length = arguments.length;
123-
var result = allocateTypedArray(this, length);
124-
while (length > index) result[index] = arguments[index++];
125-
return result;
126-
};
127-
12890
var TypedArrayPrototypeMethods = {
12991
filter: function filter(callbackfn /* , thisArg */) {
13092
return speciesFromList(this, arrayFilter(aTypedArray(this), callbackfn,
@@ -149,17 +111,6 @@ if (require('../internals/descriptors')) {
149111
return speciesFromList(this, arraySlice.call(aTypedArray(this), start, end));
150112
};
151113

152-
var typedArraySet = function set(arrayLike /* , offset */) {
153-
aTypedArray(this);
154-
var offset = toOffset(arguments[1], 1);
155-
var length = this.length;
156-
var src = toObject(arrayLike);
157-
var len = toLength(src.length);
158-
var index = 0;
159-
if (len + offset > length) throw RangeError(WRONG_LENGTH);
160-
while (index < len) this[offset + index] = src[index++];
161-
};
162-
163114
var isTypedArrayIndex = function (target, key) {
164115
return isTypedArray(target)
165116
&& typeof key != 'symbol'
@@ -200,7 +151,6 @@ if (require('../internals/descriptors')) {
200151
var $TypedArrayPrototype$ = redefineAll({}, TypedArrayPrototypeMethods);
201152
redefineAll($TypedArrayPrototype$, {
202153
slice: typedArraySlice,
203-
set: typedArraySet,
204154
constructor: function () { /* noop */ }
205155
});
206156
addGetter($TypedArrayPrototype$, 'buffer');
@@ -280,6 +230,7 @@ if (require('../internals/descriptors')) {
280230
});
281231
while (index < length) addElement(that, index++);
282232
});
233+
if (setPrototypeOf) setPrototypeOf(TypedArray, TypedArrayConstructor);
283234
TypedArrayPrototype = TypedArray[PROTOTYPE] = create($TypedArrayPrototype$);
284235
hide(TypedArrayPrototype, 'constructor', TypedArray);
285236
} else if (!fails(function () {
@@ -306,6 +257,7 @@ if (require('../internals/descriptors')) {
306257
if (isTypedArray(data)) return fromList(TypedArray, data);
307258
return typedArrayFrom.call(TypedArray, data);
308259
});
260+
if (setPrototypeOf) setPrototypeOf(TypedArray, TypedArrayConstructor);
309261
arrayForEach(TAC !== Function.prototype
310262
? getOwnPropertyNames(Base).concat(getOwnPropertyNames(TAC))
311263
: getOwnPropertyNames(Base)
@@ -333,19 +285,12 @@ if (require('../internals/descriptors')) {
333285
BYTES_PER_ELEMENT: BYTES
334286
});
335287

336-
$export({ target: NAME, stat: true, forced: fails(function () { Base.of.call(TypedArray, 1); }) }, {
337-
from: typedArrayFrom,
338-
of: typedArrayOf
339-
});
340-
341288
if (!(BYTES_PER_ELEMENT in TypedArrayPrototype)) hide(TypedArrayPrototype, BYTES_PER_ELEMENT, BYTES);
342289

343290
$export({ target: NAME, proto: true }, TypedArrayPrototypeMethods);
344291

345292
setSpecies(NAME);
346293

347-
$export({ target: NAME, proto: true, forced: FORCED_SET }, { set: typedArraySet });
348-
349294
$export({ target: NAME, proto: true, forced: fails(function () {
350295
new TypedArray(1).slice();
351296
}) }, { slice: typedArraySlice });

packages/core-js/modules/es.typed-array.copy-within.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayCopyWithin = require('../internals/array-copy-within');
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.copyWithin` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.copywithin
68
ArrayBufferViewCore.exportProto('copyWithin', function copyWithin(target, start /* , end */) {
79
return arrayCopyWithin.call(aTypedArray(this), target, start, arguments.length > 2 ? arguments[2] : undefined);
810
});

packages/core-js/modules/es.typed-array.every.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayEvery = require('../internals/array-methods')(4);
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.every` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.every
68
ArrayBufferViewCore.exportProto('every', function every(callbackfn /* , thisArg */) {
79
return arrayEvery(aTypedArray(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
810
});

packages/core-js/modules/es.typed-array.fill.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayFill = require('../internals/array-fill');
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.fill` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.fill
68
ArrayBufferViewCore.exportProto('fill', function fill(value /* , start, end */) { // eslint-disable-line no-unused-vars
79
return arrayFill.apply(aTypedArray(this), arguments);
810
});

packages/core-js/modules/es.typed-array.find-index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayFindIndex = require('../internals/array-methods')(6);
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.findIndex` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.findindex
68
ArrayBufferViewCore.exportProto('findIndex', function findIndex(predicate /* , thisArg */) {
79
return arrayFindIndex(aTypedArray(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
810
});

packages/core-js/modules/es.typed-array.find.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayFind = require('../internals/array-methods')(5);
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.find` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.find
68
ArrayBufferViewCore.exportProto('find', function find(predicate /* , thisArg */) {
79
return arrayFind(aTypedArray(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
810
});

packages/core-js/modules/es.typed-array.for-each.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayForEach = require('../internals/array-methods')(0);
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.forEach` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.foreach
68
ArrayBufferViewCore.exportProto('forEach', function forEach(callbackfn /* , thisArg */) {
79
arrayForEach(aTypedArray(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
810
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
3+
var typedArrayFrom = require('../internals/typed-array-from');
4+
5+
// `%TypedArray%.from` method
6+
// https://tc39.github.io/ecma262/#sec-%typedarray%.from
7+
ArrayBufferViewCore.exportStatic('from', typedArrayFrom);

packages/core-js/modules/es.typed-array.includes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayIncludes = require('../internals/array-includes')(true);
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.includes` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.includes
68
ArrayBufferViewCore.exportProto('includes', function includes(searchElement /* , fromIndex */) {
79
return arrayIncludes(aTypedArray(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
810
});

packages/core-js/modules/es.typed-array.index-of.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var arrayIndexOf = require('../internals/array-includes')(false);
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
44
var aTypedArray = ArrayBufferViewCore.aTypedArray;
55

6+
// `%TypedArray%.prototype.indexOf` method
7+
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.indexof
68
ArrayBufferViewCore.exportProto('indexOf', function indexOf(searchElement /* , fromIndex */) {
79
return arrayIndexOf(aTypedArray(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
810
});

0 commit comments

Comments
 (0)