Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-arrays --harmony-classes | |
| 6 | |
| 7 (function testArrayConcatArity() { | |
| 8 "use strict"; | |
| 9 assertEquals(1, Array.prototype.concat.length); | |
| 10 })(); | |
| 11 | |
| 12 | |
| 13 (function testArrayConcatNoPrototype() { | |
| 14 "use strict"; | |
| 15 assertEquals(void 0, Array.prototype.concat.prototype); | |
| 16 })(); | |
| 17 | |
| 18 | |
| 19 (function testArrayConcatDescriptor() { | |
| 20 "use strict"; | |
| 21 var desc = Object.getOwnPropertyDescriptor(Array.prototype, 'concat'); | |
| 22 assertEquals(false, desc.enumerable); | |
| 23 })(); | |
| 24 | |
| 25 | |
| 26 (function testConcatArrayLike() { | |
| 27 "use strict"; | |
| 28 var obj = { | |
| 29 "length": 6, | |
| 30 "1": "A", | |
| 31 "3": "B", | |
| 32 "5": "C" | |
| 33 }; | |
| 34 obj[Symbol.isConcatSpreadable] = true; | |
| 35 var obj2 = { length: 3, "0": "0", "1": "1", "2": "2" }; | |
| 36 var arr = ["X", "Y", "Z"]; | |
| 37 assertEquals([void 0, "A", void 0, "B", void 0, "C", | |
| 38 { "length": 3, "0": "0", "1": "1", "2": "2" }, | |
| 39 "X", "Y", "Z"], Array.prototype.concat.call(obj, obj2, arr)); | |
| 40 })(); | |
| 41 | |
| 42 | |
| 43 (function testConcatHoleyArray() { | |
| 44 "use strict"; | |
| 45 var arr = []; | |
| 46 arr[4] = "Item 4"; | |
| 47 arr[8] = "Item 8"; | |
| 48 var arr2 = [".", "!", "?"]; | |
| 49 assertEquals([void 0, void 0, void 0, void 0, "Item 4", void 0, void 0, | |
| 50 void 0, "Item 8", ".", "!", "?"], arr.concat(arr2)); | |
| 51 })(); | |
| 52 | |
| 53 | |
| 54 (function testIsConcatSpreadableGetterThrows() { | |
| 55 "use strict"; | |
| 56 function MyError() {} | |
| 57 var obj = {}; | |
| 58 Object.defineProperty(obj, Symbol.isConcatSpreadable, { | |
| 59 get: function() { throw new MyError(); } | |
| 60 }); | |
| 61 | |
| 62 assertThrows(function() { | |
| 63 [].concat(obj); | |
| 64 }, MyError); | |
| 65 | |
| 66 assertThrows(function() { | |
| 67 Array.prototype.concat.call(obj, 1, 2, 3); | |
| 68 }, MyError); | |
| 69 })(); | |
| 70 | |
| 71 | |
| 72 (function testConcatLengthThrows() { | |
| 73 "use strict"; | |
| 74 function MyError() {} | |
| 75 var obj = {}; | |
| 76 obj[Symbol.isConcatSpreadable] = true; | |
| 77 Object.defineProperty(obj, "length", { | |
| 78 get: function() { throw new MyError(); } | |
| 79 }); | |
| 80 | |
| 81 assertThrows(function() { | |
| 82 [].concat(obj); | |
| 83 }, MyError); | |
| 84 | |
| 85 assertThrows(function() { | |
| 86 Array.prototype.concat.call(obj, 1, 2, 3); | |
| 87 }, MyError); | |
| 88 })(); | |
| 89 | |
| 90 | |
| 91 (function testConcatArraySubclass() { | |
| 92 "use strict"; | |
| 93 // TODO(caitp): when concat is called on instances of classes which extend | |
| 94 // Array, they should: | |
| 95 // | |
| 96 // - return an instance of the class, rather than an Array instance (if from | |
| 97 // same Realm) | |
| 98 // - always treat such classes as concat-spreadable | |
| 99 })(); | |
| 100 | |
| 101 | |
| 102 (function testConcatNonArray() { | |
| 103 "use strict"; | |
| 104 class NonArray { | |
| 105 constructor() { Array.apply(this, arguments); } | |
| 106 }; | |
| 107 | |
| 108 var obj = new NonArray(1,2,3); | |
| 109 var result = Array.prototype.concat.call(obj, 4, 5, 6); | |
| 110 assertEquals(Array, result.constructor); | |
| 111 assertEquals([obj,4,5,6], result); | |
| 112 assertFalse(result instanceof NonArray); | |
| 113 })(); | |
| 114 | |
| 115 | |
| 116 function testConcatTypedArray(type, elems, modulo) { | |
| 117 "use strict"; | |
| 118 var items = new Array(elems); | |
| 119 for (var i = 0; i < elems; ++i) { | |
| 120 items[i] = modulo === false ? i : elems % modulo; | |
| 121 } | |
| 122 var ta = new type(items); | |
| 123 assertEquals([ta, ta], [].concat(ta, ta)); | |
| 124 ta[Symbol.isConcatSpreadable] = true; | |
| 125 assertEquals(items, [].concat(ta)); | |
| 126 } | |
| 127 | |
| 128 (function testConcatSmallTypedArray() { | |
| 129 var max = [2^8, 2^16, 2^32, false, false]; | |
| 130 [ | |
| 131 Uint8Array, | |
| 132 Uint16Array, | |
| 133 Uint32Array, | |
| 134 Float32Array, | |
| 135 Float64Array | |
| 136 ].forEach(function(ctor, i) { | |
| 137 testConcatTypedArray(ctor, 2, max[i]); | |
| 138 }); | |
| 139 })(); | |
| 140 | |
| 141 | |
| 142 (function testConcatLargeTypedArray() { | |
| 143 var max = [2^8, 2^16, 2^32, false, false]; | |
| 144 [ | |
| 145 Uint8Array, | |
| 146 Uint16Array, | |
| 147 Uint32Array, | |
| 148 Float32Array, | |
| 149 Float64Array | |
| 150 ].forEach(function(ctor, i) { | |
| 151 testConcatTypedArray(ctor, 4000, max[i]); | |
| 152 }); | |
| 153 })(); | |
| 154 | |
| 155 | |
| 156 (function testConcatStrictArguments() { | |
| 157 var args = (function() { "use strict"; return arguments; })(1,2,3); | |
| 158 args[Symbol.isConcatSpreadable] = true; | |
| 159 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); | |
| 160 })(); | |
| 161 | |
| 162 | |
| 163 (function testConcatSloppyArguments() { | |
| 164 var args = (function() { return arguments; })(1,2,3); | |
|
Dmitry Lomov (no reviews)
2014/12/11 11:05:47
Yes, this test does not produce SLOPPY_ARGUMENTS_E
caitp (gmail)
2014/12/11 13:09:35
Okay --- I've added a handler for SLOPPY_ARGUMENTS
Dmitry Lomov (no reviews)
2014/12/11 14:01:50
No, please remove the default case, so that compil
caitp (gmail)
2014/12/11 14:41:17
Done --- There were a bunch of missing cases, I'm
| |
| 165 args[Symbol.isConcatSpreadable] = true; | |
| 166 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); | |
| 167 })(); | |
| 168 | |
| 169 | |
| 170 // ES5 tests | |
| 171 (function testArrayConcatES5() { | |
| 172 "use strict"; | |
| 173 var poses; | |
| 174 var pos; | |
| 175 | |
| 176 poses = [140, 4000000000]; | |
| 177 while (pos = poses.shift()) { | |
| 178 var a = new Array(pos); | |
| 179 var array_proto = []; | |
| 180 a.__proto__ = array_proto; | |
| 181 assertEquals(pos, a.length); | |
| 182 a.push('foo'); | |
| 183 assertEquals(pos + 1, a.length); | |
| 184 var b = ['bar']; | |
| 185 var c = a.concat(b); | |
| 186 assertEquals(pos + 2, c.length); | |
| 187 assertEquals("undefined", typeof(c[pos - 1])); | |
| 188 assertEquals("foo", c[pos]); | |
| 189 assertEquals("bar", c[pos + 1]); | |
| 190 | |
| 191 // Can we fool the system by putting a number in a string? | |
| 192 var onetwofour = "124"; | |
| 193 a[onetwofour] = 'doo'; | |
| 194 assertEquals(a[124], 'doo'); | |
| 195 c = a.concat(b); | |
| 196 assertEquals(c[124], 'doo'); | |
| 197 | |
| 198 // If we put a number in the prototype, then the spec says it should be | |
| 199 // copied on concat. | |
| 200 array_proto["123"] = 'baz'; | |
| 201 assertEquals(a[123], 'baz'); | |
| 202 | |
| 203 c = a.concat(b); | |
| 204 assertEquals(pos + 2, c.length); | |
| 205 assertEquals("baz", c[123]); | |
| 206 assertEquals("undefined", typeof(c[pos - 1])); | |
| 207 assertEquals("foo", c[pos]); | |
| 208 assertEquals("bar", c[pos + 1]); | |
| 209 | |
| 210 // When we take the number off the prototype it disappears from a, but | |
| 211 // the concat put it in c itself. | |
| 212 array_proto["123"] = undefined; | |
| 213 assertEquals("undefined", typeof(a[123])); | |
| 214 assertEquals("baz", c[123]); | |
| 215 | |
| 216 // If the element of prototype is shadowed, the element on the instance | |
| 217 // should be copied, but not the one on the prototype. | |
| 218 array_proto[123] = 'baz'; | |
| 219 a[123] = 'xyz'; | |
| 220 assertEquals('xyz', a[123]); | |
| 221 c = a.concat(b); | |
| 222 assertEquals('xyz', c[123]); | |
| 223 | |
| 224 // Non-numeric properties on the prototype or the array shouldn't get | |
| 225 // copied. | |
| 226 array_proto.moe = 'joe'; | |
| 227 a.ben = 'jerry'; | |
| 228 assertEquals(a["moe"], 'joe'); | |
| 229 assertEquals(a["ben"], 'jerry'); | |
| 230 c = a.concat(b); | |
| 231 // ben was not copied | |
| 232 assertEquals("undefined", typeof(c.ben)); | |
| 233 | |
| 234 // When we take moe off the prototype it disappears from all arrays. | |
| 235 array_proto.moe = undefined; | |
| 236 assertEquals("undefined", typeof(c.moe)); | |
| 237 | |
| 238 // Negative indices don't get concated. | |
| 239 a[-1] = 'minus1'; | |
| 240 assertEquals("minus1", a[-1]); | |
| 241 assertEquals("undefined", typeof(a[0xffffffff])); | |
| 242 c = a.concat(b); | |
| 243 assertEquals("undefined", typeof(c[-1])); | |
| 244 assertEquals("undefined", typeof(c[0xffffffff])); | |
| 245 assertEquals(c.length, a.length + 1); | |
| 246 } | |
| 247 | |
| 248 poses = [140, 4000000000]; | |
| 249 while (pos = poses.shift()) { | |
| 250 var a = new Array(pos); | |
| 251 assertEquals(pos, a.length); | |
| 252 a.push('foo'); | |
| 253 assertEquals(pos + 1, a.length); | |
| 254 var b = ['bar']; | |
| 255 var c = a.concat(b); | |
| 256 assertEquals(pos + 2, c.length); | |
| 257 assertEquals("undefined", typeof(c[pos - 1])); | |
| 258 assertEquals("foo", c[pos]); | |
| 259 assertEquals("bar", c[pos + 1]); | |
| 260 | |
| 261 // Can we fool the system by putting a number in a string? | |
| 262 var onetwofour = "124"; | |
| 263 a[onetwofour] = 'doo'; | |
| 264 assertEquals(a[124], 'doo'); | |
| 265 c = a.concat(b); | |
| 266 assertEquals(c[124], 'doo'); | |
| 267 | |
| 268 // If we put a number in the prototype, then the spec says it should be | |
| 269 // copied on concat. | |
| 270 Array.prototype["123"] = 'baz'; | |
| 271 assertEquals(a[123], 'baz'); | |
| 272 | |
| 273 c = a.concat(b); | |
| 274 assertEquals(pos + 2, c.length); | |
| 275 assertEquals("baz", c[123]); | |
| 276 assertEquals("undefined", typeof(c[pos - 1])); | |
| 277 assertEquals("foo", c[pos]); | |
| 278 assertEquals("bar", c[pos + 1]); | |
| 279 | |
| 280 // When we take the number off the prototype it disappears from a, but | |
| 281 // the concat put it in c itself. | |
| 282 Array.prototype["123"] = undefined; | |
| 283 assertEquals("undefined", typeof(a[123])); | |
| 284 assertEquals("baz", c[123]); | |
| 285 | |
| 286 // If the element of prototype is shadowed, the element on the instance | |
| 287 // should be copied, but not the one on the prototype. | |
| 288 Array.prototype[123] = 'baz'; | |
| 289 a[123] = 'xyz'; | |
| 290 assertEquals('xyz', a[123]); | |
| 291 c = a.concat(b); | |
| 292 assertEquals('xyz', c[123]); | |
| 293 | |
| 294 // Non-numeric properties on the prototype or the array shouldn't get | |
| 295 // copied. | |
| 296 Array.prototype.moe = 'joe'; | |
| 297 a.ben = 'jerry'; | |
| 298 assertEquals(a["moe"], 'joe'); | |
| 299 assertEquals(a["ben"], 'jerry'); | |
| 300 c = a.concat(b); | |
| 301 // ben was not copied | |
| 302 assertEquals("undefined", typeof(c.ben)); | |
| 303 // moe was not copied, but we can see it through the prototype | |
| 304 assertEquals("joe", c.moe); | |
| 305 | |
| 306 // When we take moe off the prototype it disappears from all arrays. | |
| 307 Array.prototype.moe = undefined; | |
| 308 assertEquals("undefined", typeof(c.moe)); | |
| 309 | |
| 310 // Negative indices don't get concated. | |
| 311 a[-1] = 'minus1'; | |
| 312 assertEquals("minus1", a[-1]); | |
| 313 assertEquals("undefined", typeof(a[0xffffffff])); | |
| 314 c = a.concat(b); | |
| 315 assertEquals("undefined", typeof(c[-1])); | |
| 316 assertEquals("undefined", typeof(c[0xffffffff])); | |
| 317 assertEquals(c.length, a.length + 1); | |
| 318 | |
| 319 } | |
| 320 | |
| 321 a = []; | |
| 322 c = a.concat('Hello'); | |
| 323 assertEquals(1, c.length); | |
| 324 assertEquals("Hello", c[0]); | |
| 325 assertEquals("Hello", c.toString()); | |
| 326 | |
| 327 // Check that concat preserves holes. | |
| 328 var holey = [void 0,'a',,'c'].concat(['d',,'f',[0,,2],void 0]) | |
| 329 assertEquals(9, holey.length); // hole in embedded array is ignored | |
| 330 for (var i = 0; i < holey.length; i++) { | |
| 331 if (i == 2 || i == 5) { | |
| 332 assertFalse(i in holey); | |
| 333 } else { | |
| 334 assertTrue(i in holey); | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 // Polluted prototype from prior tests. | |
| 339 delete Array.prototype[123]; | |
| 340 | |
| 341 // Check that concat reads getters in the correct order. | |
| 342 var arr1 = [,2]; | |
| 343 var arr2 = [1,3]; | |
| 344 var r1 = [].concat(arr1, arr2); // [,2,1,3] | |
| 345 assertEquals([,2,1,3], r1); | |
| 346 | |
| 347 // Make first array change length of second array. | |
| 348 Object.defineProperty(arr1, 0, {get: function() { | |
| 349 arr2.push("X"); | |
| 350 return undefined; | |
| 351 }, configurable: true}) | |
| 352 var r2 = [].concat(arr1, arr2); // [undefined,2,1,3,"X"] | |
| 353 assertEquals([undefined,2,1,3,"X"], r2); | |
| 354 | |
| 355 // Make first array change length of second array massively. | |
| 356 arr2.length = 2; | |
| 357 Object.defineProperty(arr1, 0, {get: function() { | |
| 358 arr2[500000] = "X"; | |
| 359 return undefined; | |
| 360 }, configurable: true}) | |
| 361 var r3 = [].concat(arr1, arr2); // [undefined,2,1,3,"X"] | |
| 362 var expected = [undefined,2,1,3]; | |
| 363 expected[500000 + 2] = "X"; | |
| 364 | |
| 365 assertEquals(expected, r3); | |
| 366 | |
| 367 var arr3 = []; | |
| 368 var trace = []; | |
| 369 var expectedTrace = [] | |
| 370 function mkGetter(i) { return function() { trace.push(i); }; } | |
| 371 arr3.length = 10000; | |
| 372 for (var i = 0; i < 100; i++) { | |
| 373 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); | |
| 374 expectedTrace[i] = i; | |
| 375 expectedTrace[100 + i] = i; | |
| 376 } | |
| 377 var r4 = [0].concat(arr3, arr3); | |
| 378 assertEquals(1 + arr3.length * 2, r4.length); | |
| 379 assertEquals(expectedTrace, trace); | |
| 380 })(); | |
| OLD | NEW |