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 |
(...skipping 10 matching lines...) Expand all Loading... | |
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: --expose-gc --allow-natives-syntax | 28 // Flags: --expose-gc --allow-natives-syntax |
29 | 29 |
30 | 30 |
31 function assertSize(expected, collection) { | |
32 if (collection instanceof Map || collection instanceof Set) { | |
33 assertEquals(expected, collection.size); | |
34 } | |
35 } | |
36 | |
37 | |
31 // Test valid getter and setter calls on Sets and WeakSets | 38 // Test valid getter and setter calls on Sets and WeakSets |
32 function TestValidSetCalls(m) { | 39 function TestValidSetCalls(m) { |
33 assertDoesNotThrow(function () { m.add(new Object) }); | 40 assertDoesNotThrow(function () { m.add(new Object) }); |
34 assertDoesNotThrow(function () { m.has(new Object) }); | 41 assertDoesNotThrow(function () { m.has(new Object) }); |
35 assertDoesNotThrow(function () { m.delete(new Object) }); | 42 assertDoesNotThrow(function () { m.delete(new Object) }); |
36 } | 43 } |
37 TestValidSetCalls(new Set); | 44 TestValidSetCalls(new Set); |
38 TestValidSetCalls(new WeakSet); | 45 TestValidSetCalls(new WeakSet); |
39 | 46 |
40 | 47 |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 TestPrototype(Map); | 309 TestPrototype(Map); |
303 TestPrototype(WeakMap); | 310 TestPrototype(WeakMap); |
304 TestPrototype(WeakSet); | 311 TestPrototype(WeakSet); |
305 | 312 |
306 | 313 |
307 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. | 314 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. |
308 function TestConstructor(C) { | 315 function TestConstructor(C) { |
309 assertFalse(C === Object.prototype.constructor); | 316 assertFalse(C === Object.prototype.constructor); |
310 assertSame(C, C.prototype.constructor); | 317 assertSame(C, C.prototype.constructor); |
311 assertSame(C, (new C).__proto__.constructor); | 318 assertSame(C, (new C).__proto__.constructor); |
319 assertEquals(1, C.length); | |
312 } | 320 } |
313 TestConstructor(Set); | 321 TestConstructor(Set); |
314 TestConstructor(Map); | 322 TestConstructor(Map); |
315 TestConstructor(WeakMap); | 323 TestConstructor(WeakMap); |
316 TestConstructor(WeakSet); | 324 TestConstructor(WeakSet); |
317 | 325 |
318 | 326 |
319 // Test the Set, Map, WeakMap and WeakSet global properties themselves. | 327 // Test the Set, Map, WeakMap and WeakSet global properties themselves. |
320 function TestDescriptor(global, C) { | 328 function TestDescriptor(global, C) { |
321 assertEquals({ | 329 assertEquals({ |
(...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
980 for (var i = 5; i < 16; i++) { | 988 for (var i = 5; i < 16; i++) { |
981 map.delete(i); | 989 map.delete(i); |
982 } | 990 } |
983 } | 991 } |
984 }); | 992 }); |
985 | 993 |
986 assertArrayEquals([0, 1, 2, 3, 4], buffer); | 994 assertArrayEquals([0, 1, 2, 3, 4], buffer); |
987 })(); | 995 })(); |
988 | 996 |
989 | 997 |
990 (function TestSetConstructor() { | 998 // Allows testing iterator-based constructors easily. |
991 var s = new Set(null); | 999 var oneAndTwo = new Map(); |
992 assertEquals(s.size, 0); | 1000 var k0 = {key: 0}; |
1001 var k1 = {key: 1}; | |
1002 var k2 = {key: 2}; | |
1003 oneAndTwo.set(k1, 1); | |
1004 oneAndTwo.set(k2, 2); | |
993 | 1005 |
994 s = new Set(undefined); | 1006 |
995 assertEquals(s.size, 0); | 1007 function TestSetConstructor(ctor) { |
1008 var s = new ctor(null); | |
1009 assertSize(0, s); | |
1010 | |
1011 s = new ctor(undefined); | |
1012 assertSize(0, s); | |
996 | 1013 |
997 // No @@iterator | 1014 // No @@iterator |
998 assertThrows(function() { | 1015 assertThrows(function() { |
999 new Set({}); | 1016 new ctor({}); |
1000 }, TypeError); | 1017 }, TypeError); |
1001 | 1018 |
1002 // @@iterator not callable | 1019 // @@iterator not callable |
1003 assertThrows(function() { | 1020 assertThrows(function() { |
1004 var object = {}; | 1021 var object = {}; |
1005 object[Symbol.iterator] = 42; | 1022 object[Symbol.iterator] = 42; |
1006 new Set(object); | 1023 new ctor(object); |
1007 }, TypeError); | 1024 }, TypeError); |
1008 | 1025 |
1009 // @@iterator result not object | 1026 // @@iterator result not object |
rossberg
2014/08/08 11:24:05
Exclusively for weak maps and sets, we should also
| |
1010 assertThrows(function() { | 1027 assertThrows(function() { |
1011 var object = {}; | 1028 var object = {}; |
1012 object[Symbol.iterator] = function() { | 1029 object[Symbol.iterator] = function() { |
1013 return 42; | 1030 return 42; |
1014 }; | 1031 }; |
1015 new Set(object); | 1032 new ctor(object); |
1016 }, TypeError); | 1033 }, TypeError); |
1017 | 1034 |
1018 var s2 = new Set(); | 1035 var s2 = new Set(); |
1019 s2.add('a'); | 1036 s2.add(k0); |
1020 s2.add('b'); | 1037 s2.add(k1); |
1021 s2.add('c'); | 1038 s2.add(k2); |
1022 s = new Set(s2.values()); | 1039 s = new ctor(s2.values()); |
1023 assertEquals(s.size, 3); | 1040 assertSize(3, s); |
1024 assertTrue(s.has('a')); | 1041 assertTrue(s.has(k0)); |
1025 assertTrue(s.has('b')); | 1042 assertTrue(s.has(k1)); |
1026 assertTrue(s.has('c')); | 1043 assertTrue(s.has(k2)); |
1027 })(); | 1044 } |
1045 TestSetConstructor(Set); | |
1046 TestSetConstructor(WeakSet); | |
1028 | 1047 |
1029 | 1048 |
1030 // Allows testing iterator-based constructors easily. | 1049 function TestSetConstructorAddNotCallable(ctor) { |
1031 var oneAndTwo = new Set(); | 1050 var originalPrototypeAdd = ctor.prototype.add; |
1032 oneAndTwo.add(1); | 1051 assertThrows(function() { |
1033 oneAndTwo.add(2); | 1052 ctor.prototype.add = 42; |
1053 new ctor(oneAndTwo.values()); | |
1054 }, TypeError); | |
1055 ctor.prototype.add = originalPrototypeAdd; | |
1056 } | |
1057 TestSetConstructorAddNotCallable(Set); | |
1058 TestSetConstructorAddNotCallable(WeakSet); | |
1034 | 1059 |
1035 | 1060 |
1036 (function TestSetConstructorAddNotCallable() { | 1061 function TestSetConstructorGetAddOnce(ctor) { |
1037 var originalSetPrototypeAdd = Set.prototype.add; | 1062 var originalPrototypeAdd = ctor.prototype.add; |
1038 assertThrows(function() { | |
1039 Set.prototype.add = 42; | |
1040 new Set(oneAndTwo.values()); | |
1041 }, TypeError); | |
1042 Set.prototype.add = originalSetPrototypeAdd; | |
1043 })(); | |
1044 | |
1045 | |
1046 (function TestSetConstructorGetAddOnce() { | |
1047 var originalSetPrototypeAdd = Set.prototype.add; | |
1048 var getAddCount = 0; | 1063 var getAddCount = 0; |
1049 Object.defineProperty(Set.prototype, 'add', { | 1064 Object.defineProperty(ctor.prototype, 'add', { |
1050 get: function() { | 1065 get: function() { |
1051 getAddCount++; | 1066 getAddCount++; |
1052 return function() {}; | 1067 return function() {}; |
1053 } | 1068 } |
1054 }); | 1069 }); |
1055 var s = new Set(oneAndTwo.values()); | 1070 var s = new ctor(oneAndTwo.values()); |
1056 assertEquals(getAddCount, 1); | 1071 assertEquals(1, getAddCount); |
1057 assertEquals(s.size, 0); | 1072 assertSize(0, s); |
1058 Object.defineProperty(Set.prototype, 'add', { | 1073 Object.defineProperty(ctor.prototype, 'add', { |
1059 value: originalSetPrototypeAdd, | 1074 value: originalPrototypeAdd, |
1060 writable: true | 1075 writable: true |
1061 }); | 1076 }); |
1062 })(); | 1077 } |
1078 TestSetConstructorGetAddOnce(Set); | |
1079 TestSetConstructorGetAddOnce(WeakSet); | |
1063 | 1080 |
1064 | 1081 |
1065 (function TestSetConstructorAddReplaced() { | 1082 function TestSetConstructorAddReplaced(ctor) { |
1066 var originalSetPrototypeAdd = Set.prototype.add; | 1083 var originalPrototypeAdd = ctor.prototype.add; |
1067 var addCount = 0; | 1084 var addCount = 0; |
1068 Set.prototype.add = function(value) { | 1085 ctor.prototype.add = function(value) { |
1069 addCount++; | 1086 addCount++; |
1070 originalSetPrototypeAdd.call(this, value); | 1087 originalPrototypeAdd.call(this, value); |
1071 Set.prototype.add = null; | 1088 ctor.prototype.add = null; |
1072 }; | 1089 }; |
1073 var s = new Set(oneAndTwo.values()); | 1090 var s = new ctor(oneAndTwo.keys()); |
1074 assertEquals(addCount, 2); | 1091 assertEquals(2, addCount); |
1075 assertEquals(s.size, 2); | 1092 assertSize(2, s); |
1076 Set.prototype.add = originalSetPrototypeAdd; | 1093 ctor.prototype.add = originalPrototypeAdd; |
1077 })(); | 1094 } |
1095 TestSetConstructorAddReplaced(Set); | |
1096 TestSetConstructorAddReplaced(WeakSet); | |
1078 | 1097 |
1079 | 1098 |
1080 (function TestSetConstructorOrderOfDoneValue() { | 1099 function TestSetConstructorOrderOfDoneValue(ctor) { |
1081 var valueCount = 0, doneCount = 0; | 1100 var valueCount = 0, doneCount = 0; |
1082 var iterator = { | 1101 var iterator = { |
1083 next: function() { | 1102 next: function() { |
1084 return { | 1103 return { |
1085 get value() { | 1104 get value() { |
1086 valueCount++; | 1105 valueCount++; |
1087 }, | 1106 }, |
1088 get done() { | 1107 get done() { |
1089 doneCount++; | 1108 doneCount++; |
1090 throw new Error(); | 1109 throw new Error(); |
1091 } | 1110 } |
1092 }; | 1111 }; |
1093 } | 1112 } |
1094 }; | 1113 }; |
1095 iterator[Symbol.iterator] = function() { | 1114 iterator[Symbol.iterator] = function() { |
1096 return this; | 1115 return this; |
1097 }; | 1116 }; |
1098 assertThrows(function() { | 1117 assertThrows(function() { |
1099 new Set(iterator); | 1118 new ctor(iterator); |
1100 }); | 1119 }); |
1101 assertEquals(doneCount, 1); | 1120 assertEquals(1, doneCount); |
1102 assertEquals(valueCount, 0); | 1121 assertEquals(0, valueCount); |
1103 })(); | 1122 } |
1123 TestSetConstructorOrderOfDoneValue(Set); | |
1124 TestSetConstructorOrderOfDoneValue(WeakSet); | |
1104 | 1125 |
1105 | 1126 |
1106 (function TestSetConstructorNextNotAnObject() { | 1127 function TestSetConstructorNextNotAnObject(ctor) { |
1107 var iterator = { | 1128 var iterator = { |
1108 next: function() { | 1129 next: function() { |
1109 return 'abc'; | 1130 return 'abc'; |
1110 } | 1131 } |
1111 }; | 1132 }; |
1112 iterator[Symbol.iterator] = function() { | 1133 iterator[Symbol.iterator] = function() { |
1113 return this; | 1134 return this; |
1114 }; | 1135 }; |
1115 assertThrows(function() { | 1136 assertThrows(function() { |
1116 new Set(iterator); | 1137 new ctor(iterator); |
1117 }, TypeError); | 1138 }, TypeError); |
1118 })(); | 1139 } |
1140 TestSetConstructorNextNotAnObject(Set); | |
1141 TestSetConstructorNextNotAnObject(WeakSet); | |
1119 | 1142 |
1120 | 1143 |
1121 (function TestMapConstructor() { | 1144 function TestMapConstructor(ctor) { |
1122 var m = new Map(null); | 1145 var m = new ctor(null); |
1123 assertEquals(m.size, 0); | 1146 assertSize(0, m); |
1124 | 1147 |
1125 m = new Map(undefined); | 1148 m = new ctor(undefined); |
1126 assertEquals(m.size, 0); | 1149 assertSize(0, m); |
1127 | 1150 |
1128 // No @@iterator | 1151 // No @@iterator |
1129 assertThrows(function() { | 1152 assertThrows(function() { |
1130 new Map({}); | 1153 new ctor({}); |
1131 }, TypeError); | 1154 }, TypeError); |
1132 | 1155 |
1133 // @@iterator not callable | 1156 // @@iterator not callable |
1134 assertThrows(function() { | 1157 assertThrows(function() { |
1135 var object = {}; | 1158 var object = {}; |
1136 object[Symbol.iterator] = 42; | 1159 object[Symbol.iterator] = 42; |
1137 new Map(object); | 1160 new ctor(object); |
1138 }, TypeError); | 1161 }, TypeError); |
1139 | 1162 |
1140 // @@iterator result not object | 1163 // @@iterator result not object |
1141 assertThrows(function() { | 1164 assertThrows(function() { |
1142 var object = {}; | 1165 var object = {}; |
1143 object[Symbol.iterator] = function() { | 1166 object[Symbol.iterator] = function() { |
1144 return 42; | 1167 return 42; |
1145 }; | 1168 }; |
1146 new Map(object); | 1169 new ctor(object); |
1147 }, TypeError); | 1170 }, TypeError); |
1148 | 1171 |
1149 var m2 = new Map(); | 1172 var m2 = new Map(); |
1150 m2.set(0, 'a'); | 1173 m2.set(k0, 'a'); |
1151 m2.set(1, 'b'); | 1174 m2.set(k1, 'b'); |
1152 m2.set(2, 'c'); | 1175 m2.set(k2, 'c'); |
1153 m = new Map(m2.entries()); | 1176 m = new ctor(m2.entries()); |
1154 assertEquals(m.size, 3); | 1177 assertSize(3, m); |
1155 assertEquals(m.get(0), 'a'); | 1178 assertEquals('a', m.get(k0)); |
1156 assertEquals(m.get(1), 'b'); | 1179 assertEquals('b', m.get(k1)); |
1157 assertEquals(m.get(2), 'c'); | 1180 assertEquals('c', m.get(k2)); |
1158 })(); | 1181 } |
1182 TestMapConstructor(Map); | |
1183 TestMapConstructor(WeakMap); | |
1159 | 1184 |
1160 | 1185 |
1161 (function TestMapConstructorSetNotCallable() { | 1186 function TestMapConstructorSetNotCallable(ctor) { |
1162 var originalMapPrototypeSet = Map.prototype.set; | 1187 var originalPrototypeSet = ctor.prototype.set; |
1163 assertThrows(function() { | 1188 assertThrows(function() { |
1164 Map.prototype.set = 42; | 1189 ctor.prototype.set = 42; |
1165 new Map(oneAndTwo.entries()); | 1190 new ctor(oneAndTwo.entries()); |
1166 }, TypeError); | 1191 }, TypeError); |
1167 Map.prototype.set = originalMapPrototypeSet; | 1192 ctor.prototype.set = originalPrototypeSet; |
1168 })(); | 1193 } |
1194 TestMapConstructorSetNotCallable(Map); | |
1195 TestMapConstructorSetNotCallable(WeakMap); | |
1169 | 1196 |
1170 | 1197 |
1171 (function TestMapConstructorGetAddOnce() { | 1198 function TestMapConstructorGetAddOnce(ctor) { |
1172 var originalMapPrototypeSet = Map.prototype.set; | 1199 var originalPrototypeSet = ctor.prototype.set; |
1173 var getSetCount = 0; | 1200 var getSetCount = 0; |
1174 Object.defineProperty(Map.prototype, 'set', { | 1201 Object.defineProperty(ctor.prototype, 'set', { |
1175 get: function() { | 1202 get: function() { |
1176 getSetCount++; | 1203 getSetCount++; |
1177 return function() {}; | 1204 return function() {}; |
1178 } | 1205 } |
1179 }); | 1206 }); |
1180 var m = new Map(oneAndTwo.entries()); | 1207 var m = new ctor(oneAndTwo.entries()); |
1181 assertEquals(getSetCount, 1); | 1208 assertEquals(1, getSetCount); |
1182 assertEquals(m.size, 0); | 1209 assertSize(0, m); |
1183 Object.defineProperty(Map.prototype, 'set', { | 1210 Object.defineProperty(ctor.prototype, 'set', { |
1184 value: originalMapPrototypeSet, | 1211 value: originalPrototypeSet, |
1185 writable: true | 1212 writable: true |
1186 }); | 1213 }); |
1187 })(); | 1214 } |
1215 TestMapConstructorGetAddOnce(Map); | |
1216 TestMapConstructorGetAddOnce(WeakMap); | |
1188 | 1217 |
1189 | 1218 |
1190 (function TestMapConstructorSetReplaced() { | 1219 function TestMapConstructorSetReplaced(ctor) { |
1191 var originalMapPrototypeSet = Map.prototype.set; | 1220 var originalPrototypeSet = ctor.prototype.set; |
1192 var setCount = 0; | 1221 var setCount = 0; |
1193 Map.prototype.set = function(key, value) { | 1222 ctor.prototype.set = function(key, value) { |
1194 setCount++; | 1223 setCount++; |
1195 originalMapPrototypeSet.call(this, key, value); | 1224 originalPrototypeSet.call(this, key, value); |
1196 Map.prototype.set = null; | 1225 ctor.prototype.set = null; |
1197 }; | 1226 }; |
1198 var m = new Map(oneAndTwo.entries()); | 1227 var m = new ctor(oneAndTwo.entries()); |
1199 assertEquals(setCount, 2); | 1228 assertEquals(2, setCount); |
1200 assertEquals(m.size, 2); | 1229 assertSize(2, m); |
1201 Map.prototype.set = originalMapPrototypeSet; | 1230 ctor.prototype.set = originalPrototypeSet; |
1202 })(); | 1231 } |
1232 TestMapConstructorSetReplaced(Map); | |
1233 TestMapConstructorSetReplaced(WeakMap); | |
1203 | 1234 |
1204 | 1235 |
1205 (function TestMapConstructorOrderOfDoneValue() { | 1236 function TestMapConstructorOrderOfDoneValue(ctor) { |
1206 var valueCount = 0, doneCount = 0; | 1237 var valueCount = 0, doneCount = 0; |
1207 function FakeError() {} | 1238 function FakeError() {} |
1208 var iterator = { | 1239 var iterator = { |
1209 next: function() { | 1240 next: function() { |
1210 return { | 1241 return { |
1211 get value() { | 1242 get value() { |
1212 valueCount++; | 1243 valueCount++; |
1213 }, | 1244 }, |
1214 get done() { | 1245 get done() { |
1215 doneCount++; | 1246 doneCount++; |
1216 throw new FakeError(); | 1247 throw new FakeError(); |
1217 } | 1248 } |
1218 }; | 1249 }; |
1219 } | 1250 } |
1220 }; | 1251 }; |
1221 iterator[Symbol.iterator] = function() { | 1252 iterator[Symbol.iterator] = function() { |
1222 return this; | 1253 return this; |
1223 }; | 1254 }; |
1224 assertThrows(function() { | 1255 assertThrows(function() { |
1225 new Map(iterator); | 1256 new ctor(iterator); |
1226 }, FakeError); | 1257 }, FakeError); |
1227 assertEquals(doneCount, 1); | 1258 assertEquals(1, doneCount); |
1228 assertEquals(valueCount, 0); | 1259 assertEquals(0, valueCount); |
1229 })(); | 1260 } |
1261 TestMapConstructorOrderOfDoneValue(Map); | |
1262 TestMapConstructorOrderOfDoneValue(WeakMap); | |
1230 | 1263 |
1231 | 1264 |
1232 (function TestMapConstructorNextNotAnObject() { | 1265 function TestMapConstructorNextNotAnObject(ctor) { |
1233 var iterator = { | 1266 var iterator = { |
1234 next: function() { | 1267 next: function() { |
1235 return 'abc'; | 1268 return 'abc'; |
1236 } | 1269 } |
1237 }; | 1270 }; |
1238 iterator[Symbol.iterator] = function() { | 1271 iterator[Symbol.iterator] = function() { |
1239 return this; | 1272 return this; |
1240 }; | 1273 }; |
1241 assertThrows(function() { | 1274 assertThrows(function() { |
1242 new Map(iterator); | 1275 new ctor(iterator); |
1243 }, TypeError); | 1276 }, TypeError); |
1244 })(); | 1277 } |
1278 TestMapConstructorNextNotAnObject(Map); | |
1279 TestMapConstructorNextNotAnObject(WeakMap); | |
1245 | 1280 |
1246 | 1281 |
1247 (function TestMapConstructorIteratorNotObjectValues() { | 1282 function TestMapConstructorIteratorNotObjectValues(ctor) { |
1248 assertThrows(function() { | 1283 assertThrows(function() { |
1249 new Map(oneAndTwo.values()); | 1284 new ctor(oneAndTwo.values()); |
1250 }, TypeError); | 1285 }, TypeError); |
1251 })(); | 1286 } |
1287 TestMapConstructorIteratorNotObjectValues(Map); | |
1288 TestMapConstructorIteratorNotObjectValues(WeakMap); | |
OLD | NEW |