From 4af4a76f63dc3367ad2cf0e3250a283d80f55e82 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Thu, 2 Jun 2022 00:06:16 +0700 Subject: [PATCH 1/2] change the order of operations in `%TypedArray%.prototype.with` --- CHANGELOG.md | 1 + .../modules/esnext.typed-array.with.js | 19 +++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f45c4c67b0..94e220acb286 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Changelog ##### Unreleased +- Changed the order of operations in `%TypedArray%.prototype.with` following [proposal-change-array-by-copy/86](https://github.com/tc39/proposal-change-array-by-copy/issues/86) - Fixed a bug in the order of getting flags in `RegExp.prototype.flags` in the actual version of V8 - Fixed property descriptors of some `Math` and `Number` constants - Added detection of NodeJS [bug](https://github.com/nodejs/node/issues/41038) in `structuredClone` that can not clone `DOMException` (just in case for future versions that will fix other issues) diff --git a/packages/core-js/modules/esnext.typed-array.with.js b/packages/core-js/modules/esnext.typed-array.with.js index 9936f839c90d..dc2f82d4a082 100644 --- a/packages/core-js/modules/esnext.typed-array.with.js +++ b/packages/core-js/modules/esnext.typed-array.with.js @@ -1,22 +1,21 @@ 'use strict'; var arrayWith = require('../internals/array-with'); var ArrayBufferViewCore = require('../internals/array-buffer-view-core'); -// var toIntegerOrInfinity = require('../internals/to-integer-or-infinity'); -// var toBigInt = require('../internals/to-big-int'); -// var classof = require('../internals/classof'); -// var uncurryThis = require('../internals/function-uncurry-this'); +var toIntegerOrInfinity = require('../internals/to-integer-or-infinity'); +var toBigInt = require('../internals/to-big-int'); +var classof = require('../internals/classof'); +var uncurryThis = require('../internals/function-uncurry-this'); var aTypedArray = ArrayBufferViewCore.aTypedArray; var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod; var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR; -// var slice = uncurryThis(''.slice); +var slice = uncurryThis(''.slice); // `%TypedArray%.prototype.with` method // https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.with 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(aTypedArray(this), this[TYPED_ARRAY_CONSTRUCTOR], 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); } }['with']); From da4c8af71cbbe65d637814ee18b0863f13d8f535 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Fri, 10 Jun 2022 00:51:47 +0700 Subject: [PATCH 2/2] add detection of order --- packages/core-js/modules/esnext.typed-array.with.js | 13 ++++++++++++- tests/compat/tests.js | 6 +++++- tests/tests/esnext.typed-array.with.js | 10 ++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/core-js/modules/esnext.typed-array.with.js b/packages/core-js/modules/esnext.typed-array.with.js index dc2f82d4a082..95344096ddb5 100644 --- a/packages/core-js/modules/esnext.typed-array.with.js +++ b/packages/core-js/modules/esnext.typed-array.with.js @@ -11,6 +11,17 @@ var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod; var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR; var slice = uncurryThis(''.slice); +var PROPER_ORDER = !!function () { + try { + // eslint-disable-next-line no-throw-literal, es-x/no-typed-arrays -- required for testing + new Int8Array(1)['with'](2, { valueOf: function () { throw 8; } }); + } catch (error) { + // some early implementations, like WebKit, does not follow the final semantic + // https://github.com/tc39/proposal-change-array-by-copy/pull/86 + return error === 8; + } +}(); + // `%TypedArray%.prototype.with` method // https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.with exportTypedArrayMethod('with', { 'with': function (index, value) { @@ -18,4 +29,4 @@ exportTypedArrayMethod('with', { 'with': function (index, value) { var relativeIndex = toIntegerOrInfinity(index); var actualValue = slice(classof(this), 0, 3) === 'Big' ? toBigInt(value) : +value; return arrayWith(this, this[TYPED_ARRAY_CONSTRUCTOR], relativeIndex, actualValue); -} }['with']); +} }['with'], !PROPER_ORDER); diff --git a/tests/compat/tests.js b/tests/compat/tests.js index e839ea1ea1eb..4fd55421c0e1 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1704,7 +1704,11 @@ GLOBAL.tests = { return Int8Array.prototype.uniqueBy; }, 'esnext.typed-array.with': function () { - return Int8Array.prototype['with']; + try { + new Int8Array(1)['with'](2, { valueOf: function () { throw 8; } }); + } catch (error) { + return error === 8; + } }, 'esnext.weak-map.delete-all': function () { return WeakMap.prototype.deleteAll; diff --git a/tests/tests/esnext.typed-array.with.js b/tests/tests/esnext.typed-array.with.js index 5dff50fda46c..fb3a087c2de6 100644 --- a/tests/tests/esnext.typed-array.with.js +++ b/tests/tests/esnext.typed-array.with.js @@ -30,5 +30,15 @@ if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.with', assert => { assert.same(new TypedArray(5).with(2, checker)[2], 10); assert.same(checker.$valueOf, 1, 'valueOf calls'); assert.same(checker.$toString, 0, 'toString calls'); + + assert.true(!!function () { + try { + new Int8Array(1).with(2, { valueOf() { throw 8; } }); + } catch (error) { + // some early implementations, like WebKit, does not follow the final semantic + // https://github.com/tc39/proposal-change-array-by-copy/pull/86 + return error === 8; + } + }(), 'proper order of operations'); } });