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.
5 // Flags: --harmony-rest-parameters
7 (function testRestIndex() {
8 assertEquals(5, (function(...args) { return args.length; })(1,2,3,4,5));
9 assertEquals(4, (function(a, ...args) { return args.length; })(1,2,3,4,5));
10 assertEquals(3, (function(a, b, ...args) { return args.length; })(1,2,3,4,5));
11 assertEquals(2, (function(a, b, c, ...args) {
12 return args.length; })(1,2,3,4,5));
13 assertEquals(1, (function(a, b, c, d, ...args) {
14 return args.length; })(1,2,3,4,5));
15 assertEquals(0, (function(a, b, c, d, e, ...args) {
16 return args.length; })(1,2,3,4,5));
19 function strictTest(a, b, ...c) {
21 assertEquals(Array, c.constructor);
22 assertTrue(Array.isArray(c));
24 var expectedLength = arguments.length >= 3 ? arguments.length - 2 : 0;
25 assertEquals(expectedLength, c.length);
27 for (var i = 2, j = 0; i < arguments.length; ++i) {
28 assertEquals(c[j++], arguments[i]);
32 function sloppyTest(a, b, ...c) {
33 assertEquals(Array, c.constructor);
34 assertTrue(Array.isArray(c));
36 var expectedLength = arguments.length >= 3 ? arguments.length - 2 : 0;
37 assertEquals(expectedLength, c.length);
39 for (var i = 2, j = 0; i < arguments.length; ++i) {
40 assertEquals(c[j++], arguments[i]);
50 (function testStrictRestParamArity() {
51 assertEquals(2, strictTest.length);
52 assertEquals(2, O.strict.length);
56 (function testRestParamsStrictMode() {
59 strictTest(1, 2, 3, 4, 5, 6);
63 O.strict(1, 2, 3, 4, 5, 6);
68 (function testRestParamsStrictModeApply() {
69 strictTest.apply(null, []);
70 strictTest.apply(null, [1, 2]);
71 strictTest.apply(null, [1, 2, 3, 4, 5, 6]);
72 strictTest.apply(null, [1, 2, 3]);
73 O.strict.apply(O, []);
74 O.strict.apply(O, [1, 2]);
75 O.strict.apply(O, [1, 2, 3, 4, 5, 6]);
76 O.strict.apply(O, [1, 2, 3]);
80 (function testRestParamsStrictModeCall() {
81 strictTest.call(null);
82 strictTest.call(null, 1, 2);
83 strictTest.call(null, 1, 2, 3, 4, 5, 6);
84 strictTest.call(null, 1, 2, 3);
86 O.strict.call(O, 1, 2);
87 O.strict.call(O, 1, 2, 3, 4, 5, 6);
88 O.strict.call(O, 1, 2, 3);
92 (function testsloppyRestParamArity() {
93 assertEquals(2, sloppyTest.length);
94 assertEquals(2, O.sloppy.length);
98 (function testRestParamssloppyMode() {
101 sloppyTest(1, 2, 3, 4, 5, 6);
105 O.sloppy(1, 2, 3, 4, 5, 6);
110 (function testRestParamssloppyModeApply() {
111 sloppyTest.apply(null, []);
112 sloppyTest.apply(null, [1, 2]);
113 sloppyTest.apply(null, [1, 2, 3, 4, 5, 6]);
114 sloppyTest.apply(null, [1, 2, 3]);
115 O.sloppy.apply(O, []);
116 O.sloppy.apply(O, [1, 2]);
117 O.sloppy.apply(O, [1, 2, 3, 4, 5, 6]);
118 O.sloppy.apply(O, [1, 2, 3]);
122 (function testRestParamssloppyModeCall() {
123 sloppyTest.call(null);
124 sloppyTest.call(null, 1, 2);
125 sloppyTest.call(null, 1, 2, 3, 4, 5, 6);
126 sloppyTest.call(null, 1, 2, 3);
128 O.sloppy.call(O, 1, 2);
129 O.sloppy.call(O, 1, 2, 3, 4, 5, 6);
130 O.sloppy.call(O, 1, 2, 3);
134 (function testUnmappedArguments() {
135 // Strict/Unmapped arguments should always be used for functions with rest
137 assertThrows(function(...rest) { return arguments.caller; }, TypeError);
138 assertThrows(function(...rest) { return arguments.callee; }, TypeError);
139 // TODO(caitp): figure out why this doesn't throw sometimes, even though the
140 // getter always does =)
141 // assertThrows(function(...rest) { arguments.caller = 1; }, TypeError);
142 // assertThrows(function(...rest) { arguments.callee = 1; }, TypeError);
146 (function testNoAliasArgumentsStrict() {
147 function strictF(a, ...rest) {
152 assertArrayEquals([4, 5], rest);
158 (function testNoAliasArgumentsSloppy() {
159 function sloppyF(a, ...rest) {
163 assertArrayEquals([4, 5], rest);
169 /* TODO(caitp): support arrow functions (blocked on spread operator support)
170 (function testRestParamsArrowFunctions() {
173 var fn = (a, b, ...c) => c;
174 assertEquals([], fn());
175 assertEquals([], fn(1, 2));
176 assertEquals([3], fn(1, 2, 3));
177 assertEquals([3, 4], fn(1, 2, 3, 4));
178 assertEquals([3, 4, 5], fn(1, 2, 3, 4, 5));
179 assertThrows("var x = ...y => y;", SyntaxError);
180 assertEquals([], ((...args) => args)());
181 assertEquals([1,2,3], ((...args) => args)(1,2,3));