OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 (function(global, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 var InnerArrayForEach; | 42 var InnerArrayForEach; |
43 var InnerArrayIndexOf; | 43 var InnerArrayIndexOf; |
44 var InnerArrayJoin; | 44 var InnerArrayJoin; |
45 var InnerArrayLastIndexOf; | 45 var InnerArrayLastIndexOf; |
46 var InnerArrayMap; | 46 var InnerArrayMap; |
47 var InnerArrayReverse; | 47 var InnerArrayReverse; |
48 var InnerArraySome; | 48 var InnerArraySome; |
49 var InnerArraySort; | 49 var InnerArraySort; |
50 var InnerArrayToLocaleString; | 50 var InnerArrayToLocaleString; |
51 var IsNaN; | 51 var IsNaN; |
52 var MathMax; | |
53 var MathMin; | |
52 | 54 |
53 utils.Import(function(from) { | 55 utils.Import(function(from) { |
54 ArrayFrom = from.ArrayFrom; | 56 ArrayFrom = from.ArrayFrom; |
55 ArrayToString = from.ArrayToString; | 57 ArrayToString = from.ArrayToString; |
56 InnerArrayCopyWithin = from.InnerArrayCopyWithin; | 58 InnerArrayCopyWithin = from.InnerArrayCopyWithin; |
57 InnerArrayEvery = from.InnerArrayEvery; | 59 InnerArrayEvery = from.InnerArrayEvery; |
58 InnerArrayFill = from.InnerArrayFill; | 60 InnerArrayFill = from.InnerArrayFill; |
59 InnerArrayFilter = from.InnerArrayFilter; | 61 InnerArrayFilter = from.InnerArrayFilter; |
60 InnerArrayFind = from.InnerArrayFind; | 62 InnerArrayFind = from.InnerArrayFind; |
61 InnerArrayFindIndex = from.InnerArrayFindIndex; | 63 InnerArrayFindIndex = from.InnerArrayFindIndex; |
62 InnerArrayForEach = from.InnerArrayForEach; | 64 InnerArrayForEach = from.InnerArrayForEach; |
63 InnerArrayIndexOf = from.InnerArrayIndexOf; | 65 InnerArrayIndexOf = from.InnerArrayIndexOf; |
64 InnerArrayJoin = from.InnerArrayJoin; | 66 InnerArrayJoin = from.InnerArrayJoin; |
65 InnerArrayLastIndexOf = from.InnerArrayLastIndexOf; | 67 InnerArrayLastIndexOf = from.InnerArrayLastIndexOf; |
66 InnerArrayMap = from.InnerArrayMap; | 68 InnerArrayMap = from.InnerArrayMap; |
67 InnerArrayReduce = from.InnerArrayReduce; | 69 InnerArrayReduce = from.InnerArrayReduce; |
68 InnerArrayReduceRight = from.InnerArrayReduceRight; | 70 InnerArrayReduceRight = from.InnerArrayReduceRight; |
69 InnerArrayReverse = from.InnerArrayReverse; | 71 InnerArrayReverse = from.InnerArrayReverse; |
70 InnerArraySome = from.InnerArraySome; | 72 InnerArraySome = from.InnerArraySome; |
71 InnerArraySort = from.InnerArraySort; | 73 InnerArraySort = from.InnerArraySort; |
72 InnerArrayToLocaleString = from.InnerArrayToLocaleString; | 74 InnerArrayToLocaleString = from.InnerArrayToLocaleString; |
73 IsNaN = from.IsNaN; | 75 IsNaN = from.IsNaN; |
76 MathMax = from.MathMax; | |
77 MathMin = from.MathMin; | |
74 }); | 78 }); |
75 | 79 |
76 // ------------------------------------------------------------------- | 80 // ------------------------------------------------------------------- |
77 | 81 |
78 function ConstructTypedArray(constructor, array) { | 82 function ConstructTypedArray(constructor, arg) { |
79 // TODO(littledan): This is an approximation of the spec, which requires | 83 // TODO(littledan): This is an approximation of the spec, which requires |
80 // that only real TypedArray classes should be accepted (22.2.2.1.1) | 84 // that only real TypedArray classes should be accepted (22.2.2.1.1) |
81 if (!%IsConstructor(constructor) || IS_UNDEFINED(constructor.prototype) || | 85 if (!%IsConstructor(constructor) || IS_UNDEFINED(constructor.prototype) || |
82 !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) { | 86 !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) { |
83 throw MakeTypeError(kNotTypedArray); | 87 throw MakeTypeError(kNotTypedArray); |
84 } | 88 } |
85 | 89 |
86 // TODO(littledan): The spec requires that, rather than directly calling | 90 // TODO(littledan): The spec requires that, rather than directly calling |
87 // the constructor, a TypedArray is created with the proper proto and | 91 // the constructor, a TypedArray is created with the proper proto and |
88 // underlying size and element size, and elements are put in one by one. | 92 // underlying size and element size, and elements are put in one by one. |
89 // By contrast, this would allow subclasses to make a radically different | 93 // By contrast, this would allow subclasses to make a radically different |
90 // constructor with different semantics. | 94 // constructor with different semantics. |
91 return new constructor(array); | 95 return new constructor(arg); |
92 } | 96 } |
93 | 97 |
94 function ConstructTypedArrayLike(typedArray, arrayContents) { | 98 function ConstructTypedArrayLike(typedArray, arg) { |
95 // TODO(littledan): The spec requires that we actuallly use | 99 // TODO(littledan): The spec requires that we actuallly use |
96 // typedArray.constructor[Symbol.species] (bug v8:4093) | 100 // typedArray.constructor[Symbol.species] (bug v8:4093) |
97 return ConstructTypedArray(typedArray.constructor, arrayContents); | 101 // Also, it should default to the default constructor from |
102 // table 49 if typedArray.constructor doesn't exist. | |
103 return ConstructTypedArray(typedArray.constructor, arg); | |
98 } | 104 } |
99 | 105 |
100 function TypedArrayCopyWithin(target, start, end) { | 106 function TypedArrayCopyWithin(target, start, end) { |
101 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 107 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
102 | 108 |
103 var length = %_TypedArrayGetLength(this); | 109 var length = %_TypedArrayGetLength(this); |
104 | 110 |
105 // TODO(littledan): Replace with a memcpy for better performance | 111 // TODO(littledan): Replace with a memcpy for better performance |
106 return InnerArrayCopyWithin(target, start, end, this, length); | 112 return InnerArrayCopyWithin(target, start, end, this, length); |
107 } | 113 } |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 function TypedArrayReduceRight(callback, current) { | 305 function TypedArrayReduceRight(callback, current) { |
300 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 306 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
301 | 307 |
302 var length = %_TypedArrayGetLength(this); | 308 var length = %_TypedArrayGetLength(this); |
303 return InnerArrayReduceRight(callback, current, this, length, | 309 return InnerArrayReduceRight(callback, current, this, length, |
304 %_ArgumentsLength()); | 310 %_ArgumentsLength()); |
305 } | 311 } |
306 %FunctionSetLength(TypedArrayReduceRight, 1); | 312 %FunctionSetLength(TypedArrayReduceRight, 1); |
307 | 313 |
308 | 314 |
315 function TypedArraySlice(start, end) { | |
316 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | |
317 var len = %_TypedArrayGetLength(this); | |
318 | |
319 var relativeStart = TO_INTEGER(start); | |
320 | |
321 var k; | |
322 if (relativeStart < 0) { | |
323 k = MathMax(len + relativeStart, 0); | |
324 } else { | |
325 k = MathMin(relativeStart, len); | |
326 } | |
327 | |
328 var relativeEnd; | |
329 if (IS_UNDEFINED(end)) { | |
330 relativeEnd = len; | |
331 } else { | |
332 relativeEnd = TO_INTEGER(end); | |
333 } | |
334 | |
335 var final; | |
336 if (relativeEnd < 0) { | |
337 final = MathMax(len + relativeEnd, 0); | |
338 } else { | |
339 final = MathMin(relativeEnd, len); | |
340 } | |
341 | |
342 var count = MathMax(final - k, 0); | |
343 var array = ConstructTypedArrayLike(this, count); | |
344 // The code below is the 'then' branch; the 'else' branch species | |
345 // a memcpy. Because V8 doesn't canonicalize NaN, the difference is | |
346 // unobservable. | |
347 var n = 0; | |
348 while (k < final) { | |
349 var kValue = this[k]; | |
350 // TODO(littledan): The spec says to throw on an error in setting; | |
351 // does this throw? | |
arv (Not doing code reviews)
2015/06/08 23:57:12
If you make this function strict you should get th
| |
352 array[n] = kValue; | |
353 k++; | |
354 n++; | |
355 } | |
356 return array; | |
357 } | |
358 | |
359 | |
309 // ES6 draft 08-24-14, section 22.2.2.2 | 360 // ES6 draft 08-24-14, section 22.2.2.2 |
310 function TypedArrayOf() { | 361 function TypedArrayOf() { |
311 var length = %_ArgumentsLength(); | 362 var length = %_ArgumentsLength(); |
312 var array = new this(length); | 363 var array = new this(length); |
313 for (var i = 0; i < length; i++) { | 364 for (var i = 0; i < length; i++) { |
314 array[i] = %_Arguments(i); | 365 array[i] = %_Arguments(i); |
315 } | 366 } |
316 return array; | 367 return array; |
317 } | 368 } |
318 | 369 |
(...skipping 23 matching lines...) Expand all Loading... | |
342 "find", TypedArrayFind, | 393 "find", TypedArrayFind, |
343 "findIndex", TypedArrayFindIndex, | 394 "findIndex", TypedArrayFindIndex, |
344 "indexOf", TypedArrayIndexOf, | 395 "indexOf", TypedArrayIndexOf, |
345 "join", TypedArrayJoin, | 396 "join", TypedArrayJoin, |
346 "lastIndexOf", TypedArrayLastIndexOf, | 397 "lastIndexOf", TypedArrayLastIndexOf, |
347 "forEach", TypedArrayForEach, | 398 "forEach", TypedArrayForEach, |
348 "map", TypedArrayMap, | 399 "map", TypedArrayMap, |
349 "reduce", TypedArrayReduce, | 400 "reduce", TypedArrayReduce, |
350 "reduceRight", TypedArrayReduceRight, | 401 "reduceRight", TypedArrayReduceRight, |
351 "reverse", TypedArrayReverse, | 402 "reverse", TypedArrayReverse, |
403 "slice", TypedArraySlice, | |
352 "some", TypedArraySome, | 404 "some", TypedArraySome, |
353 "sort", TypedArraySort, | 405 "sort", TypedArraySort, |
354 "toString", TypedArrayToString, | 406 "toString", TypedArrayToString, |
355 "toLocaleString", TypedArrayToLocaleString | 407 "toLocaleString", TypedArrayToLocaleString |
356 ]); | 408 ]); |
357 endmacro | 409 endmacro |
358 | 410 |
359 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | 411 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) |
360 | 412 |
361 }) | 413 }) |
OLD | NEW |