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

Unified Diff: src/harmony-array.js

Issue 363833006: Implement Array.from() (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Update expected builtins Created 6 years, 5 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/flag-definitions.h ('k') | 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 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,
« no previous file with comments | « src/flag-definitions.h ('k') | src/runtime.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698