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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/harmony/array-from.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 // This file relies on the fact that the following declaration has been made 7 // This file relies on the fact that the following declaration has been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Array = global.Array; 9 // var $Array = global.Array;
10 10
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 if ((end - i) > 0 && ObjectIsFrozen(array)) { 120 if ((end - i) > 0 && ObjectIsFrozen(array)) {
121 throw MakeTypeError("array_functions_on_frozen", 121 throw MakeTypeError("array_functions_on_frozen",
122 ["Array.prototype.fill"]); 122 ["Array.prototype.fill"]);
123 } 123 }
124 124
125 for (; i < end; i++) 125 for (; i < end; i++)
126 array[i] = value; 126 array[i] = value;
127 return array; 127 return array;
128 } 128 }
129 129
130 // ES6, draft 10-14-14, section 22.1.2.1
131 function ArrayFrom(arrayLike, mapfn, receiver) {
132 var items = ToObject(arrayLike);
133 var mapping = !IS_UNDEFINED(mapfn);
134
135 if (mapping) {
136 if (!IS_SPEC_FUNCTION(mapfn)) {
137 throw MakeTypeError('called_non_callable', [ mapfn ]);
138 } else if (IS_NULL_OR_UNDEFINED(receiver)) {
139 receiver = %GetDefaultReceiver(mapfn) || receiver;
140 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) {
141 receiver = ToObject(receiver);
142 }
143 }
144
145 var iterable = ToIterable(items);
146 var k;
147 var result;
148 var mappedValue;
149 var nextValue;
150
151 if (!IS_UNDEFINED(iterable)) {
152 result = IS_SPEC_FUNCTION(this) && this.prototype ? new this() : [];
153
154 k = 0;
155 for (nextValue of items) {
156 if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
157 else mappedValue = nextValue;
158 %AddElement(result, k++, mappedValue, NONE);
159 }
160
161 result.length = k;
162 return result;
163 } else {
164 var len = ToLength(items.length);
165 result = IS_SPEC_FUNCTION(this) && this.prototype ? new this(len) :
166 new $Array(len);
167
168 for (k = 0; k < len; ++k) {
169 nextValue = items[k];
170 if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
171 else mappedValue = nextValue;
172 %AddElement(result, k, mappedValue, NONE);
173 }
174
175 result.length = k;
176 return result;
177 }
178 }
179
130 // ES6, draft 05-22-14, section 22.1.2.3 180 // ES6, draft 05-22-14, section 22.1.2.3
131 function ArrayOf() { 181 function ArrayOf() {
132 var length = %_ArgumentsLength(); 182 var length = %_ArgumentsLength();
133 var constructor = this; 183 var constructor = this;
134 // TODO: Implement IsConstructor (ES6 section 7.2.5) 184 // TODO: Implement IsConstructor (ES6 section 7.2.5)
135 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; 185 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : [];
136 for (var i = 0; i < length; i++) { 186 for (var i = 0; i < length; i++) {
137 %AddElement(array, i, %_Arguments(i), NONE); 187 %AddElement(array, i, %_Arguments(i), NONE);
138 } 188 }
139 array.length = length; 189 array.length = length;
140 return array; 190 return array;
141 } 191 }
142 192
143 // ------------------------------------------------------------------- 193 // -------------------------------------------------------------------
144 194
145 function HarmonyArrayExtendArrayPrototype() { 195 function HarmonyArrayExtendArrayPrototype() {
146 %CheckIsBootstrapping(); 196 %CheckIsBootstrapping();
147 197
198 %FunctionSetLength(ArrayFrom, 1);
199
148 // Set up non-enumerable functions on the Array object. 200 // Set up non-enumerable functions on the Array object.
149 InstallFunctions($Array, DONT_ENUM, $Array( 201 InstallFunctions($Array, DONT_ENUM, $Array(
202 "from", ArrayFrom,
150 "of", ArrayOf 203 "of", ArrayOf
151 )); 204 ));
152 205
153 // Set up the non-enumerable functions on the Array prototype object. 206 // Set up the non-enumerable functions on the Array prototype object.
154 InstallFunctions($Array.prototype, DONT_ENUM, $Array( 207 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
155 "find", ArrayFind, 208 "find", ArrayFind,
156 "findIndex", ArrayFindIndex, 209 "findIndex", ArrayFindIndex,
157 "fill", ArrayFill 210 "fill", ArrayFill
158 )); 211 ));
159 } 212 }
160 213
161 HarmonyArrayExtendArrayPrototype(); 214 HarmonyArrayExtendArrayPrototype();
OLDNEW
« 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