v8/test/mjsunit/array-reverse.js
Simon Zünd 837fec91a4 [array] Replace JS Array.p.reverse with a Torque implementation
This CL adds a baseline implementation for Array.p.reverse in Torque,
as well as fastpaths for PACKED elements kinds.

Support for sparse JSArrays was removed.

R=jgruber@chromium.org, petermarshall@chromium.org

Bug: v8:7624
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I12900fbbb44746f1c5d36b78be826e14b88b4f69
Reviewed-on: https://chromium-review.googlesource.com/1185600
Commit-Queue: Simon Zünd <szuend@google.com>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55369}
2018-08-24 05:57:20 +00:00

71 lines
2.1 KiB
JavaScript

// Copyright 2018 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.
assertArrayEquals([], [].reverse());
assertArrayEquals([8, 6, 4, 2], [2, 4, 6, 8].reverse());
assertArrayEquals([0.8, 0.6, 0.4], [0.4, 0.6, 0.8].reverse());
assertArrayEquals(["str4", "str3", "str2"], ["str2", "str3", "str4"].reverse());
assertArrayEquals([4,3,,1], [1,,3,4].reverse());
assertArrayEquals([4,,2,1], [1,2,,4].reverse());
assertArrayEquals([5,,3,,1], [1,,3,,5].reverse());
function TestReverseWithObject() {
let obj = { length: 5 };
obj[0] = "foo";
obj[3] = "bar";
Array.prototype.reverse.call(obj);
assertArrayEquals([,"bar",,,"foo"], obj);
}
TestReverseWithObject();
function TestReverseWithPrototypeChain() {
let proto = { 0: "foo", 19: "bar" };
let obj = { length: 20, 5: "foobar", __proto__: proto };
Array.prototype.reverse.call(obj);
assertEquals("bar", obj[0]);
assertEquals("foobar", obj[14]);
assertEquals("foo", obj[19]);
}
TestReverseWithPrototypeChain();
function TestReverseWithTypedArrays() {
const constructors = [
Uint8Array,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
Uint8ClampedArray,
Float32Array,
Float64Array
];
for (const constructor of constructors) {
const array_odd = new constructor([1, 2, 3]);
Array.prototype.reverse.call(array_odd);
assertArrayEquals([3, 2, 1], array_odd, constructor);
const array_even = new constructor([1, 2, 3, 4]);
Array.prototype.reverse.call(array_even);
assertArrayEquals([4, 3, 2, 1], array_even, constructor);
// Array.prototype.reverse respects shadowing length on TypedArrays.
const array = new constructor([1, 2, 3, 4]);
Object.defineProperty(array, 'length', {value: 2});
Array.prototype.reverse.call(array);
assertArrayEquals([2, 1], array, constructor);
const array_shadowed_length = new constructor([1, 2]);
Object.defineProperty(array_shadowed_length, 'length', {value: 5});
assertThrows(() => Array.prototype.reverse.call(array_shadowed_length));
}
}
TestReverseWithTypedArrays();