Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-arrays | |
| 6 | |
| 7 var typedArrayConstructors = [ | |
| 8 Uint8Array, | |
| 9 Int8Array, | |
| 10 Uint16Array, | |
| 11 Int16Array, | |
| 12 Uint32Array, | |
| 13 Int32Array, | |
| 14 Uint8ClampedArray, | |
| 15 Float32Array, | |
| 16 Float64Array]; | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
] on next line
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 17 | |
| 18 typedArrayConstructors.forEach(function(constructor) { | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
I find for-of loops easier to read
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 19 | |
| 20 assertEquals(1, constructor.from.length); | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
indent
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 21 | |
| 22 // TypedArray.from only callable on this subclassing %TypedArray% | |
| 23 assertThrows(function () {constructor.from.call(Array, [])}, TypeError); | |
| 24 | |
| 25 var construct_fn = constructor.construct; | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
Where did construct come from?
dehrenberg
2015/05/15 23:39:14
Oops, this was a remnant for a test that I deleted
| |
| 26 | |
| 27 function assertArrayLikeEquals(value, expected, type) { | |
| 28 assertInstanceof(value, type); | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
I tend to prefer to check the __proto__ or Object.
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 29 assertEquals(expected.length, value.length); | |
| 30 for (var i=0; i<value.length; ++i) { | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
ws around binops
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 31 assertEquals(expected[i], value[i]); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 // Assert that calling mapfn with / without thisArg in sloppy and strict modes | |
| 36 // works as expected. | |
| 37 var global = this; | |
| 38 function non_strict(){ assertEquals(global, this); } | |
|
arv (Not doing code reviews)
2015/05/15 00:20:23
ws before {
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 39 function strict(){ "use strict"; assertEquals(void 0, this); } | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
you can use undefined in tests if you want
dehrenberg
2015/05/15 23:39:14
Done.
| |
| 40 function strict_null(){ "use strict"; assertEquals(null, this); } | |
| 41 constructor.from([1], non_strict); | |
| 42 constructor.from([1], non_strict, void 0); | |
| 43 constructor.from([1], non_strict, null); | |
| 44 constructor.from([1], strict); | |
| 45 constructor.from([1], strict, void 0); | |
| 46 constructor.from([1], strict_null, null); | |
| 47 | |
| 48 // TypedArray.from can only be called on TypedArray constructors | |
| 49 assertThrows(function() {constructor.from.call({}, [])}, TypeError); | |
| 50 assertThrows(function() {constructor.from.call([], [])}, TypeError); | |
| 51 assertThrows(function() {constructor.from.call(1, [])}, TypeError); | |
| 52 assertThrows(function() {constructor.from.call(undefined, [])}, TypeError); | |
| 53 | |
| 54 // Converting from various other types, demonstrating that it can | |
| 55 // operate on array-like objects as well as iterables. | |
| 56 // TODO(dehrenberg): constructors should have similar flexibility. | |
| 57 assertArrayLikeEquals(constructor.from( | |
| 58 { length: 1, '0': 5 }), [5], constructor); | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
no need to quote the key here.
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 59 | |
| 60 assertArrayLikeEquals(constructor.from( | |
| 61 { length: -1, '0': 5 }), [], constructor); | |
| 62 | |
| 63 assertArrayLikeEquals(constructor.from( | |
| 64 [1, 2, 3]), [1, 2, 3], constructor); | |
| 65 | |
| 66 var kSet = new Set([1, 2, 3]); | |
| 67 assertArrayLikeEquals(constructor.from(kSet), [1, 2, 3], | |
| 68 constructor); | |
| 69 | |
| 70 function* generator() { | |
| 71 yield 4; | |
| 72 yield 5; | |
| 73 yield 6; | |
| 74 } | |
| 75 | |
| 76 assertArrayLikeEquals(constructor.from(generator()), | |
| 77 [4, 5, 6], constructor); | |
| 78 | |
| 79 assertThrows(function() { constructor.from(null); }, TypeError); | |
| 80 assertThrows(function() { constructor.from(undefined); }, TypeError); | |
| 81 assertThrows(function() { constructor.from([], null); }, TypeError); | |
| 82 assertThrows(function() { constructor.from([], "noncallable"); }, | |
|
arv (Not doing code reviews)
2015/05/15 00:20:22
stick to either " or '
dehrenberg
2015/05/15 23:39:15
Done.
| |
| 83 TypeError); | |
| 84 | |
| 85 var nullIterator = {}; | |
| 86 nullIterator[Symbol.iterator] = null; | |
| 87 assertArrayLikeEquals(constructor.from(nullIterator), [], | |
| 88 constructor); | |
| 89 | |
| 90 var nonObjIterator = {}; | |
| 91 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; | |
| 92 assertThrows(function() { constructor.from(nonObjIterator); }, | |
| 93 TypeError); | |
| 94 | |
| 95 assertThrows(function() { constructor.from([], null); }, TypeError); | |
| 96 | |
| 97 // Ensure iterator is only accessed once, and only invoked once | |
| 98 var called = false; | |
| 99 var arr = [1, 2, 3]; | |
| 100 var obj = {}; | |
| 101 | |
| 102 // Test order --- only get iterator method once | |
| 103 function testIterator() { | |
| 104 assertFalse(called, "@@iterator should be called only once"); | |
| 105 called = true; | |
| 106 assertEquals(obj, this); | |
| 107 return arr[Symbol.iterator](); | |
| 108 } | |
| 109 var getCalled = false; | |
| 110 Object.defineProperty(obj, Symbol.iterator, { | |
| 111 get: function() { | |
| 112 assertFalse(getCalled, "@@iterator should be accessed only once"); | |
| 113 getCalled = true; | |
| 114 return testIterator; | |
| 115 }, | |
| 116 set: function() { | |
| 117 assertUnreachable("@@iterator should not be set"); | |
| 118 } | |
| 119 }); | |
| 120 assertArrayLikeEquals(constructor.from(obj), [1, 2, 3], constructor); | |
| 121 assertEquals(getCalled, true); | |
| 122 assertEquals(called, true); | |
| 123 | |
| 124 assertEquals(constructor, Uint8Array.from.call(constructor, [1]).constructor); | |
| 125 assertEquals(Uint8Array, constructor.from.call(Uint8Array, [1]).constructor); | |
| 126 | |
| 127 }); | |
| OLD | NEW |