summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/TypedArray/prototype/sort
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/tests/test262/built-ins/TypedArray/prototype/sort')
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/arraylength-internal.js41
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/browser.js0
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-call-throws.js44
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-calls.js44
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-is-undefined.js25
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-nonfunction-call-throws.js57
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/detached-buffer.js33
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-abrupt-from-this-out-of-bounds.js62
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-same-instance.js27
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/shell.js42
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sortcompare-with-no-tostring.js33
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sorted-values.js33
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/arraylength-internal.js41
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/browser.js0
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-call-throws.js44
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-calls.js44
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-is-undefined.js25
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-nonfunction-call-throws.js57
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/detached-buffer.js33
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-func.js37
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-method.js37
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/length.js32
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/name.js29
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/not-a-constructor.js35
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/prop-desc.js21
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-abrupt-from-this-out-of-bounds.js62
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-same-instance.js27
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/shell.js24
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/sort-tonumber.js35
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/sortcompare-with-no-tostring.js33
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values-nan.js40
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values.js59
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/stability.js47
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-object.js53
-rw-r--r--js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-typedarray-instance.js45
35 files changed, 1301 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/arraylength-internal.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/arraylength-internal.js
new file mode 100644
index 0000000000..9f22698cee
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/arraylength-internal.js
@@ -0,0 +1,41 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Use internal ArrayLength instead of getting a length property
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ ...
+ 3. Let len be the value of obj's [[ArrayLength]] internal slot.
+includes: [testBigIntTypedArray.js, compareArray.js]
+features: [BigInt, TypedArray]
+---*/
+
+var getCalls = 0;
+var desc = {
+ get: function getLen() {
+ getCalls++;
+ return 0;
+ }
+};
+
+Object.defineProperty(TypedArray.prototype, "length", desc);
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample = new TA([42n, 42n, 42n]);
+ getCalls = 0;
+
+ Object.defineProperty(TA.prototype, "length", desc);
+ Object.defineProperty(sample, "length", desc);
+
+ var result = sample.sort();
+
+ assert.sameValue(getCalls, 0, "ignores length properties");
+ assert(
+ compareArray(result, sample),
+ "result is not affected by custom length"
+ );
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/browser.js
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/browser.js
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-call-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-call-throws.js
new file mode 100644
index 0000000000..87b67132b2
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-call-throws.js
@@ -0,0 +1,44 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Returns abrupt from comparefn
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+ 2. If the argument comparefn is not undefined, then
+ a. Let v be ? Call(comparefn, undefined, « x, y »).
+ ...
+ ...
+
+ 22.1.3.25 Array.prototype.sort (comparefn)
+
+ The following steps are taken:
+
+ - If an abrupt completion is returned from any of these operations, it is
+ immediately returned as the value of this function.
+includes: [testBigIntTypedArray.js]
+features: [BigInt, TypedArray]
+---*/
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample = new TA([42n, 43n, 44n, 45n, 46n]);
+ var calls = 0;
+
+ var comparefn = function() {
+ calls += 1;
+ throw new Test262Error();
+ };
+
+ assert.throws(Test262Error, function() {
+ sample.sort(comparefn);
+ });
+
+ assert.sameValue(calls, 1, "immediately returned");
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-calls.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-calls.js
new file mode 100644
index 0000000000..e1133bb3fc
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-calls.js
@@ -0,0 +1,44 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: comparefn is called if not undefined
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+ 2. If the argument comparefn is not undefined, then
+ a. Let v be ? Call(comparefn, undefined, « x, y »).
+ ...
+ ...
+includes: [testBigIntTypedArray.js]
+features: [BigInt, TypedArray]
+---*/
+
+var expectedThis = (function() {
+ return this;
+})();
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample = new TA([42n, 42n, 42n, 42n, 42n]);
+ var calls = [];
+
+ var comparefn = function() {
+ calls.push([this, arguments]);
+ };
+
+ sample.sort(comparefn);
+
+ assert(calls.length > 0, "calls comparefn");
+ calls.forEach(function(args) {
+ assert.sameValue(args[0], expectedThis, "comparefn is called no specific this");
+ assert.sameValue(args[1].length, 2, "comparefn is always called with 2 args");
+ assert.sameValue(args[1][0], 42n, "x is a listed value");
+ assert.sameValue(args[1][0], 42n, "y is a listed value");
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-is-undefined.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-is-undefined.js
new file mode 100644
index 0000000000..ed3575fba8
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-is-undefined.js
@@ -0,0 +1,25 @@
+// Copyright (C) 2020 Rick Waldron. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: >
+ Treats explicit undefined comparefn the same as implicit undefined comparefn
+info: |
+ %TypedArray%.prototype.sort ( comparefn )
+
+ 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
+ ...
+includes: [compareArray.js, testBigIntTypedArray.js]
+features: [TypedArray, BigInt]
+---*/
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ let sample = new TA([42n, 44n, 46n, 43n, 45n]);
+ let explicit = sample.sort(undefined);
+ let implicit = sample.sort();
+
+ assert.compareArray(explicit, [42n, 43n, 44n, 45n, 46n], 'The value of `explicit` is [42n, 43n, 44n, 45n, 46n]');
+ assert.compareArray(implicit, [42n, 43n, 44n, 45n, 46n], 'The value of `implicit` is [42n, 43n, 44n, 45n, 46n]');
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-nonfunction-call-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-nonfunction-call-throws.js
new file mode 100644
index 0000000000..2c96e3099d
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/comparefn-nonfunction-call-throws.js
@@ -0,0 +1,57 @@
+// Copyright (C) 2017 Jordan Harband. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: throws on a non-undefined non-function
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ Upon entry, the following steps are performed to initialize evaluation
+ of the sort function. These steps are used instead of the entry steps
+ in 22.1.3.25:
+
+ ...
+ 1. If _comparefn_ is not *undefined* and IsCallable(_comparefn_) is *false*, throw a *TypeError* exception.
+ ...
+
+includes: [testBigIntTypedArray.js]
+features: [BigInt, TypedArray]
+---*/
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample = new TA([42n, 43n, 44n, 45n, 46n]);
+
+ assert.throws(TypeError, function() {
+ sample.sort(null);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(true);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(false);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort('');
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(/a/g);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(42);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort([]);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort({});
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/detached-buffer.js
new file mode 100644
index 0000000000..5f21eb1dda
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/detached-buffer.js
@@ -0,0 +1,33 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Throws a TypeError if this has a detached buffer
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ 1. Let obj be the this value.
+ 2. Let buffer be ? ValidateTypedArray(obj).
+
+ 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
+
+ ...
+ 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
+ ...
+includes: [testBigIntTypedArray.js, detachArrayBuffer.js]
+features: [BigInt, TypedArray]
+---*/
+
+var comparefn = function() {
+ throw new Test262Error();
+};
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample = new TA(1);
+ $DETACHBUFFER(sample.buffer);
+ assert.throws(TypeError, function() {
+ sample.sort(comparefn);
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-abrupt-from-this-out-of-bounds.js
new file mode 100644
index 0000000000..dee08240ed
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-abrupt-from-this-out-of-bounds.js
@@ -0,0 +1,62 @@
+// |reftest| skip -- resizable-arraybuffer is not supported
+// Copyright (C) 2021 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Return abrupt when "this" value fails buffer boundary checks
+includes: [testBigIntTypedArray.js]
+features: [ArrayBuffer, BigInt, TypedArray, arrow-function, resizable-arraybuffer]
+---*/
+
+assert.sameValue(
+ typeof TypedArray.prototype.sort,
+ 'function',
+ 'implements TypedArray.prototype.sort'
+);
+
+assert.sameValue(
+ typeof ArrayBuffer.prototype.resize,
+ 'function',
+ 'implements ArrayBuffer.prototype.resize'
+);
+
+testWithBigIntTypedArrayConstructors(TA => {
+ var BPE = TA.BYTES_PER_ELEMENT;
+ var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
+ var array = new TA(ab, BPE, 2);
+
+ try {
+ ab.resize(BPE * 5);
+ } catch (_) {}
+
+ // no error following grow:
+ array.sort();
+
+ try {
+ ab.resize(BPE * 3);
+ } catch (_) {}
+
+ // no error following shrink (within bounds):
+ array.sort();
+
+ var expectedError;
+ try {
+ ab.resize(BPE * 2);
+ // If the preceding "resize" operation is successful, the typed array will
+ // be out out of bounds, so the subsequent prototype method should produce
+ // a TypeError due to the semantics of ValidateTypedArray.
+ expectedError = TypeError;
+ } catch (_) {
+ // The host is permitted to fail any "resize" operation at its own
+ // discretion. If that occurs, the sort operation should complete
+ // successfully.
+ expectedError = Test262Error;
+ }
+
+ assert.throws(expectedError, () => {
+ array.sort();
+ throw new Test262Error('sort completed successfully');
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-same-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-same-instance.js
new file mode 100644
index 0000000000..813b6d3659
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/return-same-instance.js
@@ -0,0 +1,27 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Returns the same instance
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+includes: [testBigIntTypedArray.js]
+features: [BigInt, TypedArray]
+---*/
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample = new TA([2n, 1n]);
+ var result = sample.sort();
+
+ assert.sameValue(sample, result, "without comparefn");
+
+ result = sample.sort(function() { return 0; });
+ assert.sameValue(sample, result, "with comparefn");
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/shell.js
new file mode 100644
index 0000000000..90ee9c114d
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/shell.js
@@ -0,0 +1,42 @@
+// GENERATED, DO NOT EDIT
+// file: testBigIntTypedArray.js
+// Copyright (C) 2015 André Bargull. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+description: |
+ Collection of functions used to assert the correctness of BigInt TypedArray objects.
+defines:
+ - TypedArray
+ - testWithBigIntTypedArrayConstructors
+---*/
+
+/**
+ * The %TypedArray% intrinsic constructor function.
+ */
+var TypedArray = Object.getPrototypeOf(Int8Array);
+
+/**
+ * Calls the provided function for every typed array constructor.
+ *
+ * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor.
+ * @param {Array} selected - An optional Array with filtered typed arrays
+ */
+function testWithBigIntTypedArrayConstructors(f, selected) {
+ /**
+ * Array containing every BigInt typed array constructor.
+ */
+ var constructors = selected || [
+ BigInt64Array,
+ BigUint64Array
+ ];
+
+ for (var i = 0; i < constructors.length; ++i) {
+ var constructor = constructors[i];
+ try {
+ f(constructor);
+ } catch (e) {
+ e.message += " (Testing with " + constructor.name + ".)";
+ throw e;
+ }
+ }
+}
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sortcompare-with-no-tostring.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sortcompare-with-no-tostring.js
new file mode 100644
index 0000000000..e610ad58a5
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sortcompare-with-no-tostring.js
@@ -0,0 +1,33 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: TypedArrays sort does not cast values to String
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+ 2. If the argument comparefn is not undefined, then
+ a. Let v be ? Call(comparefn, undefined, « x, y »).
+ ...
+ ...
+includes: [testBigIntTypedArray.js, compareArray.js]
+features: [BigInt, TypedArray]
+---*/
+
+var toStringCalled = false;
+BigInt.prototype.toString = function() {
+ toStringCalled = true;
+}
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample = new TA([20n, 100n, 3n]);
+ var result = sample.sort();
+ assert.sameValue(toStringCalled, false, "BigInt.prototype.toString will not be called");
+ assert(compareArray(result, [3n, 20n, 100n]));
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sorted-values.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sorted-values.js
new file mode 100644
index 0000000000..a56f0c442a
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/BigInt/sorted-values.js
@@ -0,0 +1,33 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Sort values to numeric ascending order
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+includes: [testBigIntTypedArray.js, compareArray.js]
+features: [BigInt, TypedArray]
+---*/
+
+testWithBigIntTypedArrayConstructors(function(TA) {
+ var sample;
+
+ sample = new TA([4n, 3n, 2n, 1n]).sort();
+ assert(compareArray(sample, [1n, 2n, 3n, 4n]), "descending values");
+
+ sample = new TA([3n, 4n, 1n, 2n]).sort();
+ assert(compareArray(sample, [1n, 2n, 3n, 4n]), "mixed numbers");
+
+ sample = new TA([3n, 4n, 3n, 1n, 0n, 1n, 2n]).sort();
+ assert(compareArray(sample, [0n, 1n, 1n, 2n, 3n, 3n, 4n]), "repeating numbers");
+});
+
+var sample = new BigInt64Array([-4n, 3n, 4n, -3n, 2n, -2n, 1n, 0n]).sort();
+assert(compareArray(sample, [-4n, -3n, -2n, 0n, 1n, 2n, 3n, 4n]), "negative values");
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/arraylength-internal.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/arraylength-internal.js
new file mode 100644
index 0000000000..b11284fb67
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/arraylength-internal.js
@@ -0,0 +1,41 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Use internal ArrayLength instead of getting a length property
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ ...
+ 3. Let len be the value of obj's [[ArrayLength]] internal slot.
+includes: [testTypedArray.js, compareArray.js]
+features: [TypedArray]
+---*/
+
+var getCalls = 0;
+var desc = {
+ get: function getLen() {
+ getCalls++;
+ return 0;
+ }
+};
+
+Object.defineProperty(TypedArray.prototype, "length", desc);
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([42, 42, 42]);
+ getCalls = 0;
+
+ Object.defineProperty(TA.prototype, "length", desc);
+ Object.defineProperty(sample, "length", desc);
+
+ var result = sample.sort();
+
+ assert.sameValue(getCalls, 0, "ignores length properties");
+ assert(
+ compareArray(result, sample),
+ "result is not affected by custom length"
+ );
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/browser.js
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/browser.js
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-call-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-call-throws.js
new file mode 100644
index 0000000000..4ec5160af3
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-call-throws.js
@@ -0,0 +1,44 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Returns abrupt from comparefn
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+ 2. If the argument comparefn is not undefined, then
+ a. Let v be ? Call(comparefn, undefined, « x, y »).
+ ...
+ ...
+
+ 22.1.3.25 Array.prototype.sort (comparefn)
+
+ The following steps are taken:
+
+ - If an abrupt completion is returned from any of these operations, it is
+ immediately returned as the value of this function.
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([42, 43, 44, 45, 46]);
+ var calls = 0;
+
+ var comparefn = function() {
+ calls += 1;
+ throw new Test262Error();
+ };
+
+ assert.throws(Test262Error, function() {
+ sample.sort(comparefn);
+ });
+
+ assert.sameValue(calls, 1, "immediately returned");
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-calls.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-calls.js
new file mode 100644
index 0000000000..53c60777d9
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-calls.js
@@ -0,0 +1,44 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: comparefn is called if not undefined
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+ 2. If the argument comparefn is not undefined, then
+ a. Let v be ? Call(comparefn, undefined, « x, y »).
+ ...
+ ...
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+var expectedThis = (function() {
+ return this;
+})();
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([42, 42, 42, 42, 42]);
+ var calls = [];
+
+ var comparefn = function() {
+ calls.push([this, arguments]);
+ };
+
+ sample.sort(comparefn);
+
+ assert(calls.length > 0, "calls comparefn");
+ calls.forEach(function(args) {
+ assert.sameValue(args[0], expectedThis, "comparefn is called no specific this");
+ assert.sameValue(args[1].length, 2, "comparefn is always called with 2 args");
+ assert.sameValue(args[1][0], 42, "x is a listed value");
+ assert.sameValue(args[1][0], 42, "y is a listed value");
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-is-undefined.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-is-undefined.js
new file mode 100644
index 0000000000..f63e734f2d
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-is-undefined.js
@@ -0,0 +1,25 @@
+// Copyright (C) 2020 Rick Waldron. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: >
+ Treats explicit undefined comparefn the same as implicit undefined comparefn
+info: |
+ %TypedArray%.prototype.sort ( comparefn )
+
+ 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
+ ...
+includes: [compareArray.js, testTypedArray.js]
+features: [TypedArray]
+---*/
+
+testWithTypedArrayConstructors(function(TA) {
+ let sample = new TA([42, 44, 46, 43, 45]);
+ let explicit = sample.sort(undefined);
+ let implicit = sample.sort();
+
+ assert.compareArray(explicit, [42, 43, 44, 45, 46], 'The value of `explicit` is [42, 43, 44, 45, 46]');
+ assert.compareArray(implicit, [42, 43, 44, 45, 46], 'The value of `implicit` is [42, 43, 44, 45, 46]');
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-nonfunction-call-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-nonfunction-call-throws.js
new file mode 100644
index 0000000000..8bbdd3bce0
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/comparefn-nonfunction-call-throws.js
@@ -0,0 +1,57 @@
+// Copyright (C) 2017 Jordan Harband. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: throws on a non-undefined non-function
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ Upon entry, the following steps are performed to initialize evaluation
+ of the sort function. These steps are used instead of the entry steps
+ in 22.1.3.25:
+
+ ...
+ 1. If _comparefn_ is not *undefined* and IsCallable(_comparefn_) is *false*, throw a *TypeError* exception.
+ ...
+
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([42, 43, 44, 45, 46]);
+
+ assert.throws(TypeError, function() {
+ sample.sort(null);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(true);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(false);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort('');
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(/a/g);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort(42);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort([]);
+ });
+
+ assert.throws(TypeError, function() {
+ sample.sort({});
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/detached-buffer.js
new file mode 100644
index 0000000000..cb25eab0dd
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/detached-buffer.js
@@ -0,0 +1,33 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Throws a TypeError if this has a detached buffer
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ 1. Let obj be the this value.
+ 2. Let buffer be ? ValidateTypedArray(obj).
+
+ 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
+
+ ...
+ 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
+ ...
+includes: [testTypedArray.js, detachArrayBuffer.js]
+features: [TypedArray]
+---*/
+
+var comparefn = function() {
+ throw new Test262Error();
+};
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA(1);
+ $DETACHBUFFER(sample.buffer);
+ assert.throws(TypeError, function() {
+ sample.sort(comparefn);
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-func.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-func.js
new file mode 100644
index 0000000000..582fe56c42
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-func.js
@@ -0,0 +1,37 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Throws a TypeError exception when invoked as a function
+info: |
+ 22.2.3.25 %TypedArray%.prototype.sort ( comparefn )
+
+ ...
+ This function is not generic. The this value must be an object with a
+ [[TypedArrayName]] internal slot.
+ ...
+
+ 1. Let obj be the this value as the argument.
+ 2. Let buffer be ValidateTypedArray(obj).
+ 3. ReturnIfAbrupt(buffer).
+ ...
+
+ 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
+
+ 1. If Type(O) is not Object, throw a TypeError exception.
+ 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError
+ exception.
+ ...
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+var sort = TypedArray.prototype.sort;
+
+assert.sameValue(typeof sort, 'function');
+
+assert.throws(TypeError, function() {
+ sort();
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-method.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-method.js
new file mode 100644
index 0000000000..50f84f46dd
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/invoked-as-method.js
@@ -0,0 +1,37 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Requires a [[TypedArrayName]] internal slot.
+info: |
+ 22.2.3.25 %TypedArray%.prototype.sort ( comparefn )
+
+ ...
+ This function is not generic. The this value must be an object with a
+ [[TypedArrayName]] internal slot.
+ ...
+
+ 1. Let obj be the this value as the argument.
+ 2. Let buffer be ValidateTypedArray(obj).
+ 3. ReturnIfAbrupt(buffer).
+ ...
+
+ 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
+
+ 1. If Type(O) is not Object, throw a TypeError exception.
+ 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError
+ exception.
+ ...
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+var TypedArrayPrototype = TypedArray.prototype;
+
+assert.sameValue(typeof TypedArrayPrototype.sort, 'function');
+
+assert.throws(TypeError, function() {
+ TypedArrayPrototype.sort();
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/length.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/length.js
new file mode 100644
index 0000000000..bb8480eaa4
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/length.js
@@ -0,0 +1,32 @@
+// Copyright (C) 2015 André Bargull. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: >
+ %TypedArray%.prototype.sort.length is 1.
+info: |
+ %TypedArray%.prototype.sort ( comparefn )
+
+ 17 ECMAScript Standard Built-in Objects:
+ Every built-in Function object, including constructors, has a length
+ property whose value is an integer. Unless otherwise specified, this
+ value is equal to the largest number of named arguments shown in the
+ subclause headings for the function description, including optional
+ parameters. However, rest parameters shown using the form “...name”
+ are not included in the default argument count.
+
+ Unless otherwise specified, the length property of a built-in Function
+ object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
+ [[Configurable]]: true }.
+includes: [propertyHelper.js, testTypedArray.js]
+features: [TypedArray]
+---*/
+
+assert.sameValue(TypedArray.prototype.sort.length, 1);
+
+verifyNotEnumerable(TypedArray.prototype.sort, "length");
+verifyNotWritable(TypedArray.prototype.sort, "length");
+verifyConfigurable(TypedArray.prototype.sort, "length");
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/name.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/name.js
new file mode 100644
index 0000000000..347586ddce
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/name.js
@@ -0,0 +1,29 @@
+// Copyright (C) 2015 André Bargull. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: >
+ %TypedArray%.prototype.sort.name is "sort".
+info: |
+ %TypedArray%.prototype.sort ( comparefn )
+
+ 17 ECMAScript Standard Built-in Objects:
+ Every built-in Function object, including constructors, that is not
+ identified as an anonymous function has a name property whose value
+ is a String.
+
+ Unless otherwise specified, the name property of a built-in Function
+ object, if it exists, has the attributes { [[Writable]]: false,
+ [[Enumerable]]: false, [[Configurable]]: true }.
+includes: [propertyHelper.js, testTypedArray.js]
+features: [TypedArray]
+---*/
+
+assert.sameValue(TypedArray.prototype.sort.name, "sort");
+
+verifyNotEnumerable(TypedArray.prototype.sort, "name");
+verifyNotWritable(TypedArray.prototype.sort, "name");
+verifyConfigurable(TypedArray.prototype.sort, "name");
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/not-a-constructor.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/not-a-constructor.js
new file mode 100644
index 0000000000..4814d4a7df
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/not-a-constructor.js
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 Rick Waldron. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+esid: sec-ecmascript-standard-built-in-objects
+description: >
+ TypedArray.prototype.sort does not implement [[Construct]], is not new-able
+info: |
+ ECMAScript Function Objects
+
+ Built-in function objects that are not identified as constructors do not
+ implement the [[Construct]] internal method unless otherwise specified in
+ the description of a particular function.
+
+ sec-evaluatenew
+
+ ...
+ 7. If IsConstructor(constructor) is false, throw a TypeError exception.
+ ...
+includes: [isConstructor.js, testTypedArray.js]
+features: [Reflect.construct, arrow-function, TypedArray]
+---*/
+
+assert.sameValue(
+ isConstructor(TypedArray.prototype.sort),
+ false,
+ 'isConstructor(TypedArray.prototype.sort) must return false'
+);
+
+assert.throws(TypeError, () => {
+ let u8 = new Uint8Array(1); new u8.sort();
+}, '`let u8 = new Uint8Array(1); new u8.sort()` throws TypeError');
+
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/prop-desc.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/prop-desc.js
new file mode 100644
index 0000000000..8d0d2d273b
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/prop-desc.js
@@ -0,0 +1,21 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: >
+ "sort" property of TypedArrayPrototype
+info: |
+ ES6 section 17: Every other data property described in clauses 18 through 26
+ and in Annex B.2 has the attributes { [[Writable]]: true,
+ [[Enumerable]]: false, [[Configurable]]: true } unless otherwise specified.
+includes: [propertyHelper.js, testTypedArray.js]
+features: [TypedArray]
+---*/
+
+var TypedArrayPrototype = TypedArray.prototype;
+
+verifyNotEnumerable(TypedArrayPrototype, 'sort');
+verifyWritable(TypedArrayPrototype, 'sort');
+verifyConfigurable(TypedArrayPrototype, 'sort');
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-abrupt-from-this-out-of-bounds.js
new file mode 100644
index 0000000000..aa4f91883b
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-abrupt-from-this-out-of-bounds.js
@@ -0,0 +1,62 @@
+// |reftest| skip -- resizable-arraybuffer is not supported
+// Copyright (C) 2021 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Return abrupt when "this" value fails buffer boundary checks
+includes: [testTypedArray.js]
+features: [ArrayBuffer, TypedArray, arrow-function, resizable-arraybuffer]
+---*/
+
+assert.sameValue(
+ typeof TypedArray.prototype.sort,
+ 'function',
+ 'implements TypedArray.prototype.sort'
+);
+
+assert.sameValue(
+ typeof ArrayBuffer.prototype.resize,
+ 'function',
+ 'implements ArrayBuffer.prototype.resize'
+);
+
+testWithTypedArrayConstructors(TA => {
+ var BPE = TA.BYTES_PER_ELEMENT;
+ var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
+ var array = new TA(ab, BPE, 2);
+
+ try {
+ ab.resize(BPE * 5);
+ } catch (_) {}
+
+ // no error following grow:
+ array.sort();
+
+ try {
+ ab.resize(BPE * 3);
+ } catch (_) {}
+
+ // no error following shrink (within bounds):
+ array.sort();
+
+ var expectedError;
+ try {
+ ab.resize(BPE * 2);
+ // If the preceding "resize" operation is successful, the typed array will
+ // be out out of bounds, so the subsequent prototype method should produce
+ // a TypeError due to the semantics of ValidateTypedArray.
+ expectedError = TypeError;
+ } catch (_) {
+ // The host is permitted to fail any "resize" operation at its own
+ // discretion. If that occurs, the sort operation should complete
+ // successfully.
+ expectedError = Test262Error;
+ }
+
+ assert.throws(expectedError, () => {
+ array.sort();
+ throw new Test262Error('sort completed successfully');
+ });
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-same-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-same-instance.js
new file mode 100644
index 0000000000..ad1283c0f5
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/return-same-instance.js
@@ -0,0 +1,27 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Returns the same instance
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([2, 1]);
+ var result = sample.sort();
+
+ assert.sameValue(sample, result, "without comparefn");
+
+ result = sample.sort(function() { return 0; });
+ assert.sameValue(sample, result, "with comparefn");
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/shell.js
new file mode 100644
index 0000000000..eda1477282
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/shell.js
@@ -0,0 +1,24 @@
+// GENERATED, DO NOT EDIT
+// file: isConstructor.js
+// Copyright (C) 2017 André Bargull. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+description: |
+ Test if a given function is a constructor function.
+defines: [isConstructor]
+features: [Reflect.construct]
+---*/
+
+function isConstructor(f) {
+ if (typeof f !== "function") {
+ throw new Test262Error("isConstructor invoked with a non-function value");
+ }
+
+ try {
+ Reflect.construct(function(){}, [], f);
+ } catch (e) {
+ return false;
+ }
+ return true;
+}
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sort-tonumber.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sort-tonumber.js
new file mode 100644
index 0000000000..dcc99bc87c
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sort-tonumber.js
@@ -0,0 +1,35 @@
+// Copyright (C) 2018 Mozilla Corporation. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: The result of compareFn is immediately passed through ToNumber
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ ...
+ 2. If comparefn is not undefined, then
+ a. Let v be ? ToNumber(? Call(comparefn, undefined, « x, y »)).
+ b. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
+ ...
+ ...
+includes: [testTypedArray.js, detachArrayBuffer.js]
+features: [TypedArray]
+---*/
+
+testWithTypedArrayConstructors(function(TA) {
+ var ta = new TA(4);
+ var ab = ta.buffer;
+
+ var called = false;
+ ta.sort(function(a, b) {
+ // Detaching the buffer does not cause sort to throw.
+ $DETACHBUFFER(ab);
+ return {
+ [Symbol.toPrimitive]() { called = true; }
+ };
+ });
+
+ assert.sameValue(true, called);
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sortcompare-with-no-tostring.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sortcompare-with-no-tostring.js
new file mode 100644
index 0000000000..82c8d16ad2
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sortcompare-with-no-tostring.js
@@ -0,0 +1,33 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: TypedArrays sort does not cast values to String
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+ 2. If the argument comparefn is not undefined, then
+ a. Let v be ? Call(comparefn, undefined, « x, y »).
+ ...
+ ...
+includes: [testTypedArray.js, compareArray.js]
+features: [TypedArray]
+---*/
+
+var toStringCalled = false;
+Number.prototype.toString = function() {
+ toStringCalled = true;
+}
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([20, 100, 3]);
+ var result = sample.sort();
+ assert.sameValue(toStringCalled, false, "Number.prototype.toString will not be called");
+ assert(compareArray(result, [3, 20, 100]), "Default sorting by value");
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values-nan.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values-nan.js
new file mode 100644
index 0000000000..f2928c9e1a
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values-nan.js
@@ -0,0 +1,40 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Sort values to numeric ascending order
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+
+ NOTE: Because NaN always compares greater than any other value, NaN property
+ values always sort to the end of the result when comparefn is not provided.
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample;
+
+ sample = new TA([2, NaN, NaN, 0, 1]).sort();
+ assert.sameValue(sample[0], 0, "#1 [0]");
+ assert.sameValue(sample[1], 1, "#1 [1]");
+ assert.sameValue(sample[2], 2, "#1 [2]");
+ assert.sameValue(sample[3], NaN, "#1 [3]");
+ assert.sameValue(sample[4], NaN, "#1 [4]");
+
+ sample = new TA([3, NaN, NaN, Infinity, 0, -Infinity, 2]).sort();
+ assert.sameValue(sample[0], -Infinity, "#2 [0]");
+ assert.sameValue(sample[1], 0, "#2 [1]");
+ assert.sameValue(sample[2], 2, "#2 [2]");
+ assert.sameValue(sample[3], 3, "#2 [3]");
+ assert.sameValue(sample[4], Infinity, "#2 [4]");
+ assert.sameValue(sample[5], NaN, "#2 [5]");
+ assert.sameValue(sample[6], NaN, "#2 [6]");
+}, [Float64Array, Float32Array]);
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values.js
new file mode 100644
index 0000000000..2d411d2a2d
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/sorted-values.js
@@ -0,0 +1,59 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Sort values to numeric ascending order
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ When the TypedArray SortCompare abstract operation is called with two
+ arguments x and y, the following steps are taken:
+
+ ...
+includes: [testTypedArray.js, compareArray.js]
+features: [TypedArray]
+---*/
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample;
+
+ sample = new TA([4, 3, 2, 1]).sort();
+ assert(compareArray(sample, [1, 2, 3, 4]), "descending values");
+
+ sample = new TA([3, 4, 1, 2]).sort();
+ assert(compareArray(sample, [1, 2, 3, 4]), "mixed numbers");
+
+ sample = new TA([3, 4, 3, 1, 0, 1, 2]).sort();
+ assert(compareArray(sample, [0, 1, 1, 2, 3, 3, 4]), "repeating numbers");
+});
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([1, 0, -0, 2]).sort();
+ assert(compareArray(sample, [-0, 0, 1, 2]), "0s");
+}, floatArrayConstructors);
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([1, 0, -0, 2]).sort();
+ assert(compareArray(sample, [0, 0, 1, 2]), "0s");
+}, intArrayConstructors);
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample = new TA([-4, 3, 4, -3, 2, -2, 1, 0]).sort();
+ assert(compareArray(sample, [-4, -3, -2, 0, 1, 2, 3, 4]), "negative values");
+}, [Float64Array, Float32Array, Int8Array, Int16Array, Int32Array]);
+
+testWithTypedArrayConstructors(function(TA) {
+ var sample;
+
+ sample = new TA([0.5, 0, 1.5, 1]).sort();
+ assert(compareArray(sample, [0, 0.5, 1, 1.5]), "non integers");
+
+ sample = new TA([0.5, 0, 1.5, -0.5, -1, -1.5, 1]).sort();
+ assert(compareArray(sample, [-1.5, -1, -0.5, 0, 0.5, 1, 1.5]), "non integers + negatives");
+
+ sample = new TA([3, 4, Infinity, -Infinity, 1, 2]).sort();
+ assert(compareArray(sample, [-Infinity, 1, 2, 3, 4, Infinity]), "infinities");
+
+}, [Float64Array, Float32Array]);
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/stability.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/stability.js
new file mode 100644
index 0000000000..3a904f9f02
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/stability.js
@@ -0,0 +1,47 @@
+// Copyright (C) 2019 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Stability of %TypedArray%.prototype.sort.
+info: |
+ https://github.com/tc39/ecma262/pull/1433
+includes: [testTypedArray.js, compareArray.js]
+features: [TypedArray]
+---*/
+
+// Treat 0..3, 4..7, etc. as equal.
+const compare = (a, b) => (a / 4 | 0) - (b / 4 | 0);
+
+testWithTypedArrayConstructors((TA) => {
+ // Create an array of the form `[0, 1, …, 126, 127]`.
+ const array = Array.from({ length: 128 }, (_, i) => i);
+
+ const typedArray1 = new TA(array);
+ assert(compareArray(
+ typedArray1.sort(compare),
+ array
+ ), 'pre-sorted');
+
+ // Reverse `array` in-place so it becomes `[127, 126, …, 1, 0]`.
+ array.reverse();
+
+ const typedArray2 = new TA(array);
+ assert(compareArray(
+ typedArray2.sort(compare),
+ [
+ 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8,
+ 15, 14, 13, 12, 19, 18, 17, 16, 23, 22, 21, 20,
+ 27, 26, 25, 24, 31, 30, 29, 28, 35, 34, 33, 32,
+ 39, 38, 37, 36, 43, 42, 41, 40, 47, 46, 45, 44,
+ 51, 50, 49, 48, 55, 54, 53, 52, 59, 58, 57, 56,
+ 63, 62, 61, 60, 67, 66, 65, 64, 71, 70, 69, 68,
+ 75, 74, 73, 72, 79, 78, 77, 76, 83, 82, 81, 80,
+ 87, 86, 85, 84, 91, 90, 89, 88, 95, 94, 93, 92,
+ 99, 98, 97, 96, 103, 102, 101, 100, 107, 106, 105, 104,
+ 111, 110, 109, 108, 115, 114, 113, 112, 119, 118, 117, 116,
+ 123, 122, 121, 120, 127, 126, 125, 124,
+ ]
+ ), 'not presorted');
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-object.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-object.js
new file mode 100644
index 0000000000..0331551ca8
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-object.js
@@ -0,0 +1,53 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: Throws a TypeError exception when `this` is not Object
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ 1. Let obj be the this value as the argument.
+ 2. Let buffer be ? ValidateTypedArray(obj).
+ ...
+
+ 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
+
+ 1. If Type(O) is not Object, throw a TypeError exception.
+ ...
+includes: [testTypedArray.js]
+features: [Symbol, TypedArray]
+---*/
+
+var sort = TypedArray.prototype.sort;
+var comparefn = function() {};
+
+assert.throws(TypeError, function() {
+ sort.call(undefined, comparefn);
+}, "this is undefined");
+
+assert.throws(TypeError, function() {
+ sort.call(null, comparefn);
+}, "this is null");
+
+assert.throws(TypeError, function() {
+ sort.call(42, comparefn);
+}, "this is 42");
+
+assert.throws(TypeError, function() {
+ sort.call("1", comparefn);
+}, "this is a string");
+
+assert.throws(TypeError, function() {
+ sort.call(true, comparefn);
+}, "this is true");
+
+assert.throws(TypeError, function() {
+ sort.call(false, comparefn);
+}, "this is false");
+
+var s = Symbol("s");
+assert.throws(TypeError, function() {
+ sort.call(s, comparefn);
+}, "this is a Symbol");
+
+reportCompare(0, 0);
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-typedarray-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-typedarray-instance.js
new file mode 100644
index 0000000000..8dfac62ce6
--- /dev/null
+++ b/js/src/tests/test262/built-ins/TypedArray/prototype/sort/this-is-not-typedarray-instance.js
@@ -0,0 +1,45 @@
+// Copyright (C) 2016 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-%typedarray%.prototype.sort
+description: >
+ Throws a TypeError exception when `this` is not a TypedArray instance
+info: |
+ 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
+
+ 1. Let obj be the this value as the argument.
+ 2. Let buffer be ? ValidateTypedArray(obj).
+ ...
+
+ 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
+
+ 1. If Type(O) is not Object, throw a TypeError exception.
+ 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError
+ exception.
+ ...
+includes: [testTypedArray.js]
+features: [TypedArray]
+---*/
+
+var sort = TypedArray.prototype.sort;
+var comparefn = function() {};
+
+assert.throws(TypeError, function() {
+ sort.call({}, comparefn);
+}, "this is an Object");
+
+assert.throws(TypeError, function() {
+ sort.call([], comparefn);
+}, "this is an Array");
+
+var ab = new ArrayBuffer(8);
+assert.throws(TypeError, function() {
+ sort.call(ab, comparefn);
+}, "this is an ArrayBuffer instance");
+
+var dv = new DataView(new ArrayBuffer(8), 0, 1);
+assert.throws(TypeError, function() {
+ sort.call(dv, comparefn);
+}, "this is a DataView instance");
+
+reportCompare(0, 0);