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

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: Add tests for calling mapfn with/without receiver in/out of sloppy mode Created 6 years, 3 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/runtime.js » ('j') | src/runtime.js » ('J')
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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 if ((end - i) > 0 && ObjectIsFrozen(array)) { 116 if ((end - i) > 0 && ObjectIsFrozen(array)) {
117 throw MakeTypeError("array_functions_on_frozen", 117 throw MakeTypeError("array_functions_on_frozen",
118 ["Array.prototype.fill"]); 118 ["Array.prototype.fill"]);
119 } 119 }
120 120
121 for (; i < end; i++) 121 for (; i < end; i++)
122 array[i] = value; 122 array[i] = value;
123 return array; 123 return array;
124 } 124 }
125 125
126 // ES6, draft 08-24-14, section 22.1.2.1
127 function ArrayFrom(arrayLike, mapfn, receiver) {
128 var C = this;
rossberg 2014/09/10 07:23:55 Nit: drop this redundant variable.
129 var items = ToObject(arrayLike);
130 var mapping;
131
132 if (IS_UNDEFINED(mapfn)) {
rossberg 2014/09/10 07:23:55 var mapping = !IS_UNDEFINED(mapfn) But in fact, i
133 mapping = false;
134 } else {
135 mapping = true;
136 if (!IS_SPEC_FUNCTION(mapfn)) {
137 throw MakeTypeError('called_non_callable', [ mapfn ]);
138 }
139
140 if (IS_NULL_OR_UNDEFINED(receiver)) {
141 receiver = %GetDefaultReceiver(mapfn) || receiver;
142 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) {
143 receiver = ToObject(receiver);
rossberg 2014/09/10 07:23:55 Hm, is this actually necessary? Doesn't _CallFunct
caitp (gmail) 2014/09/10 14:26:04 Looks like Execution::Call() does this, yes --- ed
arv (Not doing code reviews) 2014/09/10 14:33:58 Yes. We don't want _CallFunction to have to do the
144 }
145 }
146
147 var usingIterator = ToIterable(items);
rossberg 2014/09/10 07:23:55 Nit: rename to iterable?
148 var k;
rossberg 2014/09/10 07:23:55 These can all be locally declared. Also, please us
caitp (gmail) 2014/09/10 14:26:04 More descriptive name is good --- I think declarin
149 var A;
150 var mappedValue;
151 var nextValue;
152
153 if (!IS_UNDEFINED(usingIterator)) {
154 // Step 8.
155 A = IS_SPEC_FUNCTION(C) ? new C() : [];
156
157 var iterator = GetIterator(items, usingIterator);
158 var next;
159 k = 0;
160
161 while (!(next = iterator.next()).done) {
rossberg 2014/09/10 07:23:55 I think for (var next = iterator.next(); !next.
arv (Not doing code reviews) 2014/09/10 14:05:14 Using for-of is going to be an observable differen
162 if (!IS_SPEC_OBJECT(next)) {
163 throw MakeTypeError('iterator_result_not_an_object', [next]);
164 }
165 nextValue = next.value;
166 mappedValue = mapping ? %_CallFunction(receiver, nextValue, k, mapfn) : ne xtValue;
rossberg 2014/09/10 07:23:55 Nit: line length
167 %AddElement(A, k++, mappedValue, NONE);
168 }
169 A.length = k;
170 return A;
171 } else {
172 // Steps 9 .. 20
173 var len = TO_LENGTH(items.length);
174 A = IS_SPEC_FUNCTION(C) ? new C(len) : new $Array(len);
175
176 for (k = 0; k < len; ++k) {
177 nextValue = items[k];
178 mappedValue = mapping ? %_CallFunction(receiver, nextValue, k, mapfn) : ne xtValue;
rossberg 2014/09/10 07:23:55 Nit: line length
179 %AddElement(A, k, mappedValue, NONE);
180 }
181
182 A.length = k;
183 return A;
184 }
185 }
186
126 // ES6, draft 05-22-14, section 22.1.2.3 187 // ES6, draft 05-22-14, section 22.1.2.3
127 function ArrayOf() { 188 function ArrayOf() {
128 var length = %_ArgumentsLength(); 189 var length = %_ArgumentsLength();
129 var constructor = this; 190 var constructor = this;
130 // TODO: Implement IsConstructor (ES6 section 7.2.5) 191 // TODO: Implement IsConstructor (ES6 section 7.2.5)
131 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; 192 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : [];
132 for (var i = 0; i < length; i++) { 193 for (var i = 0; i < length; i++) {
133 %AddElement(array, i, %_Arguments(i), NONE); 194 %AddElement(array, i, %_Arguments(i), NONE);
134 } 195 }
135 array.length = length; 196 array.length = length;
136 return array; 197 return array;
137 } 198 }
138 199
139 // ------------------------------------------------------------------- 200 // -------------------------------------------------------------------
140 201
141 function HarmonyArrayExtendArrayPrototype() { 202 function HarmonyArrayExtendArrayPrototype() {
142 %CheckIsBootstrapping(); 203 %CheckIsBootstrapping();
143 204
205 %FunctionSetLength(ArrayFrom, 1);
206
144 // Set up non-enumerable functions on the Array object. 207 // Set up non-enumerable functions on the Array object.
145 InstallFunctions($Array, DONT_ENUM, $Array( 208 InstallFunctions($Array, DONT_ENUM, $Array(
209 "from", ArrayFrom,
146 "of", ArrayOf 210 "of", ArrayOf
147 )); 211 ));
148 212
149 // Set up the non-enumerable functions on the Array prototype object. 213 // Set up the non-enumerable functions on the Array prototype object.
150 InstallFunctions($Array.prototype, DONT_ENUM, $Array( 214 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
151 "find", ArrayFind, 215 "find", ArrayFind,
152 "findIndex", ArrayFindIndex, 216 "findIndex", ArrayFindIndex,
153 "fill", ArrayFill 217 "fill", ArrayFill
154 )); 218 ));
155 } 219 }
156 220
157 HarmonyArrayExtendArrayPrototype(); 221 HarmonyArrayExtendArrayPrototype();
OLDNEW
« no previous file with comments | « no previous file | src/runtime.js » ('j') | src/runtime.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698