Commit 51df8df9 authored by dehrenberg's avatar dehrenberg Committed by Commit bot

Implement %TypedArray%.prototype.slice

The initial implementation of this method takes a couple shortcuts:
- At some points in the spec, we should be making a reference to
  "Table 49" and instead do property lookup on the constructor.
  This is an issue with some other TypedArray methods too.
- The spec indicates that, if constructor and @@species haven't been
  messed with in particular ways, then there should be a memcpy
  from the old to the new. That fast path is not implemented here
  in this patch, but because V8 doesn't canonicalize NaN, the result
  isn't observably different.

BUG=v8:3578
LOG=Y
R=arv

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

Cr-Commit-Position: refs/heads/master@{#28882}
parent eed7363a
......@@ -49,6 +49,8 @@ var InnerArraySome;
var InnerArraySort;
var InnerArrayToLocaleString;
var IsNaN;
var MathMax;
var MathMin;
utils.Import(function(from) {
ArrayFrom = from.ArrayFrom;
......@@ -71,11 +73,13 @@ utils.Import(function(from) {
InnerArraySort = from.InnerArraySort;
InnerArrayToLocaleString = from.InnerArrayToLocaleString;
IsNaN = from.IsNaN;
MathMax = from.MathMax;
MathMin = from.MathMin;
});
// -------------------------------------------------------------------
function ConstructTypedArray(constructor, array) {
function ConstructTypedArray(constructor, arg) {
// TODO(littledan): This is an approximation of the spec, which requires
// that only real TypedArray classes should be accepted (22.2.2.1.1)
if (!%IsConstructor(constructor) || IS_UNDEFINED(constructor.prototype) ||
......@@ -88,13 +92,15 @@ function ConstructTypedArray(constructor, array) {
// underlying size and element size, and elements are put in one by one.
// By contrast, this would allow subclasses to make a radically different
// constructor with different semantics.
return new constructor(array);
return new constructor(arg);
}
function ConstructTypedArrayLike(typedArray, arrayContents) {
function ConstructTypedArrayLike(typedArray, arg) {
// TODO(littledan): The spec requires that we actuallly use
// typedArray.constructor[Symbol.species] (bug v8:4093)
return ConstructTypedArray(typedArray.constructor, arrayContents);
// Also, it should default to the default constructor from
// table 49 if typedArray.constructor doesn't exist.
return ConstructTypedArray(typedArray.constructor, arg);
}
function TypedArrayCopyWithin(target, start, end) {
......@@ -306,6 +312,51 @@ function TypedArrayReduceRight(callback, current) {
%FunctionSetLength(TypedArrayReduceRight, 1);
function TypedArraySlice(start, end) {
if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
var len = %_TypedArrayGetLength(this);
var relativeStart = TO_INTEGER(start);
var k;
if (relativeStart < 0) {
k = MathMax(len + relativeStart, 0);
} else {
k = MathMin(relativeStart, len);
}
var relativeEnd;
if (IS_UNDEFINED(end)) {
relativeEnd = len;
} else {
relativeEnd = TO_INTEGER(end);
}
var final;
if (relativeEnd < 0) {
final = MathMax(len + relativeEnd, 0);
} else {
final = MathMin(relativeEnd, len);
}
var count = MathMax(final - k, 0);
var array = ConstructTypedArrayLike(this, count);
// The code below is the 'then' branch; the 'else' branch species
// a memcpy. Because V8 doesn't canonicalize NaN, the difference is
// unobservable.
var n = 0;
while (k < final) {
var kValue = this[k];
// TODO(littledan): The spec says to throw on an error in setting;
// does this throw?
array[n] = kValue;
k++;
n++;
}
return array;
}
// ES6 draft 08-24-14, section 22.2.2.2
function TypedArrayOf() {
var length = %_ArgumentsLength();
......@@ -349,6 +400,7 @@ macro EXTEND_TYPED_ARRAY(NAME)
"reduce", TypedArrayReduce,
"reduceRight", TypedArrayReduceRight,
"reverse", TypedArrayReverse,
"slice", TypedArraySlice,
"some", TypedArraySome,
"sort", TypedArraySort,
"toString", TypedArrayToString,
......
// 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) {
// Check various variants of empty array's slicing.
var array = new constructor(0);
for (var i = 0; i < 7; i++) {
assertEquals(0, array.slice(0, 0).length);
assertEquals(0, array.slice(1, 0).length);
assertEquals(0, array.slice(0, 1).length);
assertEquals(0, array.slice(-1, 0).length);
}
// Check various forms of arguments omission.
array = new constructor(7);
for (var i = 0; i < 7; i++) {
assertEquals(array, array.slice());
assertEquals(array, array.slice(0));
assertEquals(array, array.slice(undefined));
assertEquals(array, array.slice("foobar"));
assertEquals(array, array.slice(undefined, undefined));
}
// Check variants of negatives and positive indices.
array = new constructor(7);
assertEquals(7, array.slice(-100).length);
assertEquals(3, array.slice(-3).length);
assertEquals(3, array.slice(4).length);
assertEquals(1, array.slice(6).length);
assertEquals(0, array.slice(7).length);
assertEquals(0, array.slice(8).length);
assertEquals(0, array.slice(100).length);
assertEquals(0, array.slice(0, -100).length);
assertEquals(4, array.slice(0, -3).length);
assertEquals(4, array.slice(0, 4).length);
assertEquals(6, array.slice(0, 6).length);
assertEquals(7, array.slice(0, 7).length);
assertEquals(7, array.slice(0, 8).length);
assertEquals(7, array.slice(0, 100).length);
// Does not permit being called on other types
assertThrows(function () {
constructor.prototype.slice.call([], 0, 0);
}, TypeError);
// Check that elements are copied properly in slice
array = new constructor([1, 2, 3, 4]);
var slice = array.slice(1, 3);
assertEquals(2, slice.length);
assertEquals(2, slice[0]);
assertEquals(3, slice[1]);
assertTrue(slice instanceof constructor);
}
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