Index: src/harmony-array.js |
diff --git a/src/harmony-array.js b/src/harmony-array.js |
index 88b878f0a76ef594f64f7905d61459c05749724b..6dc8337dccf419f53c41826de0ccb44534bbafa6 100644 |
--- a/src/harmony-array.js |
+++ b/src/harmony-array.js |
@@ -123,6 +123,66 @@ function ArrayFill(value /* [, start [, end ] ] */) { // length == 1 |
return array; |
} |
+// ES6, draft 07-18-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) || receiver; |
+ } 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() : []; |
+ |
+ 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 +201,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 |
)); |