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 | |
| 17 ]; | |
| 18 | |
| 19 for (var constructor of typedArrayConstructors) { | |
| 20 assertEquals(1, constructor.from.length); | |
| 21 | |
| 22 // TypedArray.from only callable on this subclassing %TypedArray% | |
| 23 assertThrows(function () {constructor.from.call(Array, [])}, TypeError); | |
| 24 | |
| 25 function assertArrayLikeEquals(value, expected, type) { | |
| 26 assertEquals(value.__proto__, type.prototype); | |
| 27 assertEquals(expected.length, value.length); | |
| 28 for (var i = 0; i < value.length; ++i) { | |
| 29 assertEquals(expected[i], value[i]); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 // Assert that calling mapfn with / without thisArg in sloppy and strict modes | |
| 34 // works as expected. | |
| 35 var global = this; | |
| 36 function non_strict() { assertEquals(global, this); } | |
| 37 function strict() { 'use strict'; assertEquals(undefined, this); } | |
| 38 function strict_null() { 'use strict'; assertEquals(null, this); } | |
| 39 constructor.from([1], non_strict); | |
| 40 constructor.from([1], non_strict, void 0); | |
| 41 constructor.from([1], non_strict, null); | |
| 42 constructor.from([1], strict); | |
| 43 constructor.from([1], strict, void 0); | |
| 44 constructor.from([1], strict_null, null); | |
| 45 | |
| 46 // TypedArray.from can only be called on TypedArray constructors | |
| 47 assertThrows(function() {constructor.from.call({}, [])}, TypeError); | |
| 48 assertThrows(function() {constructor.from.call([], [])}, TypeError); | |
| 49 assertThrows(function() {constructor.from.call(1, [])}, TypeError); | |
| 50 assertThrows(function() {constructor.from.call(undefined, [])}, TypeError); | |
| 51 | |
| 52 // Converting from various other types, demonstrating that it can | |
| 53 // operate on array-like objects as well as iterables. | |
| 54 // TODO(littledan): constructors should have similar flexibility. | |
| 55 assertArrayLikeEquals(constructor.from( | |
| 56 { length: 1, 0: 5 }), [5], constructor); | |
| 57 | |
| 58 assertArrayLikeEquals(constructor.from( | |
| 59 { length: -1, 0: 5 }), [], constructor); | |
| 60 | |
| 61 assertArrayLikeEquals(constructor.from( | |
| 62 [1, 2, 3]), [1, 2, 3], constructor); | |
| 63 | |
| 64 var kSet = new Set([1, 2, 3]); | |
|
arv (Not doing code reviews)
2015/05/16 18:23:45
kSet -> set
dehrenberg
2015/05/18 17:56:50
Done.
| |
| 65 assertArrayLikeEquals(constructor.from(kSet), [1, 2, 3], | |
| 66 constructor); | |
| 67 | |
| 68 function* generator() { | |
| 69 yield 4; | |
| 70 yield 5; | |
| 71 yield 6; | |
| 72 } | |
| 73 | |
| 74 assertArrayLikeEquals(constructor.from(generator()), | |
| 75 [4, 5, 6], constructor); | |
| 76 | |
| 77 assertThrows(function() { constructor.from(null); }, TypeError); | |
| 78 assertThrows(function() { constructor.from(undefined); }, TypeError); | |
| 79 assertThrows(function() { constructor.from([], null); }, TypeError); | |
| 80 assertThrows(function() { constructor.from([], 'noncallable'); }, | |
| 81 TypeError); | |
| 82 | |
| 83 var nullIterator = {}; | |
| 84 nullIterator[Symbol.iterator] = null; | |
| 85 assertArrayLikeEquals(constructor.from(nullIterator), [], | |
| 86 constructor); | |
| 87 | |
| 88 var nonObjIterator = {}; | |
| 89 nonObjIterator[Symbol.iterator] = function() { return 'nonObject'; }; | |
| 90 assertThrows(function() { constructor.from(nonObjIterator); }, | |
| 91 TypeError); | |
| 92 | |
| 93 assertThrows(function() { constructor.from([], null); }, TypeError); | |
| 94 | |
| 95 // Ensure iterator is only accessed once, and only invoked once | |
| 96 var called = false; | |
| 97 var arr = [1, 2, 3]; | |
| 98 var obj = {}; | |
| 99 | |
| 100 // Test order --- only get iterator method once | |
| 101 function testIterator() { | |
| 102 assertFalse(called, '@@iterator should be called only once'); | |
| 103 called = true; | |
|
arv (Not doing code reviews)
2015/05/16 18:23:45
another way to do this is to use a counter and the
dehrenberg
2015/05/18 17:56:50
Done. Thanks, that's cleaner.
| |
| 104 assertEquals(obj, this); | |
| 105 return arr[Symbol.iterator](); | |
| 106 } | |
| 107 var getCalled = false; | |
| 108 Object.defineProperty(obj, Symbol.iterator, { | |
| 109 get: function() { | |
| 110 assertFalse(getCalled, '@@iterator should be accessed only once'); | |
| 111 getCalled = true; | |
| 112 return testIterator; | |
| 113 }, | |
| 114 set: function() { | |
| 115 assertUnreachable('@@iterator should not be set'); | |
| 116 } | |
| 117 }); | |
| 118 assertArrayLikeEquals(constructor.from(obj), [1, 2, 3], constructor); | |
| 119 assertEquals(getCalled, true); | |
| 120 assertEquals(called, true); | |
| 121 | |
| 122 assertEquals(constructor, Uint8Array.from.call(constructor, [1]).constructor); | |
| 123 assertEquals(Uint8Array, constructor.from.call(Uint8Array, [1]).constructor); | |
| 124 | |
|
arv (Not doing code reviews)
2015/05/16 18:23:45
remove empty line
dehrenberg
2015/05/18 17:56:50
Done.
| |
| 125 } | |
| OLD | NEW |