Index: src/harmony-array.js |
diff --git a/src/harmony-array.js b/src/harmony-array.js |
index dbcb292a0876842e8d3a6d4c0d21d997cdf6d977..0e943256b35308a4e7fb85e733b43b86910ccb62 100644 |
--- a/src/harmony-array.js |
+++ b/src/harmony-array.js |
@@ -123,11 +123,83 @@ function ArrayFill(value /* [, start [, end ] ] */) { // length == 1 |
return array; |
} |
+// ES6, 22.1.2.1 --- Requires harmony_symbols and harmony_iteration |
arv (Not doing code reviews)
2014/07/16 20:31:48
Why does this depend in harmony_iteration?
caitp (gmail)
2014/07/19 18:29:06
There are two cases in the Array.from spec: 1) ite
arv (Not doing code reviews)
2014/07/21 18:00:33
I can see why the test depends on harmony_iteratio
|
+function ArrayFrom(arrayLike, mapfn, receiver) { |
+ var constructor = $Array; |
+ var items = ToObject(arrayLike); |
+ var mapping = false; |
+ var array; |
+ |
+ // FIXME: Implement IsConstructor (ES6 section 7.2.5) instead of IS_SPEC_FUNCTION |
+ if (IS_SPEC_FUNCTION(this)) { |
+ constructor = this; |
+ } |
+ |
+ if (!IS_UNDEFINED(mapfn)) { |
+ if (!IS_SPEC_FUNCTION(mapfn)) { |
+ throw MakeTypeError('called_non_callable', [ mapfn ]); |
+ } |
+ if (IS_NULL_OR_UNDEFINED(receiver)) { |
arv (Not doing code reviews)
2014/07/16 20:31:48
Why is null included here? I thought this was part
caitp (gmail)
2014/07/19 18:29:06
The pattern was just taken from one of the ES5 rou
|
+ receiver = %GetDefaultReceiver(mapfn) || receiver; |
+ } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) { |
+ receiver = ToObject(receiver); |
+ } |
+ mapping = true; |
+ } |
+ |
+ var usingIterator = items[symbolIterator]; |
+ var k = 0; |
+ var value; |
+ |
+ if (!IS_UNDEFINED(usingIterator)) { |
+ array = new constructor(); |
+ |
+ if (!IS_SPEC_FUNCTION(usingIterator)) { |
+ throw MakeTypeError('called_non_callable', [ usingIterator ]); |
+ } |
+ |
+ var iterator = %_CallFunction(items, usingIterator); |
+ if (!IS_OBJECT(iterator)) { |
+ throw MakeTypeError('iterator_result_not_an_object', [ iterator ]); |
+ } |
+ |
+ var next; |
+ while (!(next = iterator.next()).done) { |
+ value = next.value; |
+ if (mapping) { |
+ value = %_CallFunction(receiver, value, k, mapfn); |
+ } |
+ array[k++] = value; |
+ } |
+ } else { |
+ var len = TO_LENGTH(items.length); |
+ |
+ array = new constructor(len); |
+ for (; k < len; ++k) { |
+ value = items[k]; |
+ if (mapping) { |
+ value = %_CallFunction(receiver, value, k, mapfn); |
+ } |
+ array[k] = value; |
+ } |
+ } |
+ |
+ array.length = k; |
+ return array; |
+} |
+ |
// ------------------------------------------------------------------- |
function HarmonyArrayExtendArrayPrototype() { |
%CheckIsBootstrapping(); |
+ %FunctionSetLength(ArrayFrom, 1); |
+ |
+ // Set up non-enumerable functions on the Array object. |
+ InstallFunctions($Array, DONT_ENUM, $Array( |
+ "from", ArrayFrom |
+ )); |
+ |
// Set up the non-enumerable functions on the Array prototype object. |
InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
"find", ArrayFind, |