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

Unified Diff: src/harmony-typedarray.js

Issue 1132163011: Add TypedArray.from method (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: improving style Created 5 years, 7 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 | « src/harmony-array.js ('k') | test/mjsunit/harmony/typedarray-from.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 3c4c4a40fedd69f97bc46813fd624edcf3f93f7b..90679e0c1fe1afffb6b6b7115e93719e02d7b0c1 100644
--- a/src/harmony-typedarray.js
+++ b/src/harmony-typedarray.js
@@ -26,6 +26,7 @@ var GlobalNAME = global.NAME;
endmacro
TYPED_ARRAYS(DECLARE_GLOBALS)
+DECLARE_GLOBALS(Array)
// -------------------------------------------------------------------
@@ -34,7 +35,7 @@ function TypedArrayCopyWithin(target, start, end) {
var length = %_TypedArrayGetLength(this);
- // TODO(dehrenberg): Replace with a memcpy for better performance
+ // TODO(littledan): Replace with a memcpy for better performance
return $innerArrayCopyWithin(target, start, end, this, length);
}
%FunctionSetLength(TypedArrayCopyWithin, 2);
@@ -100,9 +101,35 @@ function TypedArrayOf() {
return array;
}
+function ConstructTypedArray(constructor, array) {
+ // 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 (!IS_SPEC_OBJECT(constructor) || IS_UNDEFINED(constructor.prototype) ||
+ !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) {
+ throw MakeTypeError(kNotTypedArray);
+ }
+
+ // TODO(littledan): The spec requires that, rather than directly calling
+ // the constructor, a TypedArray is created with the proper proto and
+ // 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);
+}
+
+function TypedArrayFrom(source, mapfn, thisArg) {
+ // TODO(littledan): Investigate if there is a receiver which could be
+ // faster to accumulate on than Array, e.g., a TypedVector.
+ var array = %_CallFunction(GlobalArray, source, mapfn, thisArg, $arrayFrom);
+ return ConstructTypedArray(this, array);
+}
+%FunctionSetLength(TypedArrayFrom, 1);
+
+// TODO(littledan): Fix the TypedArray proto chain (bug v8:4085).
macro EXTEND_TYPED_ARRAY(NAME)
// Set up non-enumerable functions on the object.
$installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [
+ "from", TypedArrayFrom,
"of", TypedArrayOf
]);
« no previous file with comments | « src/harmony-array.js ('k') | test/mjsunit/harmony/typedarray-from.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698