Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the order of operations in %TypedArray%.prototype.with #1087

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
32 changes: 21 additions & 11 deletions packages/core-js/modules/esnext.typed-array.with.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
'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);

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) {
// 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);
} }['with']);
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'], !PROPER_ORDER);
6 changes: 5 additions & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions tests/tests/esnext.typed-array.with.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});