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

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: 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/flag-definitions.h ('k') | src/runtime.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 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, 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
127 function ArrayFrom(arrayLike, mapfn, receiver) {
128 var constructor = $Array;
129 var items = ToObject(arrayLike);
130 var mapping = false;
131 var array;
132
133 // FIXME: Implement IsConstructor (ES6 section 7.2.5) instead of IS_SPEC_FUNCT ION
134 if (IS_SPEC_FUNCTION(this)) {
135 constructor = this;
136 }
137
138 if (!IS_UNDEFINED(mapfn)) {
139 if (!IS_SPEC_FUNCTION(mapfn)) {
140 throw MakeTypeError('called_non_callable', [ mapfn ]);
141 }
142 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
143 receiver = %GetDefaultReceiver(mapfn) || receiver;
144 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) {
145 receiver = ToObject(receiver);
146 }
147 mapping = true;
148 }
149
150 var usingIterator = items[symbolIterator];
151 var k = 0;
152 var value;
153
154 if (!IS_UNDEFINED(usingIterator)) {
155 array = new constructor();
156
157 if (!IS_SPEC_FUNCTION(usingIterator)) {
158 throw MakeTypeError('called_non_callable', [ usingIterator ]);
159 }
160
161 var iterator = %_CallFunction(items, usingIterator);
162 if (!IS_OBJECT(iterator)) {
163 throw MakeTypeError('iterator_result_not_an_object', [ iterator ]);
164 }
165
166 var next;
167 while (!(next = iterator.next()).done) {
168 value = next.value;
169 if (mapping) {
170 value = %_CallFunction(receiver, value, k, mapfn);
171 }
172 array[k++] = value;
173 }
174 } else {
175 var len = TO_LENGTH(items.length);
176
177 array = new constructor(len);
178 for (; k < len; ++k) {
179 value = items[k];
180 if (mapping) {
181 value = %_CallFunction(receiver, value, k, mapfn);
182 }
183 array[k] = value;
184 }
185 }
186
187 array.length = k;
188 return array;
189 }
190
126 // ------------------------------------------------------------------- 191 // -------------------------------------------------------------------
127 192
128 function HarmonyArrayExtendArrayPrototype() { 193 function HarmonyArrayExtendArrayPrototype() {
129 %CheckIsBootstrapping(); 194 %CheckIsBootstrapping();
130 195
196 %FunctionSetLength(ArrayFrom, 1);
197
198 // Set up non-enumerable functions on the Array object.
199 InstallFunctions($Array, DONT_ENUM, $Array(
200 "from", ArrayFrom
201 ));
202
131 // Set up the non-enumerable functions on the Array prototype object. 203 // Set up the non-enumerable functions on the Array prototype object.
132 InstallFunctions($Array.prototype, DONT_ENUM, $Array( 204 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
133 "find", ArrayFind, 205 "find", ArrayFind,
134 "findIndex", ArrayFindIndex, 206 "findIndex", ArrayFindIndex,
135 "fill", ArrayFill 207 "fill", ArrayFill
136 )); 208 ));
137 } 209 }
138 210
139 HarmonyArrayExtendArrayPrototype(); 211 HarmonyArrayExtendArrayPrototype();
OLDNEW
« 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