diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b800c0df423..8d57fae14ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/core-js/internals/array-buffer-view-core.js b/packages/core-js/internals/array-buffer-view-core.js index d8ed1e701158..d14bf5dc3a78 100644 --- a/packages/core-js/internals/array-buffer-view-core.js +++ b/packages/core-js/internals/array-buffer-view-core.js @@ -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; @@ -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; @@ -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); @@ -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 @@ -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, diff --git a/packages/core-js/internals/typed-array-constructor.js b/packages/core-js/internals/typed-array-constructor.js index c1c3b1a2583b..1d29fc877d20 100644 --- a/packages/core-js/internals/typed-array-constructor.js +++ b/packages/core-js/internals/typed-array-constructor.js @@ -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; @@ -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; @@ -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); diff --git a/packages/core-js/internals/typed-array-species-constructor.js b/packages/core-js/internals/typed-array-species-constructor.js index c1c5ac8a9583..f5a4e901794e 100644 --- a/packages/core-js/internals/typed-array-species-constructor.js +++ b/packages/core-js/internals/typed-array-species-constructor.js @@ -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))); }; diff --git a/packages/core-js/modules/esnext.typed-array.to-reversed.js b/packages/core-js/modules/esnext.typed-array.to-reversed.js index bdc9d4664756..18f211e4a755 100644 --- a/packages/core-js/modules/esnext.typed-array.to-reversed.js +++ b/packages/core-js/modules/esnext.typed-array.to-reversed.js @@ -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)); }); diff --git a/packages/core-js/modules/esnext.typed-array.to-sorted.js b/packages/core-js/modules/esnext.typed-array.to-sorted.js index d7dc40f4380e..4258b95fc892 100644 --- a/packages/core-js/modules/esnext.typed-array.to-sorted.js +++ b/packages/core-js/modules/esnext.typed-array.to-sorted.js @@ -5,8 +5,8 @@ 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 @@ -14,6 +14,6 @@ var sort = uncurryThis(ArrayBufferViewCore.TypedArrayPrototype.sort); 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); }); diff --git a/packages/core-js/modules/esnext.typed-array.to-spliced.js b/packages/core-js/modules/esnext.typed-array.to-spliced.js index 64706ae9ce70..cb66dffb6f53 100644 --- a/packages/core-js/modules/esnext.typed-array.to-spliced.js +++ b/packages/core-js/modules/esnext.typed-array.to-spliced.js @@ -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 }); diff --git a/packages/core-js/modules/esnext.typed-array.with.js b/packages/core-js/modules/esnext.typed-array.with.js index 95344096ddb5..31768c7de892 100644 --- a/packages/core-js/modules/esnext.typed-array.with.js +++ b/packages/core-js/modules/esnext.typed-array.with.js @@ -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 () { @@ -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);