| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --harmony-collections --harmony-iteration | |
| 29 // Flags: --expose-gc --allow-natives-syntax | 28 // Flags: --expose-gc --allow-natives-syntax |
| 30 | 29 |
| 31 | 30 |
| 32 // Test valid getter and setter calls on Sets and WeakSets | 31 // Test valid getter and setter calls on Sets and WeakSets |
| 33 function TestValidSetCalls(m) { | 32 function TestValidSetCalls(m) { |
| 34 assertDoesNotThrow(function () { m.add(new Object) }); | 33 assertDoesNotThrow(function () { m.add(new Object) }); |
| 35 assertDoesNotThrow(function () { m.has(new Object) }); | 34 assertDoesNotThrow(function () { m.has(new Object) }); |
| 36 assertDoesNotThrow(function () { m.delete(new Object) }); | 35 assertDoesNotThrow(function () { m.delete(new Object) }); |
| 37 } | 36 } |
| 38 TestValidSetCalls(new Set); | 37 TestValidSetCalls(new Set); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 282 |
| 284 | 283 |
| 285 // Test name of constructor. | 284 // Test name of constructor. |
| 286 assertEquals("Set", Set.name); | 285 assertEquals("Set", Set.name); |
| 287 assertEquals("Map", Map.name); | 286 assertEquals("Map", Map.name); |
| 288 assertEquals("WeakMap", WeakMap.name); | 287 assertEquals("WeakMap", WeakMap.name); |
| 289 assertEquals("WeakSet", WeakSet.name); | 288 assertEquals("WeakSet", WeakSet.name); |
| 290 | 289 |
| 291 | 290 |
| 292 // Test prototype property of Set, Map, WeakMap and WeakSet. | 291 // Test prototype property of Set, Map, WeakMap and WeakSet. |
| 293 // TODO(2793): Should all be non-writable, and the extra flag removed. | 292 function TestPrototype(C) { |
| 294 function TestPrototype(C, writable) { | |
| 295 assertTrue(C.prototype instanceof Object); | 293 assertTrue(C.prototype instanceof Object); |
| 296 assertEquals({ | 294 assertEquals({ |
| 297 value: {}, | 295 value: {}, |
| 298 writable: writable, | 296 writable: false, |
| 299 enumerable: false, | 297 enumerable: false, |
| 300 configurable: false | 298 configurable: false |
| 301 }, Object.getOwnPropertyDescriptor(C, "prototype")); | 299 }, Object.getOwnPropertyDescriptor(C, "prototype")); |
| 302 } | 300 } |
| 303 TestPrototype(Set, true); | 301 TestPrototype(Set); |
| 304 TestPrototype(Map, true); | 302 TestPrototype(Map); |
| 305 TestPrototype(WeakMap, false); | 303 TestPrototype(WeakMap); |
| 306 TestPrototype(WeakSet, false); | 304 TestPrototype(WeakSet); |
| 307 | 305 |
| 308 | 306 |
| 309 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. | 307 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. |
| 310 function TestConstructor(C) { | 308 function TestConstructor(C) { |
| 311 assertFalse(C === Object.prototype.constructor); | 309 assertFalse(C === Object.prototype.constructor); |
| 312 assertSame(C, C.prototype.constructor); | 310 assertSame(C, C.prototype.constructor); |
| 313 assertSame(C, (new C).__proto__.constructor); | 311 assertSame(C, (new C).__proto__.constructor); |
| 314 } | 312 } |
| 315 TestConstructor(Set); | 313 TestConstructor(Set); |
| 316 TestConstructor(Map); | 314 TestConstructor(Map); |
| (...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1022 s2.add('b'); | 1020 s2.add('b'); |
| 1023 s2.add('c'); | 1021 s2.add('c'); |
| 1024 s = new Set(s2.values()); | 1022 s = new Set(s2.values()); |
| 1025 assertEquals(s.size, 3); | 1023 assertEquals(s.size, 3); |
| 1026 assertTrue(s.has('a')); | 1024 assertTrue(s.has('a')); |
| 1027 assertTrue(s.has('b')); | 1025 assertTrue(s.has('b')); |
| 1028 assertTrue(s.has('c')); | 1026 assertTrue(s.has('c')); |
| 1029 })(); | 1027 })(); |
| 1030 | 1028 |
| 1031 | 1029 |
| 1030 // Allows testing iterator-based constructors easily. |
| 1031 var oneAndTwo = new Set(); |
| 1032 oneAndTwo.add(1); |
| 1033 oneAndTwo.add(2); |
| 1034 |
| 1035 |
| 1032 (function TestSetConstructorAddNotCallable() { | 1036 (function TestSetConstructorAddNotCallable() { |
| 1033 var originalSetPrototypeAdd = Set.prototype.add; | 1037 var originalSetPrototypeAdd = Set.prototype.add; |
| 1034 assertThrows(function() { | 1038 assertThrows(function() { |
| 1035 Set.prototype.add = 42; | 1039 Set.prototype.add = 42; |
| 1036 new Set([1, 2].values()); | 1040 new Set(oneAndTwo.values()); |
| 1037 }, TypeError); | 1041 }, TypeError); |
| 1038 Set.prototype.add = originalSetPrototypeAdd; | 1042 Set.prototype.add = originalSetPrototypeAdd; |
| 1039 })(); | 1043 })(); |
| 1040 | 1044 |
| 1041 | 1045 |
| 1042 (function TestSetConstructorGetAddOnce() { | 1046 (function TestSetConstructorGetAddOnce() { |
| 1043 var originalSetPrototypeAdd = Set.prototype.add; | 1047 var originalSetPrototypeAdd = Set.prototype.add; |
| 1044 var getAddCount = 0; | 1048 var getAddCount = 0; |
| 1045 Object.defineProperty(Set.prototype, 'add', { | 1049 Object.defineProperty(Set.prototype, 'add', { |
| 1046 get: function() { | 1050 get: function() { |
| 1047 getAddCount++; | 1051 getAddCount++; |
| 1048 return function() {}; | 1052 return function() {}; |
| 1049 } | 1053 } |
| 1050 }); | 1054 }); |
| 1051 var s = new Set([1, 2].values()); | 1055 var s = new Set(oneAndTwo.values()); |
| 1052 assertEquals(getAddCount, 1); | 1056 assertEquals(getAddCount, 1); |
| 1053 assertEquals(s.size, 0); | 1057 assertEquals(s.size, 0); |
| 1054 Object.defineProperty(Set.prototype, 'add', { | 1058 Object.defineProperty(Set.prototype, 'add', { |
| 1055 value: originalSetPrototypeAdd, | 1059 value: originalSetPrototypeAdd, |
| 1056 writable: true | 1060 writable: true |
| 1057 }); | 1061 }); |
| 1058 })(); | 1062 })(); |
| 1059 | 1063 |
| 1060 | 1064 |
| 1061 (function TestSetConstructorAddReplaced() { | 1065 (function TestSetConstructorAddReplaced() { |
| 1062 var originalSetPrototypeAdd = Set.prototype.add; | 1066 var originalSetPrototypeAdd = Set.prototype.add; |
| 1063 var addCount = 0; | 1067 var addCount = 0; |
| 1064 Set.prototype.add = function(value) { | 1068 Set.prototype.add = function(value) { |
| 1065 addCount++; | 1069 addCount++; |
| 1066 originalSetPrototypeAdd.call(this, value); | 1070 originalSetPrototypeAdd.call(this, value); |
| 1067 Set.prototype.add = null; | 1071 Set.prototype.add = null; |
| 1068 }; | 1072 }; |
| 1069 var s = new Set([1, 2].values()); | 1073 var s = new Set(oneAndTwo.values()); |
| 1070 assertEquals(addCount, 2); | 1074 assertEquals(addCount, 2); |
| 1071 assertEquals(s.size, 2); | 1075 assertEquals(s.size, 2); |
| 1072 Set.prototype.add = originalSetPrototypeAdd; | 1076 Set.prototype.add = originalSetPrototypeAdd; |
| 1073 })(); | 1077 })(); |
| 1074 | 1078 |
| 1075 | 1079 |
| 1076 (function TestSetConstructorOrderOfDoneValue() { | 1080 (function TestSetConstructorOrderOfDoneValue() { |
| 1077 var valueCount = 0, doneCount = 0; | 1081 var valueCount = 0, doneCount = 0; |
| 1078 var iterator = { | 1082 var iterator = { |
| 1079 next: function() { | 1083 next: function() { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1151 assertEquals(m.get(0), 'a'); | 1155 assertEquals(m.get(0), 'a'); |
| 1152 assertEquals(m.get(1), 'b'); | 1156 assertEquals(m.get(1), 'b'); |
| 1153 assertEquals(m.get(2), 'c'); | 1157 assertEquals(m.get(2), 'c'); |
| 1154 })(); | 1158 })(); |
| 1155 | 1159 |
| 1156 | 1160 |
| 1157 (function TestMapConstructorSetNotCallable() { | 1161 (function TestMapConstructorSetNotCallable() { |
| 1158 var originalMapPrototypeSet = Map.prototype.set; | 1162 var originalMapPrototypeSet = Map.prototype.set; |
| 1159 assertThrows(function() { | 1163 assertThrows(function() { |
| 1160 Map.prototype.set = 42; | 1164 Map.prototype.set = 42; |
| 1161 new Map([1, 2].entries()); | 1165 new Map(oneAndTwo.entries()); |
| 1162 }, TypeError); | 1166 }, TypeError); |
| 1163 Map.prototype.set = originalMapPrototypeSet; | 1167 Map.prototype.set = originalMapPrototypeSet; |
| 1164 })(); | 1168 })(); |
| 1165 | 1169 |
| 1166 | 1170 |
| 1167 (function TestMapConstructorGetAddOnce() { | 1171 (function TestMapConstructorGetAddOnce() { |
| 1168 var originalMapPrototypeSet = Map.prototype.set; | 1172 var originalMapPrototypeSet = Map.prototype.set; |
| 1169 var getSetCount = 0; | 1173 var getSetCount = 0; |
| 1170 Object.defineProperty(Map.prototype, 'set', { | 1174 Object.defineProperty(Map.prototype, 'set', { |
| 1171 get: function() { | 1175 get: function() { |
| 1172 getSetCount++; | 1176 getSetCount++; |
| 1173 return function() {}; | 1177 return function() {}; |
| 1174 } | 1178 } |
| 1175 }); | 1179 }); |
| 1176 var m = new Map([1, 2].entries()); | 1180 var m = new Map(oneAndTwo.entries()); |
| 1177 assertEquals(getSetCount, 1); | 1181 assertEquals(getSetCount, 1); |
| 1178 assertEquals(m.size, 0); | 1182 assertEquals(m.size, 0); |
| 1179 Object.defineProperty(Map.prototype, 'set', { | 1183 Object.defineProperty(Map.prototype, 'set', { |
| 1180 value: originalMapPrototypeSet, | 1184 value: originalMapPrototypeSet, |
| 1181 writable: true | 1185 writable: true |
| 1182 }); | 1186 }); |
| 1183 })(); | 1187 })(); |
| 1184 | 1188 |
| 1185 | 1189 |
| 1186 (function TestMapConstructorSetReplaced() { | 1190 (function TestMapConstructorSetReplaced() { |
| 1187 var originalMapPrototypeSet = Map.prototype.set; | 1191 var originalMapPrototypeSet = Map.prototype.set; |
| 1188 var setCount = 0; | 1192 var setCount = 0; |
| 1189 Map.prototype.set = function(key, value) { | 1193 Map.prototype.set = function(key, value) { |
| 1190 setCount++; | 1194 setCount++; |
| 1191 originalMapPrototypeSet.call(this, key, value); | 1195 originalMapPrototypeSet.call(this, key, value); |
| 1192 Map.prototype.set = null; | 1196 Map.prototype.set = null; |
| 1193 }; | 1197 }; |
| 1194 var m = new Map([1, 2].entries()); | 1198 var m = new Map(oneAndTwo.entries()); |
| 1195 assertEquals(setCount, 2); | 1199 assertEquals(setCount, 2); |
| 1196 assertEquals(m.size, 2); | 1200 assertEquals(m.size, 2); |
| 1197 Map.prototype.set = originalMapPrototypeSet; | 1201 Map.prototype.set = originalMapPrototypeSet; |
| 1198 })(); | 1202 })(); |
| 1199 | 1203 |
| 1200 | 1204 |
| 1201 (function TestMapConstructorOrderOfDoneValue() { | 1205 (function TestMapConstructorOrderOfDoneValue() { |
| 1202 var valueCount = 0, doneCount = 0; | 1206 var valueCount = 0, doneCount = 0; |
| 1203 function FakeError() {} | 1207 function FakeError() {} |
| 1204 var iterator = { | 1208 var iterator = { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1235 return this; | 1239 return this; |
| 1236 }; | 1240 }; |
| 1237 assertThrows(function() { | 1241 assertThrows(function() { |
| 1238 new Map(iterator); | 1242 new Map(iterator); |
| 1239 }, TypeError); | 1243 }, TypeError); |
| 1240 })(); | 1244 })(); |
| 1241 | 1245 |
| 1242 | 1246 |
| 1243 (function TestMapConstructorIteratorNotObjectValues() { | 1247 (function TestMapConstructorIteratorNotObjectValues() { |
| 1244 assertThrows(function() { | 1248 assertThrows(function() { |
| 1245 new Map([1, 2].values()); | 1249 new Map(oneAndTwo.values()); |
| 1246 }, TypeError); | 1250 }, TypeError); |
| 1247 })(); | 1251 })(); |
| OLD | NEW |