Skip to content

Commit

Permalink
avoid creation of extra properties for the handling of %TypedArray%
Browse files Browse the repository at this point in the history
… constructors in new methods

#1092 (comment)
  • Loading branch information
zloirock committed Jun 17, 2022
1 parent 3142b4d commit ca95307
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Changelog
##### Unreleased
- Nothing
- Avoided creation of extra properties for the handling of `%TypedArray%` constructors in new methods, [#1092 (comment)](https://github.com/zloirock/core-js/issues/1092#issuecomment-1158760512)

##### [3.23.1 - 2022.06.14](https://github.com/zloirock/core-js/releases/tag/v3.23.1)
- Fixed possible error on multiple `core-js` copies, [#1091](https://github.com/zloirock/core-js/issues/1091)
Expand Down
19 changes: 15 additions & 4 deletions packages/core-js/internals/array-buffer-view-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ var getPrototypeOf = require('../internals/object-get-prototype-of');
var setPrototypeOf = require('../internals/object-set-prototype-of');
var wellKnownSymbol = require('../internals/well-known-symbol');
var uid = require('../internals/uid');
var InternalStateModule = require('../internals/internal-state');

var enforceInternalState = InternalStateModule.enforce;
var getInternalState = InternalStateModule.get;
var Int8Array = global.Int8Array;
var Int8ArrayPrototype = Int8Array && Int8Array.prototype;
var Uint8ClampedArray = global.Uint8ClampedArray;
Expand All @@ -27,7 +30,7 @@ var TypeError = global.TypeError;

var TO_STRING_TAG = wellKnownSymbol('toStringTag');
var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
var TYPED_ARRAY_CONSTRUCTOR = uid('TYPED_ARRAY_CONSTRUCTOR');
var TYPED_ARRAY_CONSTRUCTOR = 'TypedArrayConstructor';
// Fixing native typed arrays in Opera Presto crashes the browser, see #595
var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(global.opera) !== 'Opera';
var TYPED_ARRAY_TAG_REQUIRED = false;
Expand Down Expand Up @@ -58,6 +61,14 @@ var isView = function isView(it) {
|| hasOwn(BigIntArrayConstructorsList, klass);
};

var getTypedArrayConstructor = function (it) {
var proto = getPrototypeOf(it);
if (!isObject(proto)) return;
var state = getInternalState(proto);
if (state && hasOwn(state, TYPED_ARRAY_CONSTRUCTOR)) return state[TYPED_ARRAY_CONSTRUCTOR];
return getTypedArrayConstructor(proto);
};

var isTypedArray = function (it) {
if (!isObject(it)) return false;
var klass = classof(it);
Expand Down Expand Up @@ -122,14 +133,14 @@ var exportTypedArrayStaticMethod = function (KEY, property, forced) {
for (NAME in TypedArrayConstructorsList) {
Constructor = global[NAME];
Prototype = Constructor && Constructor.prototype;
if (Prototype) createNonEnumerableProperty(Prototype, TYPED_ARRAY_CONSTRUCTOR, Constructor);
if (Prototype) enforceInternalState(Prototype)[TYPED_ARRAY_CONSTRUCTOR] = Constructor;
else NATIVE_ARRAY_BUFFER_VIEWS = false;
}

for (NAME in BigIntArrayConstructorsList) {
Constructor = global[NAME];
Prototype = Constructor && Constructor.prototype;
if (Prototype) createNonEnumerableProperty(Prototype, TYPED_ARRAY_CONSTRUCTOR, Constructor);
if (Prototype) enforceInternalState(Prototype)[TYPED_ARRAY_CONSTRUCTOR] = Constructor;
}

// WebKit bug - typed arrays constructors prototype is Object.prototype
Expand Down Expand Up @@ -167,12 +178,12 @@ if (DESCRIPTORS && !hasOwn(TypedArrayPrototype, TO_STRING_TAG)) {

module.exports = {
NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
TYPED_ARRAY_CONSTRUCTOR: TYPED_ARRAY_CONSTRUCTOR,
TYPED_ARRAY_TAG: TYPED_ARRAY_TAG_REQUIRED && TYPED_ARRAY_TAG,
aTypedArray: aTypedArray,
aTypedArrayConstructor: aTypedArrayConstructor,
exportTypedArrayMethod: exportTypedArrayMethod,
exportTypedArrayStaticMethod: exportTypedArrayStaticMethod,
getTypedArrayConstructor: getTypedArrayConstructor,
isView: isView,
isTypedArray: isTypedArray,
TypedArray: TypedArray,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/internals/typed-array-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var inheritIfRequired = require('../internals/inherit-if-required');

var getInternalState = InternalStateModule.get;
var setInternalState = InternalStateModule.set;
var enforceInternalState = InternalStateModule.enforce;
var nativeDefineProperty = definePropertyModule.f;
var nativeGetOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f;
var round = Math.round;
Expand All @@ -40,7 +41,6 @@ var ArrayBuffer = ArrayBufferModule.ArrayBuffer;
var ArrayBufferPrototype = ArrayBuffer.prototype;
var DataView = ArrayBufferModule.DataView;
var NATIVE_ARRAY_BUFFER_VIEWS = ArrayBufferViewCore.NATIVE_ARRAY_BUFFER_VIEWS;
var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;
var TYPED_ARRAY_TAG = ArrayBufferViewCore.TYPED_ARRAY_TAG;
var TypedArray = ArrayBufferViewCore.TypedArray;
var TypedArrayPrototype = ArrayBufferViewCore.TypedArrayPrototype;
Expand Down Expand Up @@ -217,7 +217,7 @@ if (DESCRIPTORS) {
createNonEnumerableProperty(TypedArrayConstructorPrototype, 'constructor', TypedArrayConstructor);
}

createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_CONSTRUCTOR, TypedArrayConstructor);
enforceInternalState(TypedArrayConstructorPrototype).TypedArrayConstructor = TypedArrayConstructor;

if (TYPED_ARRAY_TAG) {
createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_TAG, CONSTRUCTOR_NAME);
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/internals/typed-array-species-constructor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
var speciesConstructor = require('../internals/species-constructor');

var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;
var aTypedArrayConstructor = ArrayBufferViewCore.aTypedArrayConstructor;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;

// a part of `TypedArraySpeciesCreate` abstract operation
// https://tc39.es/ecma262/#typedarray-species-create
module.exports = function (originalArray) {
return aTypedArrayConstructor(speciesConstructor(originalArray, originalArray[TYPED_ARRAY_CONSTRUCTOR]));
return aTypedArrayConstructor(speciesConstructor(originalArray, getTypedArrayConstructor(originalArray)));
};
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.typed-array.to-reversed.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var ArrayBufferViewCore = require('../internals/array-buffer-view-core');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;

// `%TypedArray%.prototype.toReversed` method
// https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toReversed
exportTypedArrayMethod('toReversed', function toReversed() {
return arrayToReversed(aTypedArray(this), this[TYPED_ARRAY_CONSTRUCTOR]);
return arrayToReversed(aTypedArray(this), getTypedArrayConstructor(this));
});
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.typed-array.to-sorted.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ var aCallable = require('../internals/a-callable');
var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;
var sort = uncurryThis(ArrayBufferViewCore.TypedArrayPrototype.sort);

// `%TypedArray%.prototype.toSorted` method
// https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSorted
exportTypedArrayMethod('toSorted', function toSorted(compareFn) {
if (compareFn !== undefined) aCallable(compareFn);
var O = aTypedArray(this);
var A = arrayFromConstructorAndList(O[TYPED_ARRAY_CONSTRUCTOR], O);
var A = arrayFromConstructorAndList(getTypedArrayConstructor(O), O);
return sort(A, compareFn);
});
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.typed-array.to-spliced.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ var arraySlice = require('../internals/array-slice');
var arrayToSpliced = require('../internals/array-to-spliced');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;

// `%TypedArray%.prototype.toSpliced` method
// https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSpliced
// eslint-disable-next-line no-unused-vars -- required for .length
exportTypedArrayMethod('toSpliced', function toSpliced(start, deleteCount /* , ...items */) {
return arrayToSpliced(aTypedArray(this), this[TYPED_ARRAY_CONSTRUCTOR], arraySlice(arguments));
return arrayToSpliced(aTypedArray(this), getTypedArrayConstructor(this), arraySlice(arguments));
}, { arity: 2 });
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.typed-array.with.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var classof = require('../internals/classof');
var uncurryThis = require('../internals/function-uncurry-this');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;
var slice = uncurryThis(''.slice);

var PROPER_ORDER = !!function () {
Expand All @@ -28,5 +28,5 @@ exportTypedArrayMethod('with', { 'with': function (index, value) {
aTypedArray(this);
var relativeIndex = toIntegerOrInfinity(index);
var actualValue = slice(classof(this), 0, 3) === 'Big' ? toBigInt(value) : +value;
return arrayWith(this, this[TYPED_ARRAY_CONSTRUCTOR], relativeIndex, actualValue);
return arrayWith(this, getTypedArrayConstructor(this), relativeIndex, actualValue);
} }['with'], !PROPER_ORDER);

0 comments on commit ca95307

Please sign in to comment.