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 (function testRestIndex() { 6 assertEquals(5, ((...args) => args.length)(1,2,3,4,5)); 7 assertEquals(4, ((a, ...args) => args.length)(1,2,3,4,5)); 8 assertEquals(3, ((a, b, ...args) => args.length)(1,2,3,4,5)); 9 assertEquals(2, ((a, b, c, ...args) => args.length)(1,2,3,4,5)); 10 assertEquals(1, ((a, b, c, d, ...args) => args.length)(1,2,3,4,5)); 11 assertEquals(0, ((a, b, c, d, e, ...args) => args.length)(1,2,3,4,5)); 12 })(); 13 14 // strictTest and sloppyTest should be called with descending natural 15 // numbers, as in: 16 // 17 // strictTest(6,5,4,3,2,1) 18 // 19 var strictTest = (() => { 20 "use strict"; 21 return (a, b, ...c) => { 22 assertEquals(Array, c.constructor); 23 assertTrue(Array.isArray(c)); 24 25 var expectedLength = (a === undefined) ? 0 : a - 2; 26 assertEquals(expectedLength, c.length); 27 28 for (var i = 2; i < a; ++i) { 29 assertEquals(c[i - 2], a - i); 30 } 31 }; 32 })(); 33 34 var sloppyTest = (a, b, ...c) => { 35 assertEquals(Array, c.constructor); 36 assertTrue(Array.isArray(c)); 37 38 var expectedLength = (a === undefined) ? 0 : a - 2; 39 assertEquals(expectedLength, c.length); 40 41 for (var i = 2; i < a; ++i) { 42 assertEquals(c[i - 2], a - i); 43 } 44 } 45 46 47 var O = { 48 strict: strictTest, 49 sloppy: sloppyTest 50 }; 51 52 (function testStrictRestParamArity() { 53 assertEquals(2, strictTest.length); 54 assertEquals(2, O.strict.length); 55 })(); 56 57 58 (function testRestParamsStrictMode() { 59 strictTest(); 60 strictTest(2, 1); 61 strictTest(6, 5, 4, 3, 2, 1); 62 strictTest(3, 2, 1); 63 O.strict(); 64 O.strict(2, 1); 65 O.strict(6, 5, 4, 3, 2, 1); 66 O.strict(3, 2, 1); 67 })(); 68 69 70 (function testRestParamsStrictModeApply() { 71 strictTest.apply(null, []); 72 strictTest.apply(null, [2, 1]); 73 strictTest.apply(null, [6, 5, 4, 3, 2, 1]); 74 strictTest.apply(null, [3, 2, 1]); 75 O.strict.apply(O, []); 76 O.strict.apply(O, [2, 1]); 77 O.strict.apply(O, [6, 5, 4, 3, 2, 1]); 78 O.strict.apply(O, [3, 2, 1]); 79 })(); 80 81 82 (function testRestParamsStrictModeCall() { 83 strictTest.call(null); 84 strictTest.call(null, 2, 1); 85 strictTest.call(null, 6, 5, 4, 3, 2, 1); 86 strictTest.call(null, 3, 2, 1); 87 O.strict.call(O); 88 O.strict.call(O, 2, 1); 89 O.strict.call(O, 6, 5, 4, 3, 2, 1); 90 O.strict.call(O, 3, 2, 1); 91 })(); 92 93 94 (function testsloppyRestParamArity() { 95 assertEquals(2, sloppyTest.length); 96 assertEquals(2, O.sloppy.length); 97 })(); 98 99 100 (function testRestParamssloppyMode() { 101 sloppyTest(); 102 sloppyTest(2, 1); 103 sloppyTest(6, 5, 4, 3, 2, 1); 104 sloppyTest(3, 2, 1); 105 O.sloppy(); 106 O.sloppy(2, 1); 107 O.sloppy(6, 5, 4, 3, 2, 1); 108 O.sloppy(3, 2, 1); 109 })(); 110 111 112 (function testRestParamssloppyModeApply() { 113 sloppyTest.apply(null, []); 114 sloppyTest.apply(null, [2, 1]); 115 sloppyTest.apply(null, [6, 5, 4, 3, 2, 1]); 116 sloppyTest.apply(null, [3, 2, 1]); 117 O.sloppy.apply(O, []); 118 O.sloppy.apply(O, [2, 1]); 119 O.sloppy.apply(O, [6, 5, 4, 3, 2, 1]); 120 O.sloppy.apply(O, [3, 2, 1]); 121 })(); 122 123 124 (function testRestParamssloppyModeCall() { 125 sloppyTest.call(null); 126 sloppyTest.call(null, 2, 1); 127 sloppyTest.call(null, 6, 5, 4, 3, 2, 1); 128 sloppyTest.call(null, 3, 2, 1); 129 O.sloppy.call(O); 130 O.sloppy.call(O, 2, 1); 131 O.sloppy.call(O, 6, 5, 4, 3, 2, 1); 132 O.sloppy.call(O, 3, 2, 1); 133 })(); 134 135 136 (function testUnmappedArguments() { 137 // Normal functions make their arguments object unmapped, but arrow 138 // functions don't have an arguments object anyway. Check that the 139 // right thing happens for arguments in arrow functions with rest 140 // parameters. 141 assertSame(arguments, ((...rest) => arguments)()); 142 })(); 143