Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(936)

Unified Diff: src/harmony-typedarray.js

Issue 1170023002: Implement %TypedArray%.prototype.slice (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarray-slice.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-typedarray.js
diff --git a/src/harmony-typedarray.js b/src/harmony-typedarray.js
index 3968aec84674c57ec46af3388b3fb0109cb2598f..7d9006fb9ed03c47df9ecb624fa2d325855e9e14 100644
--- a/src/harmony-typedarray.js
+++ b/src/harmony-typedarray.js
@@ -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?
arv (Not doing code reviews) 2015/06/08 23:57:12 If you make this function strict you should get th
+ 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,
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarray-slice.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698