Skip to content

Commit

Permalink
Safari 10.0 - 14.0 %TypedArray%.prototype.sort accept incorrect arg…
Browse files Browse the repository at this point in the history
…uments
  • Loading branch information
zloirock committed Jun 5, 2021
1 parent 39919be commit e7e81d8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changelog
##### Unreleased
- Added polyfill of stable sort in `{ Array, %TypedArray% }.prototype.sort`, [#769](https://github.com/zloirock/core-js/issues/769)
- Fixed `Safari` 14.0- `{ Array, %TypedArray% }.prototype.sort` validation of arguments bug
- `.at` marked as supported from V8 9.2

##### 3.13.1 - 2021.05.29
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js-compat/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,8 @@ const data = {
'es.typed-array.sort': {
chrome: '74',
firefox: '67',
safari: '10.0',
// 10.0 - 14.0 accept incorrect arguments
safari: '14.1',
},
'es.typed-array.subarray': {
chrome: '26',
Expand Down
25 changes: 19 additions & 6 deletions packages/core-js/modules/es.typed-array.sort.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
'use strict';
/* eslint-disable es/no-typed-arrays -- required for testing */
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
var global = require('../internals/global');
var fails = require('../internals/fails');
var $sort = require('../internals/array-sort');
var aFunction = require('../internals/a-function');
var arraySort = require('../internals/array-sort');
var FF = require('../internals/engine-ff-version');
var IE_OR_EDGE = require('../internals/engine-is-ie-or-edge');
var V8 = require('../internals/engine-v8-version');
var WEBKIT = require('../internals/engine-webkit-version');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
var Uint16Array = global.Uint16Array;
var nativeSort = Uint16Array && Uint16Array.prototype.sort;

var STABLE_SORT = !fails(function () {
// WebKit
var ACCEPT_INCORRECT_ARGUMENTS = !!nativeSort && !fails(function () {
var array = new Uint16Array(2);
array.sort(null);
array.sort({});
});

var STABLE_SORT = !!nativeSort && !fails(function () {
// feature detection can be too slow, so check engines versions
if (V8) return V8 < 73;
if (V8) return V8 < 74;
if (FF) return FF < 67;
if (IE_OR_EDGE) return true;
if (WEBKIT) return WEBKIT < 602;

// eslint-disable-next-line es/no-typed-arrays -- required for testing
var array = new Uint16Array(516);
var expected = Array(516);
var index, mod;
Expand All @@ -40,5 +51,7 @@ var STABLE_SORT = !fails(function () {
// `%TypedArray%.prototype.sort` method
// https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort
exportTypedArrayMethod('sort', function sort(comparefn) {
return $sort.call(aTypedArray(this), comparefn);
}, !STABLE_SORT);
if (!STABLE_SORT) return arraySort.call(aTypedArray(this), comparefn);
if (comparefn !== undefined) aFunction(comparefn);
return nativeSort.call(this, comparefn);
}, !STABLE_SORT || ACCEPT_INCORRECT_ARGUMENTS);

0 comments on commit e7e81d8

Please sign in to comment.