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-rest-parameters | |
6 | |
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)); | |
17 })(); | |
18 | |
19 | |
20 var strictTest = (function() { | |
21 "use strict"; | |
22 return function strictTest(a, b, ...c) { | |
23 assertEquals(Array, c.constructor); | |
24 assertTrue(Array.isArray(c)); | |
25 | |
26 var expectedLength = arguments.length >= 3 ? arguments.length - 2 : 0; | |
27 assertEquals(expectedLength, c.length); | |
28 | |
29 for (var i = 2, j = 0; i < arguments.length; ++i) { | |
30 assertEquals(c[j++], arguments[i]); | |
31 } | |
32 }; | |
33 })(); | |
34 | |
35 | |
36 function sloppyTest(a, b, ...c) { | |
37 assertEquals(Array, c.constructor); | |
38 assertTrue(Array.isArray(c)); | |
39 | |
40 var expectedLength = arguments.length >= 3 ? arguments.length - 2 : 0; | |
41 assertEquals(expectedLength, c.length); | |
42 | |
43 for (var i = 2, j = 0; i < arguments.length; ++i) { | |
44 assertEquals(c[j++], arguments[i]); | |
45 } | |
46 } | |
47 | |
48 | |
49 var O = { | |
50 strict: strictTest, | |
51 sloppy: sloppyTest | |
52 }; | |
53 | |
54 (function testStrictRestParamArity() { | |
55 assertEquals(2, strictTest.length); | |
56 assertEquals(2, O.strict.length); | |
57 })(); | |
58 | |
59 | |
60 (function testRestParamsStrictMode() { | |
61 strictTest(); | |
62 strictTest(1, 2); | |
63 strictTest(1, 2, 3, 4, 5, 6); | |
64 strictTest(1, 2, 3); | |
65 O.strict(); | |
66 O.strict(1, 2); | |
67 O.strict(1, 2, 3, 4, 5, 6); | |
68 O.strict(1, 2, 3); | |
69 })(); | |
70 | |
71 | |
72 (function testRestParamsStrictModeApply() { | |
73 strictTest.apply(null, []); | |
74 strictTest.apply(null, [1, 2]); | |
75 strictTest.apply(null, [1, 2, 3, 4, 5, 6]); | |
76 strictTest.apply(null, [1, 2, 3]); | |
77 O.strict.apply(O, []); | |
78 O.strict.apply(O, [1, 2]); | |
79 O.strict.apply(O, [1, 2, 3, 4, 5, 6]); | |
80 O.strict.apply(O, [1, 2, 3]); | |
81 })(); | |
82 | |
83 | |
84 (function testRestParamsStrictModeCall() { | |
85 strictTest.call(null); | |
86 strictTest.call(null, 1, 2); | |
87 strictTest.call(null, 1, 2, 3, 4, 5, 6); | |
88 strictTest.call(null, 1, 2, 3); | |
89 O.strict.call(O); | |
90 O.strict.call(O, 1, 2); | |
91 O.strict.call(O, 1, 2, 3, 4, 5, 6); | |
92 O.strict.call(O, 1, 2, 3); | |
93 })(); | |
94 | |
95 | |
96 (function testsloppyRestParamArity() { | |
97 assertEquals(2, sloppyTest.length); | |
98 assertEquals(2, O.sloppy.length); | |
99 })(); | |
100 | |
101 | |
102 (function testRestParamssloppyMode() { | |
103 sloppyTest(); | |
104 sloppyTest(1, 2); | |
105 sloppyTest(1, 2, 3, 4, 5, 6); | |
106 sloppyTest(1, 2, 3); | |
107 O.sloppy(); | |
108 O.sloppy(1, 2); | |
109 O.sloppy(1, 2, 3, 4, 5, 6); | |
110 O.sloppy(1, 2, 3); | |
111 })(); | |
112 | |
113 | |
114 (function testRestParamssloppyModeApply() { | |
115 sloppyTest.apply(null, []); | |
116 sloppyTest.apply(null, [1, 2]); | |
117 sloppyTest.apply(null, [1, 2, 3, 4, 5, 6]); | |
118 sloppyTest.apply(null, [1, 2, 3]); | |
119 O.sloppy.apply(O, []); | |
120 O.sloppy.apply(O, [1, 2]); | |
121 O.sloppy.apply(O, [1, 2, 3, 4, 5, 6]); | |
122 O.sloppy.apply(O, [1, 2, 3]); | |
123 })(); | |
124 | |
125 | |
126 (function testRestParamssloppyModeCall() { | |
127 sloppyTest.call(null); | |
128 sloppyTest.call(null, 1, 2); | |
129 sloppyTest.call(null, 1, 2, 3, 4, 5, 6); | |
130 sloppyTest.call(null, 1, 2, 3); | |
131 O.sloppy.call(O); | |
132 O.sloppy.call(O, 1, 2); | |
133 O.sloppy.call(O, 1, 2, 3, 4, 5, 6); | |
134 O.sloppy.call(O, 1, 2, 3); | |
135 })(); | |
136 | |
137 | |
138 (function testUnmappedArguments() { | |
139 // Strict/Unmapped arguments should always be used for functions with rest | |
140 // parameters | |
141 assertThrows(function(...rest) { return arguments.caller; }, TypeError); | |
142 assertThrows(function(...rest) { return arguments.callee; }, TypeError); | |
143 // TODO(caitp): figure out why this doesn't throw sometimes, even though the | |
144 // getter always does =) | |
145 // assertThrows(function(...rest) { arguments.caller = 1; }, TypeError); | |
146 // assertThrows(function(...rest) { arguments.callee = 1; }, TypeError); | |
147 })(); | |
148 | |
149 | |
150 (function testNoAliasArgumentsStrict() { | |
151 ((function() { | |
152 "use strict"; | |
153 return (function strictF(a, ...rest) { | |
154 arguments[0] = 1; | |
155 assertEquals(3, a); | |
156 arguments[1] = 2; | |
157 assertArrayEquals([4, 5], rest); | |
158 }); | |
159 })())(3, 4, 5); | |
160 })(); | |
161 | |
162 | |
163 (function testNoAliasArgumentsSloppy() { | |
164 function sloppyF(a, ...rest) { | |
165 arguments[0] = 1; | |
166 assertEquals(3, a); | |
167 arguments[1] = 2; | |
168 assertArrayEquals([4, 5], rest); | |
169 } | |
170 sloppyF(3, 4, 5); | |
171 })(); | |
172 | |
173 | |
174 (function testRestParamsWithNewTarget() { | |
175 "use strict"; | |
176 class Base { | |
177 constructor(...a) { | |
178 this.base = a; | |
179 assertEquals(arguments.length, a.length); | |
180 var args = []; | |
181 for (var i = 0; i < arguments.length; ++i) { | |
182 args.push(arguments[i]); | |
183 } | |
184 assertEquals(args, a); | |
185 } | |
186 } | |
187 class Child extends Base { | |
188 constructor(...b) { | |
189 super(1, 2, 3); | |
190 this.child = b; | |
191 assertEquals(arguments.length, b.length); | |
192 var args = []; | |
193 for (var i = 0; i < arguments.length; ++i) { | |
194 args.push(arguments[i]); | |
195 } | |
196 assertEquals(args, b); | |
197 } | |
198 } | |
199 | |
200 var c = new Child(1, 2, 3); | |
201 assertEquals([1, 2, 3], c.child); | |
202 assertEquals([1, 2, 3], c.base); | |
203 })(); | |
204 | |
205 (function TestDirectiveThrows() { | |
206 "use strict"; | |
207 | |
208 assertThrows( | |
209 function(){ eval("function(...rest){'use strict';}") }, SyntaxError); | |
210 assertThrows(function(){ eval("(...rest) => {'use strict';}") }, SyntaxError); | |
211 assertThrows( | |
212 function(){ eval("(class{foo(...rest) {'use strict';}});") }, SyntaxError); | |
213 | |
214 assertThrows( | |
215 function(){ eval("function(a, ...rest){'use strict';}") }, SyntaxError); | |
216 assertThrows( | |
217 function(){ eval("(a, ...rest) => {'use strict';}") }, SyntaxError); | |
218 assertThrows( | |
219 function(){ eval("(class{foo(a, ...rest) {'use strict';}});") }, | |
220 SyntaxError); | |
221 })(); | |
OLD | NEW |