apply-native-spreadnew-newtarget.js (1435B)
1 load(libdir + "array-compare.js"); 2 3 const xs = [ 4 // Zero arguments. 5 [], 6 7 // Single argument. 8 [1], 9 10 // Few arguments. Even number of arguments. 11 [1, 2], 12 13 // Few arguments. Odd number of arguments. 14 [1, 2, 3], 15 16 // Many arguments. Even number of arguments. 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], 18 19 // Many arguments. Odd number of arguments. 20 [1, 2, 3, 4, 5, 6, 7, 8, 9], 21 ]; 22 23 class ArrayWithExplicitConstructor extends Array { 24 constructor(...args) { 25 super(...args); 26 } 27 } 28 29 class ArrayWithImplicitConstructor extends Array { 30 constructor(...args) { 31 super(...args); 32 } 33 } 34 35 function f(...x) { 36 return new ArrayWithExplicitConstructor(...x); 37 } 38 39 function g(...x) { 40 return new ArrayWithImplicitConstructor(...x); 41 } 42 43 // Don't inline |f| and |g| into the top-level script. 44 with ({}) ; 45 46 for (let i = 0; i < 400; ++i) { 47 let x = xs[i % xs.length]; 48 49 // NB: Array(1) creates the array `[,]`. 50 let expected = x.length !== 1 ? x : [,]; 51 52 let result = f.apply(null, x); 53 assertEq(arraysEqual(result, expected), true); 54 assertEq(Object.getPrototypeOf(result), ArrayWithExplicitConstructor.prototype); 55 } 56 57 for (let i = 0; i < 400; ++i) { 58 let x = xs[i % xs.length]; 59 60 // NB: Array(1) creates the array `[,]`. 61 let expected = x.length !== 1 ? x : [,]; 62 63 let result = g.apply(null, x); 64 assertEq(arraysEqual(result, expected), true); 65 assertEq(Object.getPrototypeOf(result), ArrayWithImplicitConstructor.prototype); 66 }