| 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 // Flags: --harmony-arrays --harmony-generators | 5 // Flags: --harmony-arrays |
| 6 (function() { | 6 (function() { |
| 7 | 7 |
| 8 assertEquals(1, Array.from.length); | 8 assertEquals(1, Array.from.length); |
| 9 | 9 |
| 10 function assertArrayLikeEquals(value, expected, type) { | 10 function assertArrayLikeEquals(value, expected, type) { |
| 11 assertInstanceof(value, type); | 11 assertInstanceof(value, type); |
| 12 assertEquals(expected.length, value.length); | 12 assertEquals(expected.length, value.length); |
| 13 for (var i=0; i<value.length; ++i) { | 13 for (var i=0; i<value.length; ++i) { |
| 14 assertEquals(expected[i], value[i]); | 14 assertEquals(expected[i], value[i]); |
| 15 } | 15 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 84 |
| 85 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | 85 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { |
| 86 return this.filter(x); | 86 return this.filter(x); |
| 87 }, { | 87 }, { |
| 88 filter: function(x) { return x.toUpperCase(); } | 88 filter: function(x) { return x.toUpperCase(); } |
| 89 }), ['T', 'E', 'S', 'T'], constructor); | 89 }), ['T', 'E', 'S', 'T'], constructor); |
| 90 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | 90 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { |
| 91 return x.toUpperCase(); | 91 return x.toUpperCase(); |
| 92 }), ['T', 'E', 'S', 'T'], constructor); | 92 }), ['T', 'E', 'S', 'T'], constructor); |
| 93 | 93 |
| 94 this.thisArg = thisArg; | 94 assertThrows(function() { Array.from.call(thisArg, null); }, TypeError); |
| 95 assertThrows('Array.from.call(thisArg, null)', TypeError); | 95 assertThrows(function() { Array.from.call(thisArg, undefined); }, TypeError); |
| 96 assertThrows('Array.from.call(thisArg, undefined)', TypeError); | 96 assertThrows(function() { Array.from.call(thisArg, [], null); }, TypeError); |
| 97 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | 97 assertThrows(function() { Array.from.call(thisArg, [], "noncallable"); }, |
| 98 assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError); | 98 TypeError); |
| 99 | 99 |
| 100 this.nullIterator = {}; | 100 var nullIterator = {}; |
| 101 nullIterator[Symbol.iterator] = null; | 101 nullIterator[Symbol.iterator] = null; |
| 102 assertThrows('Array.from.call(thisArg, nullIterator)', TypeError); | 102 assertArrayLikeEquals(Array.from.call(thisArg, nullIterator), [], |
| 103 constructor); |
| 103 | 104 |
| 104 this.nonObjIterator = {}; | 105 var nonObjIterator = {}; |
| 105 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; | 106 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; |
| 106 assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError); | 107 assertThrows(function() { Array.from.call(thisArg, nonObjIterator); }, |
| 108 TypeError); |
| 107 | 109 |
| 108 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | 110 assertThrows(function() { Array.from.call(thisArg, [], null); }, TypeError); |
| 111 |
| 112 // Ensure iterator is only accessed once, and only invoked once |
| 113 var called = false; |
| 114 var arr = [1, 2, 3]; |
| 115 var obj = {}; |
| 116 |
| 117 // Test order --- only get iterator method once |
| 118 function testIterator() { |
| 119 assertFalse(called, "@@iterator should be called only once"); |
| 120 called = true; |
| 121 assertEquals(obj, this); |
| 122 return arr[Symbol.iterator](); |
| 123 } |
| 124 var getCalled = false; |
| 125 Object.defineProperty(obj, Symbol.iterator, { |
| 126 get: function() { |
| 127 assertFalse(getCalled, "@@iterator should be accessed only once"); |
| 128 getCalled = true; |
| 129 return testIterator; |
| 130 }, |
| 131 set: function() { |
| 132 assertUnreachable("@@iterator should not be set"); |
| 133 } |
| 134 }); |
| 135 assertArrayLikeEquals(Array.from.call(thisArg, obj), [1, 2, 3], constructor); |
| 109 } | 136 } |
| 110 | 137 |
| 111 function Other() {} | 138 function Other() {} |
| 112 | 139 |
| 113 var boundFn = (function() {}).bind(Array, 27); | 140 var boundFn = (function() {}).bind(Array, 27); |
| 114 | 141 |
| 115 testArrayFrom(Array, Array); | 142 testArrayFrom(Array, Array); |
| 116 testArrayFrom(null, Array); | 143 testArrayFrom(null, Array); |
| 117 testArrayFrom({}, Array); | 144 testArrayFrom({}, Array); |
| 118 testArrayFrom(Object, Object); | 145 testArrayFrom(Object, Object); |
| 119 testArrayFrom(Other, Other); | 146 testArrayFrom(Other, Other); |
| 120 testArrayFrom(Math.cos, Array); | 147 testArrayFrom(Math.cos, Array); |
| 121 testArrayFrom(Math.cos.bind(Math), Array); | 148 testArrayFrom(Math.cos.bind(Math), Array); |
| 122 testArrayFrom(boundFn, boundFn); | 149 testArrayFrom(boundFn, boundFn); |
| 123 | 150 |
| 124 })(); | 151 })(); |
| OLD | NEW |