OLD | NEW |
---|---|
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 Loading... | |
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 08-24-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 // Step 8. | |
rossberg
2014/12/10 13:58:17
Nit: perhaps remove these fine-grained references,
| |
153 result = IS_SPEC_FUNCTION(this) && this.prototype ? new this() : []; | |
154 | |
155 k = 0; | |
156 for (nextValue of items) { | |
157 if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); | |
158 else mappedValue = nextValue; | |
159 %AddElement(result, k++, mappedValue, NONE); | |
160 } | |
161 | |
162 result.length = k; | |
163 return result; | |
164 } else { | |
165 // Steps 9 .. 20 | |
166 var len = ToLength(items.length); | |
167 result = IS_SPEC_FUNCTION(this) && this.prototype ? new this(len) : | |
168 new $Array(len); | |
169 | |
170 for (k = 0; k < len; ++k) { | |
171 nextValue = items[k]; | |
172 if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); | |
173 else mappedValue = nextValue; | |
174 %AddElement(result, k, mappedValue, NONE); | |
175 } | |
176 | |
177 result.length = k; | |
178 return result; | |
179 } | |
180 } | |
181 | |
130 // ES6, draft 05-22-14, section 22.1.2.3 | 182 // ES6, draft 05-22-14, section 22.1.2.3 |
131 function ArrayOf() { | 183 function ArrayOf() { |
132 var length = %_ArgumentsLength(); | 184 var length = %_ArgumentsLength(); |
133 var constructor = this; | 185 var constructor = this; |
134 // TODO: Implement IsConstructor (ES6 section 7.2.5) | 186 // TODO: Implement IsConstructor (ES6 section 7.2.5) |
135 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; | 187 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; |
136 for (var i = 0; i < length; i++) { | 188 for (var i = 0; i < length; i++) { |
137 %AddElement(array, i, %_Arguments(i), NONE); | 189 %AddElement(array, i, %_Arguments(i), NONE); |
138 } | 190 } |
139 array.length = length; | 191 array.length = length; |
140 return array; | 192 return array; |
141 } | 193 } |
142 | 194 |
143 // ------------------------------------------------------------------- | 195 // ------------------------------------------------------------------- |
144 | 196 |
145 function HarmonyArrayExtendArrayPrototype() { | 197 function HarmonyArrayExtendArrayPrototype() { |
146 %CheckIsBootstrapping(); | 198 %CheckIsBootstrapping(); |
147 | 199 |
200 %FunctionSetLength(ArrayFrom, 1); | |
201 | |
148 // Set up non-enumerable functions on the Array object. | 202 // Set up non-enumerable functions on the Array object. |
149 InstallFunctions($Array, DONT_ENUM, $Array( | 203 InstallFunctions($Array, DONT_ENUM, $Array( |
204 "from", ArrayFrom, | |
150 "of", ArrayOf | 205 "of", ArrayOf |
151 )); | 206 )); |
152 | 207 |
153 // Set up the non-enumerable functions on the Array prototype object. | 208 // Set up the non-enumerable functions on the Array prototype object. |
154 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | 209 InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
155 "find", ArrayFind, | 210 "find", ArrayFind, |
156 "findIndex", ArrayFindIndex, | 211 "findIndex", ArrayFindIndex, |
157 "fill", ArrayFill | 212 "fill", ArrayFill |
158 )); | 213 )); |
159 } | 214 } |
160 | 215 |
161 HarmonyArrayExtendArrayPrototype(); | 216 HarmonyArrayExtendArrayPrototype(); |
OLD | NEW |