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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 07-18-14, section 22.1.2.1 |
| 127 function ArrayFrom(arrayLike, mapfn, receiver) { |
| 128 var C = this; |
| 129 var items = ToObject(arrayLike); |
| 130 var mapping; |
| 131 |
| 132 if (IS_UNDEFINED(mapfn)) { |
| 133 mapping = false; |
| 134 } else { |
| 135 mapping = true; |
| 136 if (!IS_SPEC_FUNCTION(mapfn)) { |
| 137 throw MakeTypeError('called_non_callable', [ mapfn ]); |
| 138 } |
| 139 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 140 receiver = %GetDefaultReceiver(mapfn) || receiver; |
| 141 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) { |
| 142 receiver = ToObject(receiver); |
| 143 } |
| 144 } |
| 145 |
| 146 var usingIterator = ToIterable(items); |
| 147 var k; |
| 148 var A; |
| 149 var mappedValue; |
| 150 var nextValue; |
| 151 |
| 152 if (!IS_UNDEFINED(usingIterator)) { |
| 153 // Step 8. |
| 154 A = IS_SPEC_FUNCTION(C) ? new C() : []; |
| 155 |
| 156 var iterator = GetIterator(items, usingIterator); |
| 157 var next; |
| 158 k = 0; |
| 159 |
| 160 while (!(next = iterator.next()).done) { |
| 161 if (!IS_SPEC_OBJECT(next)) { |
| 162 throw MakeTypeError('iterator_result_not_an_object', [next]); |
| 163 } |
| 164 nextValue = next.value; |
| 165 mappedValue = mapping ? %_CallFunction(receiver, nextValue, k, mapfn) : ne
xtValue; |
| 166 %AddElement(A, k++, mappedValue, NONE); |
| 167 } |
| 168 A.length = k; |
| 169 return A; |
| 170 } else { |
| 171 // Steps 9 .. 20 |
| 172 var len = TO_LENGTH(items.length); |
| 173 A = IS_SPEC_FUNCTION(C) ? new C() : []; |
| 174 |
| 175 for (k = 0; k < len; ++k) { |
| 176 nextValue = items[k]; |
| 177 mappedValue = mapping ? %_CallFunction(receiver, nextValue, k, mapfn) : ne
xtValue; |
| 178 %AddElement(A, k, mappedValue, NONE); |
| 179 } |
| 180 |
| 181 A.length = k; |
| 182 return A; |
| 183 } |
| 184 } |
| 185 |
126 // ES6, draft 05-22-14, section 22.1.2.3 | 186 // ES6, draft 05-22-14, section 22.1.2.3 |
127 function ArrayOf() { | 187 function ArrayOf() { |
128 var length = %_ArgumentsLength(); | 188 var length = %_ArgumentsLength(); |
129 var constructor = this; | 189 var constructor = this; |
130 // TODO: Implement IsConstructor (ES6 section 7.2.5) | 190 // TODO: Implement IsConstructor (ES6 section 7.2.5) |
131 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; | 191 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; |
132 for (var i = 0; i < length; i++) { | 192 for (var i = 0; i < length; i++) { |
133 %AddElement(array, i, %_Arguments(i), NONE); | 193 %AddElement(array, i, %_Arguments(i), NONE); |
134 } | 194 } |
135 array.length = length; | 195 array.length = length; |
136 return array; | 196 return array; |
137 } | 197 } |
138 | 198 |
139 // ------------------------------------------------------------------- | 199 // ------------------------------------------------------------------- |
140 | 200 |
141 function HarmonyArrayExtendArrayPrototype() { | 201 function HarmonyArrayExtendArrayPrototype() { |
142 %CheckIsBootstrapping(); | 202 %CheckIsBootstrapping(); |
143 | 203 |
| 204 %FunctionSetLength(ArrayFrom, 1); |
| 205 |
144 // Set up non-enumerable functions on the Array object. | 206 // Set up non-enumerable functions on the Array object. |
145 InstallFunctions($Array, DONT_ENUM, $Array( | 207 InstallFunctions($Array, DONT_ENUM, $Array( |
| 208 "from", ArrayFrom, |
146 "of", ArrayOf | 209 "of", ArrayOf |
147 )); | 210 )); |
148 | 211 |
149 // Set up the non-enumerable functions on the Array prototype object. | 212 // Set up the non-enumerable functions on the Array prototype object. |
150 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | 213 InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
151 "find", ArrayFind, | 214 "find", ArrayFind, |
152 "findIndex", ArrayFindIndex, | 215 "findIndex", ArrayFindIndex, |
153 "fill", ArrayFill | 216 "fill", ArrayFill |
154 )); | 217 )); |
155 } | 218 } |
156 | 219 |
157 HarmonyArrayExtendArrayPrototype(); | 220 HarmonyArrayExtendArrayPrototype(); |
OLD | NEW |