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

Unified Diff: src/harmony-array.js

Issue 363833006: Implement Array.from() (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rebase it Created 6 years 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 | test/mjsunit/harmony/array-from.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 06fada7581db15aa7f576f9f9c615d8e1053e66c..f3d6801e9c32fc07d4872178cf3187521fa11ea5 100644
--- a/src/harmony-array.js
+++ b/src/harmony-array.js
@@ -127,6 +127,56 @@ function ArrayFill(value /* [, start [, end ] ] */) { // length == 1
return array;
}
+// ES6, draft 10-14-14, section 22.1.2.1
+function ArrayFrom(arrayLike, mapfn, receiver) {
+ var items = ToObject(arrayLike);
+ var mapping = !IS_UNDEFINED(mapfn);
+
+ if (mapping) {
+ if (!IS_SPEC_FUNCTION(mapfn)) {
+ throw MakeTypeError('called_non_callable', [ mapfn ]);
+ } else if (IS_NULL_OR_UNDEFINED(receiver)) {
+ receiver = %GetDefaultReceiver(mapfn) || receiver;
+ } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) {
+ receiver = ToObject(receiver);
+ }
+ }
+
+ var iterable = ToIterable(items);
+ var k;
+ var result;
+ var mappedValue;
+ var nextValue;
+
+ if (!IS_UNDEFINED(iterable)) {
+ result = IS_SPEC_FUNCTION(this) && this.prototype ? new this() : [];
+
+ k = 0;
+ for (nextValue of items) {
+ if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
+ else mappedValue = nextValue;
+ %AddElement(result, k++, mappedValue, NONE);
+ }
+
+ result.length = k;
+ return result;
+ } else {
+ var len = ToLength(items.length);
+ result = IS_SPEC_FUNCTION(this) && this.prototype ? new this(len) :
+ new $Array(len);
+
+ for (k = 0; k < len; ++k) {
+ nextValue = items[k];
+ if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
+ else mappedValue = nextValue;
+ %AddElement(result, k, mappedValue, NONE);
+ }
+
+ result.length = k;
+ return result;
+ }
+}
+
// ES6, draft 05-22-14, section 22.1.2.3
function ArrayOf() {
var length = %_ArgumentsLength();
@@ -145,8 +195,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 | test/mjsunit/harmony/array-from.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698