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 var $innerArrayCopyWithin; | |
6 var $innerArrayFill; | |
7 var $innerArrayFind; | |
8 var $innerArrayFindIndex; | |
9 var $arrayFrom; | |
10 | |
11 (function(global, utils) { | 5 (function(global, utils) { |
12 | 6 |
13 'use strict'; | 7 'use strict'; |
14 | 8 |
15 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
16 | 10 |
17 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
18 // Imports | 12 // Imports |
19 | 13 |
20 var GlobalArray = global.Array; | 14 var GlobalArray = global.Array; |
21 var GlobalSymbol = global.Symbol; | 15 var GlobalSymbol = global.Symbol; |
22 | 16 |
| 17 var GetIterator; |
| 18 var GetMethod; |
23 var MathMax; | 19 var MathMax; |
24 var MathMin; | 20 var MathMin; |
| 21 var ObjectIsFrozen; |
25 | 22 |
26 utils.Import(function(from) { | 23 utils.Import(function(from) { |
| 24 GetIterator = from.GetIterator; |
| 25 GetMethod = from.GetMethod; |
27 MathMax = from.MathMax; | 26 MathMax = from.MathMax; |
28 MathMin = from.MathMin; | 27 MathMin = from.MathMin; |
| 28 ObjectIsFrozen = from.ObjectIsFrozen; |
29 }); | 29 }); |
30 | 30 |
31 // ------------------------------------------------------------------- | 31 // ------------------------------------------------------------------- |
32 | 32 |
33 function InnerArrayCopyWithin(target, start, end, array, length) { | 33 function InnerArrayCopyWithin(target, start, end, array, length) { |
34 target = TO_INTEGER(target); | 34 target = TO_INTEGER(target); |
35 var to; | 35 var to; |
36 if (target < 0) { | 36 if (target < 0) { |
37 to = MathMax(length + target, 0); | 37 to = MathMax(length + target, 0); |
38 } else { | 38 } else { |
(...skipping 30 matching lines...) Expand all Loading... |
69 } else { | 69 } else { |
70 delete array[to]; | 70 delete array[to]; |
71 } | 71 } |
72 from = from + direction; | 72 from = from + direction; |
73 to = to + direction; | 73 to = to + direction; |
74 count--; | 74 count--; |
75 } | 75 } |
76 | 76 |
77 return array; | 77 return array; |
78 } | 78 } |
79 $innerArrayCopyWithin = InnerArrayCopyWithin; | |
80 | 79 |
81 // ES6 draft 03-17-15, section 22.1.3.3 | 80 // ES6 draft 03-17-15, section 22.1.3.3 |
82 function ArrayCopyWithin(target, start, end) { | 81 function ArrayCopyWithin(target, start, end) { |
83 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin"); | 82 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin"); |
84 | 83 |
85 var array = TO_OBJECT_INLINE(this); | 84 var array = TO_OBJECT_INLINE(this); |
86 var length = $toLength(array.length); | 85 var length = $toLength(array.length); |
87 | 86 |
88 return InnerArrayCopyWithin(target, start, end, array, length); | 87 return InnerArrayCopyWithin(target, start, end, array, length); |
89 } | 88 } |
(...skipping 15 matching lines...) Expand all Loading... |
105 var element = array[i]; | 104 var element = array[i]; |
106 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; | 105 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; |
107 if (%_CallFunction(newThisArg, element, i, array, predicate)) { | 106 if (%_CallFunction(newThisArg, element, i, array, predicate)) { |
108 return element; | 107 return element; |
109 } | 108 } |
110 } | 109 } |
111 } | 110 } |
112 | 111 |
113 return; | 112 return; |
114 } | 113 } |
115 $innerArrayFind = InnerArrayFind; | |
116 | 114 |
117 // ES6 draft 07-15-13, section 15.4.3.23 | 115 // ES6 draft 07-15-13, section 15.4.3.23 |
118 function ArrayFind(predicate, thisArg) { | 116 function ArrayFind(predicate, thisArg) { |
119 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); | 117 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); |
120 | 118 |
121 var array = $toObject(this); | 119 var array = $toObject(this); |
122 var length = $toInteger(array.length); | 120 var length = $toInteger(array.length); |
123 | 121 |
124 return InnerArrayFind(predicate, thisArg, array, length); | 122 return InnerArrayFind(predicate, thisArg, array, length); |
125 } | 123 } |
(...skipping 15 matching lines...) Expand all Loading... |
141 var element = array[i]; | 139 var element = array[i]; |
142 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; | 140 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; |
143 if (%_CallFunction(newThisArg, element, i, array, predicate)) { | 141 if (%_CallFunction(newThisArg, element, i, array, predicate)) { |
144 return i; | 142 return i; |
145 } | 143 } |
146 } | 144 } |
147 } | 145 } |
148 | 146 |
149 return -1; | 147 return -1; |
150 } | 148 } |
151 $innerArrayFindIndex = InnerArrayFindIndex; | |
152 | 149 |
153 // ES6 draft 07-15-13, section 15.4.3.24 | 150 // ES6 draft 07-15-13, section 15.4.3.24 |
154 function ArrayFindIndex(predicate, thisArg) { | 151 function ArrayFindIndex(predicate, thisArg) { |
155 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex"); | 152 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex"); |
156 | 153 |
157 var array = $toObject(this); | 154 var array = $toObject(this); |
158 var length = $toInteger(array.length); | 155 var length = $toInteger(array.length); |
159 | 156 |
160 return InnerArrayFindIndex(predicate, thisArg, array, length); | 157 return InnerArrayFindIndex(predicate, thisArg, array, length); |
161 } | 158 } |
(...skipping 10 matching lines...) Expand all Loading... |
172 if (i > length) i = length; | 169 if (i > length) i = length; |
173 } | 170 } |
174 | 171 |
175 if (end < 0) { | 172 if (end < 0) { |
176 end += length; | 173 end += length; |
177 if (end < 0) end = 0; | 174 if (end < 0) end = 0; |
178 } else { | 175 } else { |
179 if (end > length) end = length; | 176 if (end > length) end = length; |
180 } | 177 } |
181 | 178 |
182 if ((end - i) > 0 && $objectIsFrozen(array)) { | 179 if ((end - i) > 0 && ObjectIsFrozen(array)) { |
183 throw MakeTypeError(kArrayFunctionsOnFrozen); | 180 throw MakeTypeError(kArrayFunctionsOnFrozen); |
184 } | 181 } |
185 | 182 |
186 for (; i < end; i++) | 183 for (; i < end; i++) |
187 array[i] = value; | 184 array[i] = value; |
188 return array; | 185 return array; |
189 } | 186 } |
190 $innerArrayFill = InnerArrayFill; | |
191 | 187 |
192 // ES6, draft 04-05-14, section 22.1.3.6 | 188 // ES6, draft 04-05-14, section 22.1.3.6 |
193 function ArrayFill(value, start, end) { | 189 function ArrayFill(value, start, end) { |
194 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill"); | 190 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill"); |
195 | 191 |
196 var array = $toObject(this); | 192 var array = $toObject(this); |
197 var length = TO_UINT32(array.length); | 193 var length = TO_UINT32(array.length); |
198 | 194 |
199 return InnerArrayFill(value, start, end, array, length); | 195 return InnerArrayFill(value, start, end, array, length); |
200 } | 196 } |
201 | 197 |
202 // ES6, draft 10-14-14, section 22.1.2.1 | 198 // ES6, draft 10-14-14, section 22.1.2.1 |
203 function ArrayFrom(arrayLike, mapfn, receiver) { | 199 function ArrayFrom(arrayLike, mapfn, receiver) { |
204 var items = $toObject(arrayLike); | 200 var items = $toObject(arrayLike); |
205 var mapping = !IS_UNDEFINED(mapfn); | 201 var mapping = !IS_UNDEFINED(mapfn); |
206 | 202 |
207 if (mapping) { | 203 if (mapping) { |
208 if (!IS_SPEC_FUNCTION(mapfn)) { | 204 if (!IS_SPEC_FUNCTION(mapfn)) { |
209 throw MakeTypeError(kCalledNonCallable, mapfn); | 205 throw MakeTypeError(kCalledNonCallable, mapfn); |
210 } else if (%IsSloppyModeFunction(mapfn)) { | 206 } else if (%IsSloppyModeFunction(mapfn)) { |
211 if (IS_NULL(receiver)) { | 207 if (IS_NULL(receiver)) { |
212 receiver = UNDEFINED; | 208 receiver = UNDEFINED; |
213 } else if (!IS_UNDEFINED(receiver)) { | 209 } else if (!IS_UNDEFINED(receiver)) { |
214 receiver = TO_OBJECT_INLINE(receiver); | 210 receiver = TO_OBJECT_INLINE(receiver); |
215 } | 211 } |
216 } | 212 } |
217 } | 213 } |
218 | 214 |
219 var iterable = $getMethod(items, symbolIterator); | 215 var iterable = GetMethod(items, symbolIterator); |
220 var k; | 216 var k; |
221 var result; | 217 var result; |
222 var mappedValue; | 218 var mappedValue; |
223 var nextValue; | 219 var nextValue; |
224 | 220 |
225 if (!IS_UNDEFINED(iterable)) { | 221 if (!IS_UNDEFINED(iterable)) { |
226 result = %IsConstructor(this) ? new this() : []; | 222 result = %IsConstructor(this) ? new this() : []; |
227 | 223 |
228 var iterator = $getIterator(items, iterable); | 224 var iterator = GetIterator(items, iterable); |
229 | 225 |
230 k = 0; | 226 k = 0; |
231 while (true) { | 227 while (true) { |
232 var next = iterator.next(); | 228 var next = iterator.next(); |
233 | 229 |
234 if (!IS_OBJECT(next)) { | 230 if (!IS_OBJECT(next)) { |
235 throw MakeTypeError(kIteratorResultNotAnObject, next); | 231 throw MakeTypeError(kIteratorResultNotAnObject, next); |
236 } | 232 } |
237 | 233 |
238 if (next.done) { | 234 if (next.done) { |
(...skipping 20 matching lines...) Expand all Loading... |
259 } else { | 255 } else { |
260 mappedValue = nextValue; | 256 mappedValue = nextValue; |
261 } | 257 } |
262 %AddElement(result, k, mappedValue, NONE); | 258 %AddElement(result, k, mappedValue, NONE); |
263 } | 259 } |
264 | 260 |
265 result.length = k; | 261 result.length = k; |
266 return result; | 262 return result; |
267 } | 263 } |
268 } | 264 } |
269 $arrayFrom = ArrayFrom; | |
270 | 265 |
271 // ES6, draft 05-22-14, section 22.1.2.3 | 266 // ES6, draft 05-22-14, section 22.1.2.3 |
272 function ArrayOf() { | 267 function ArrayOf() { |
273 var length = %_ArgumentsLength(); | 268 var length = %_ArgumentsLength(); |
274 var constructor = this; | 269 var constructor = this; |
275 // TODO: Implement IsConstructor (ES6 section 7.2.5) | 270 // TODO: Implement IsConstructor (ES6 section 7.2.5) |
276 var array = %IsConstructor(constructor) ? new constructor(length) : []; | 271 var array = %IsConstructor(constructor) ? new constructor(length) : []; |
277 for (var i = 0; i < length; i++) { | 272 for (var i = 0; i < length; i++) { |
278 %AddElement(array, i, %_Arguments(i), NONE); | 273 %AddElement(array, i, %_Arguments(i), NONE); |
279 } | 274 } |
280 array.length = length; | 275 array.length = length; |
281 return array; | 276 return array; |
282 } | 277 } |
283 | 278 |
284 // ------------------------------------------------------------------- | 279 // ------------------------------------------------------------------- |
285 | 280 |
286 $installConstants(GlobalSymbol, [ | 281 utils.InstallConstants(GlobalSymbol, [ |
287 // TODO(dslomov, caitp): Move to symbol.js when shipping | 282 // TODO(dslomov, caitp): Move to symbol.js when shipping |
288 "isConcatSpreadable", symbolIsConcatSpreadable | 283 "isConcatSpreadable", symbolIsConcatSpreadable |
289 ]); | 284 ]); |
290 | 285 |
291 %FunctionSetLength(ArrayCopyWithin, 2); | 286 %FunctionSetLength(ArrayCopyWithin, 2); |
292 %FunctionSetLength(ArrayFrom, 1); | 287 %FunctionSetLength(ArrayFrom, 1); |
293 %FunctionSetLength(ArrayFill, 1); | 288 %FunctionSetLength(ArrayFill, 1); |
294 %FunctionSetLength(ArrayFind, 1); | 289 %FunctionSetLength(ArrayFind, 1); |
295 %FunctionSetLength(ArrayFindIndex, 1); | 290 %FunctionSetLength(ArrayFindIndex, 1); |
296 | 291 |
297 // Set up non-enumerable functions on the Array object. | 292 // Set up non-enumerable functions on the Array object. |
298 $installFunctions(GlobalArray, DONT_ENUM, [ | 293 utils.InstallFunctions(GlobalArray, DONT_ENUM, [ |
299 "from", ArrayFrom, | 294 "from", ArrayFrom, |
300 "of", ArrayOf | 295 "of", ArrayOf |
301 ]); | 296 ]); |
302 | 297 |
303 // Set up the non-enumerable functions on the Array prototype object. | 298 // Set up the non-enumerable functions on the Array prototype object. |
304 $installFunctions(GlobalArray.prototype, DONT_ENUM, [ | 299 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [ |
305 "copyWithin", ArrayCopyWithin, | 300 "copyWithin", ArrayCopyWithin, |
306 "find", ArrayFind, | 301 "find", ArrayFind, |
307 "findIndex", ArrayFindIndex, | 302 "findIndex", ArrayFindIndex, |
308 "fill", ArrayFill | 303 "fill", ArrayFill |
309 ]); | 304 ]); |
310 | 305 |
| 306 // ------------------------------------------------------------------- |
| 307 // Exports |
| 308 |
| 309 utils.Export(function(to) { |
| 310 to.ArrayFrom = ArrayFrom; |
| 311 to.InnerArrayCopyWithin = InnerArrayCopyWithin; |
| 312 to.InnerArrayFill = InnerArrayFill; |
| 313 to.InnerArrayFind = InnerArrayFind; |
| 314 to.InnerArrayFindIndex = InnerArrayFindIndex; |
| 315 }); |
| 316 |
311 }) | 317 }) |
OLD | NEW |