Commit 31fb5024 authored by Daniel Ehrenberg's avatar Daniel Ehrenberg Committed by Adam Klein

Implement %TypedArray%.{lastI,i}ndexOf

This patch adds the two TypedArray methods indexOf and lastIndexOf,
which are similar to the methods on Arrays. Tests are ported from
arrays as well.

BUG=v8:3578
LOG=Y
R=arv@chromium.org

Review URL: https://codereview.chromium.org/1141763004

Patch from Daniel Ehrenberg <dehrenberg@chromium.org>.

Cr-Commit-Position: refs/heads/master@{#28495}
parent 540bb0c8
......@@ -12,6 +12,8 @@ var $arraySplice;
var $arrayUnshift;
var $innerArrayForEach;
var $innerArrayEvery;
var $innerArrayIndexOf;
var $innerArrayLastIndexOf;
var $innerArrayReverse;
var $innerArraySort;
......@@ -1332,10 +1334,11 @@ function ArrayMap(f, receiver) {
}
function ArrayIndexOf(element, index) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf");
var length = TO_UINT32(this.length);
// For .indexOf, we don't need to pass in the number of arguments
// at the callsite since ToInteger(undefined) == 0; however, for
// .lastIndexOf, we need to pass it, since the behavior for passing
// undefined is 0 but for not including the argument is length-1.
function InnerArrayIndexOf(element, index, length) {
if (length == 0) return -1;
if (IS_UNDEFINED(index)) {
index = 0;
......@@ -1389,12 +1392,17 @@ function ArrayIndexOf(element, index) {
}
function ArrayLastIndexOf(element, index) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf");
function ArrayIndexOf(element, index) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf");
var length = TO_UINT32(this.length);
return %_CallFunction(this, element, index, length, InnerArrayIndexOf);
}
function InnerArrayLastIndexOf(element, index, length, argumentsLength) {
if (length == 0) return -1;
if (%_ArgumentsLength() < 2) {
if (argumentsLength < 2) {
index = length - 1;
} else {
index = TO_INTEGER(index);
......@@ -1442,6 +1450,15 @@ function ArrayLastIndexOf(element, index) {
}
function ArrayLastIndexOf(element, index) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf");
var length = TO_UINT32(this.length);
return %_CallFunction(this, element, index, length,
%_ArgumentsLength(), InnerArrayLastIndexOf);
}
function ArrayReduce(callback, current) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
......@@ -1621,6 +1638,8 @@ $arrayUnshift = ArrayUnshift;
$innerArrayForEach = InnerArrayForEach;
$innerArrayEvery = InnerArrayEvery;
$innerArrayIndexOf = InnerArrayIndexOf;
$innerArrayLastIndexOf = InnerArrayLastIndexOf;
$innerArrayReverse = InnerArrayReverse;
$innerArraySort = InnerArraySort;
......
......@@ -134,6 +134,29 @@ function TypedArraySort(comparefn) {
}
// ES6 section 22.2.3.13
function TypedArrayIndexOf(element, index) {
if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
var length = %_TypedArrayGetLength(this);
return %_CallFunction(this, element, index, length, $innerArrayIndexOf);
}
%FunctionSetLength(TypedArrayIndexOf, 1);
// ES6 section 22.2.3.16
function TypedArrayLastIndexOf(element, index) {
if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
var length = %_TypedArrayGetLength(this);
return %_CallFunction(this, element, index, length,
%_ArgumentsLength(), $innerArrayLastIndexOf);
}
%FunctionSetLength(TypedArrayLastIndexOf, 1);
// ES6 draft 08-24-14, section 22.2.2.2
function TypedArrayOf() {
var length = %_ArgumentsLength();
......@@ -184,6 +207,8 @@ macro EXTEND_TYPED_ARRAY(NAME)
"find", TypedArrayFind,
"findIndex", TypedArrayFindIndex,
"fill", TypedArrayFill,
"indexOf", TypedArrayIndexOf,
"lastIndexOf", TypedArrayLastIndexOf,
"reverse", TypedArrayReverse,
"sort", TypedArraySort
]);
......
// Copyright 2015 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.
// Flags: --harmony-arrays
var typedArrayConstructors = [
Uint8Array,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
Uint8ClampedArray,
Float32Array,
Float64Array
];
for (var constructor of typedArrayConstructors) {
var array = new constructor([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]);
// ----------------------------------------------------------------------
// %TypedArray%.prototype.indexOf.
// ----------------------------------------------------------------------
// Negative cases.
assertEquals(-1, new constructor([]).indexOf(1));
assertEquals(-1, array.indexOf(4));
assertEquals(-1, array.indexOf(3, array.length));
assertEquals(2, array.indexOf(3));
// Negative index out of range.
assertEquals(0, array.indexOf(1, -17));
// Negative index in rage.
assertEquals(3, array.indexOf(1, -11));
// Index in range.
assertEquals(3, array.indexOf(1, 1));
assertEquals(3, array.indexOf(1, 3));
assertEquals(6, array.indexOf(1, 4));
// Basic TypedArray function properties
assertEquals(1, array.indexOf.length);
assertThrows(function(){ array.indexOf.call([1], 1); }, TypeError);
Object.defineProperty(array, 'length', {value: 1});
assertEquals(array.indexOf(2), 1);
// ----------------------------------------------------------------------
// %TypedArray%.prototype.lastIndexOf.
// ----------------------------------------------------------------------
array = new constructor([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]);
// Negative cases.
assertEquals(-1, new constructor([]).lastIndexOf(1));
assertEquals(-1, array.lastIndexOf(1, -17));
assertEquals(9, array.lastIndexOf(1));
// Index out of range.
assertEquals(9, array.lastIndexOf(1, array.length));
// Index in range.
assertEquals(0, array.lastIndexOf(1, 2));
assertEquals(3, array.lastIndexOf(1, 4));
assertEquals(3, array.lastIndexOf(1, 3));
// Negative index in range.
assertEquals(0, array.lastIndexOf(1, -11));
// Basic TypedArray function properties
assertEquals(1, array.lastIndexOf.length);
assertThrows(function(){ array.lastIndexOf.call([1], 1); }, TypeError);
Object.defineProperty(array, 'length', {value: 1});
assertEquals(array.lastIndexOf(2), 10);
delete array.length;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment