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

Unified Diff: src/harmony-array.js

Issue 363833006: Implement Array.from() (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address comments, rebased Created 6 years, 3 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 | src/runtime.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-array.js
diff --git a/src/harmony-array.js b/src/harmony-array.js
index 88b878f0a76ef594f64f7905d61459c05749724b..09f43f44921f85b0d2cd90f201c2d8f17bec371d 100644
--- a/src/harmony-array.js
+++ b/src/harmony-array.js
@@ -123,6 +123,67 @@ function ArrayFill(value /* [, start [, end ] ] */) { // length == 1
return array;
}
+// ES6, draft 08-24-14, section 22.1.2.1
+function ArrayFrom(arrayLike, mapfn, receiver) {
+ var C = this;
+ var items = ToObject(arrayLike);
+ var mapping;
+
+ if (IS_UNDEFINED(mapfn)) {
+ mapping = false;
+ } else {
+ mapping = true;
+ if (!IS_SPEC_FUNCTION(mapfn)) {
+ throw MakeTypeError('called_non_callable', [ mapfn ]);
+ }
+
+ if (IS_NULL_OR_UNDEFINED(receiver)) {
+ receiver = %GetDefaultReceiver(mapfn) || undefined;
arv (Not doing code reviews) 2014/09/05 19:17:39 Can you add tests for this too?
+ } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) {
+ receiver = ToObject(receiver);
+ }
+ }
+
+ var usingIterator = ToIterable(items);
+ var k;
+ var A;
+ var mappedValue;
+ var nextValue;
+
+ if (!IS_UNDEFINED(usingIterator)) {
+ // Step 8.
+ A = IS_SPEC_FUNCTION(C) ? new C() : [];
+
+ var iterator = GetIterator(items, usingIterator);
+ var next;
+ k = 0;
+
+ while (!(next = iterator.next()).done) {
+ if (!IS_SPEC_OBJECT(next)) {
+ throw MakeTypeError('iterator_result_not_an_object', [next]);
+ }
+ nextValue = next.value;
+ mappedValue = mapping ? %_CallFunction(receiver, nextValue, k, mapfn) : nextValue;
+ %AddElement(A, k++, mappedValue, NONE);
+ }
+ A.length = k;
+ return A;
+ } else {
+ // Steps 9 .. 20
+ var len = TO_LENGTH(items.length);
+ A = IS_SPEC_FUNCTION(C) ? new C(len) : new $Array(len);
+
+ for (k = 0; k < len; ++k) {
+ nextValue = items[k];
+ mappedValue = mapping ? %_CallFunction(receiver, nextValue, k, mapfn) : nextValue;
+ %AddElement(A, k, mappedValue, NONE);
+ }
+
+ A.length = k;
+ return A;
+ }
+}
+
// ES6, draft 05-22-14, section 22.1.2.3
function ArrayOf() {
var length = %_ArgumentsLength();
@@ -141,8 +202,11 @@ function ArrayOf() {
function HarmonyArrayExtendArrayPrototype() {
%CheckIsBootstrapping();
+ %FunctionSetLength(ArrayFrom, 1);
+
// Set up non-enumerable functions on the Array object.
InstallFunctions($Array, DONT_ENUM, $Array(
+ "from", ArrayFrom,
"of", ArrayOf
));
« no previous file with comments | « no previous file | src/runtime.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698