v8/test/mjsunit/harmony/array-sort-comparefn.js
Mathias Bynens 35b6aa3849 [js] Remove CHECK_OBJECT_COERCIBLE for Array methods
The spec got rid of `CheckObjectCoercible` a while back, and so should
we. This change is not observable in most of the affected cases since
`ToObject` is up near the top of most Array method algorithms. An
example of an observable effect of this change occurs for the following
input:

    Array.prototype.sort.call(null, 1);

Behavior before applying the patch (incorrect message):

    TypeError: Array.prototype.sort called on null or undefined

Expected behavior:

    TypeError: The comparison function must be either a function or
               undefined

This patch removes `CheckObjectCoercible` and adds tests to ensure the
few observable cases are addressed correctly.

The patch also adds a missing `ToObject(this)` to
`Array.prototype.lastIndexOf` which would otherwise become observable
as a result of `CheckObjectCoercible` being removed.

BUG=v8:3577,v8:6921

Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel
Change-Id: Ia086095076c4bf4d8d58dab26bc28df02994ed01
Reviewed-on: https://chromium-review.googlesource.com/718577
Reviewed-by: Adam Klein <adamk@chromium.org>
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Commit-Queue: Mathias Bynens <mathias@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48800}
2017-10-20 19:29:36 +00:00

49 lines
1.5 KiB
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Array.prototype.sort + TypedArray.prototype.sort: comparefn must be either a
// function or undefined.
// https://github.com/tc39/ecma262/pull/785
const types = [
Array,
Int8Array, Uint8Array,
Int16Array, Uint16Array,
Int32Array, Uint32Array,
Uint8ClampedArray,
Float32Array, Float64Array,
];
for (const type of types) {
const array = new type();
array[0] = 1;
array[1] = 2;
array[2] = 3;
array.sort();
array.sort(undefined);
array.sort(() => {});
assertThrows(() => { array.sort(null); }, TypeError);
assertThrows(() => { array.sort(true); }, TypeError);
assertThrows(() => { array.sort(false); }, TypeError);
assertThrows(() => { array.sort(''); }, TypeError);
assertThrows(() => { array.sort(0); }, TypeError);
assertThrows(() => { array.sort(42); }, TypeError);
assertThrows(() => { array.sort([]); }, TypeError);
assertThrows(() => { array.sort(/./); }, TypeError);
assertThrows(() => { array.sort({}); }, TypeError);
assertThrows(() => { array.sort(Symbol()); }, TypeError);
}
assertThrows(() => { Array.prototype.sort.call(null, 42); }, TypeError);
try {
Array.prototype.sort.call(null, 42);
} catch (exception) {
assertEquals(
'The comparison function must be either a function or undefined',
exception.message
);
}