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

Unified Diff: src/typedarray.js

Issue 14740017: Implement TypedArray.subarray method. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 8 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/typedarrays.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/typedarray.js
diff --git a/src/typedarray.js b/src/typedarray.js
index daade1328f22f6ae913285e823e0e3c7078cde53..1040f86b236675f29b49d8f9d9017c71b8fa78a5 100644
--- a/src/typedarray.js
+++ b/src/typedarray.js
@@ -167,6 +167,33 @@ function TypedArrayGetLength() {
return %TypedArrayGetLength(this);
}
+function CreateSubArray(elementSize, constructor) {
+ return function(begin, end) {
+ var srcLength = %TypedArrayGetLength(this);
+ var beginInt = TO_INTEGER(begin);
+ if (beginInt < 0) {
+ beginInt = MathMax(0, srcLength + beginInt);
+ } else {
+ beginInt = MathMin(srcLength, beginInt);
+ }
+
+ var endInt = IS_UNDEFINED(end) ? srcLength : TO_INTEGER(end);
+ if (endInt < 0) {
+ endInt = MathMax(0, srcLength + endInt);
+ } else {
+ endInt = MathMin(endInt, srcLength);
+ }
+ if (endInt < beginInt) {
+ endInt = beginInt;
+ }
+ var newLength = endInt - beginInt;
+ var beginByteOffset =
+ %TypedArrayGetByteOffset(this) + beginInt * elementSize;
+ return new constructor(%TypedArrayGetBuffer(this),
+ beginByteOffset, newLength);
+ }
+}
+
// -------------------------------------------------------------------
@@ -205,6 +232,10 @@ function SetupTypedArray(arrayId, name, constructor, elementSize) {
InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset);
InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength);
InstallGetter(constructor.prototype, "length", TypedArrayGetLength);
+
+ InstallFunctions(constructor.prototype, DONT_ENUM, $Array(
+ "subarray", CreateSubArray(elementSize, constructor)
+ ));
}
// arrayIds below should be synchronized with Runtime_TypedArrayInitialize.
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarrays.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698