1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! §10.2.11 and §15.1 — the parameters that are not simply names.
//!
//! Checked against V8 first. A default and a rest parameter both make a parameter list *not
//! simple* (§15.1.4), and that one fact decides three other things: how many arguments the
//! function says it needs, which arguments object a call builds, and whether `arguments.callee`
//! may be read at all.
use super::*;
#[test]
fn a_default_fills_in_for_undefined_and_for_nothing_at_all() {
assert_eq!(run("(function (a = 1) { return a; })()"), "1");
assert_eq!(run("(function (a = 1) { return a; })(2)"), "2");
// §10.2.11 applies the default when the parameter *is* `undefined`, so passing one explicitly
// takes it too — `f()` and `f(undefined)` agree, and that is the rule rather than a shortcut.
assert_eq!(run("(function (a = 1) { return a; })(undefined)"), "1");
// …and nothing else is `undefined`. Every other falsy value is passed through, which is the
// difference between this and `a = a || 1`.
for given in ["null", "0", "''", "false", "NaN"] {
assert_eq!(
run(&format!(
"(function (a = 1) {{ return a; }})({given}) === 1"
)),
"false"
);
}
// A default is an expression, evaluated inside the callee, so it may name the parameters to
// its left and may call anything.
assert_eq!(run("(function (a, b = a + 1) { return b; })(5)"), "6");
assert_eq!(run("(function (a = 1, b = a * 2) { return b; })()"), "2");
assert_eq!(
run("(function (a = (function () { return 8; })()) { return a; })()"),
"8"
);
// A later parameter without a default is still positional, so a `undefined` in front of it
// takes the default and it takes its own argument.
assert_eq!(
run("(function (a = 1, b) { return b; })(undefined, 5)"),
"5"
);
assert_eq!(
run("(function (a = 1, b = 2, c = 3) { return a + b + c; })(undefined, 5)"),
"9"
);
assert_eq!(
run("(function () { var g = (a = 4) => a; return g(); })()"),
"4"
);
}
#[test]
fn a_rest_parameter_is_an_ordinary_array_of_what_was_left_over() {
assert_eq!(run("(function (...r) { return r.length; })(1, 2, 3)"), "3");
assert_eq!(run("(function (...r) { return r.length; })()"), "0");
assert_eq!(
run("(function (a, ...r) { return r.join(','); })(1, 2, 3)"),
"2,3"
);
assert_eq!(run("(function (a, ...r) { return a; })(1, 2, 3)"), "1");
// Fewer arguments than named parameters leaves it empty rather than short — there is nothing
// past the end to collect.
assert_eq!(run("(function (a, b, ...r) { return r.length; })(1)"), "0");
// An *ordinary* Array, not something array-like: `arguments` is the array-like and this is
// the thing it was added to replace.
assert_eq!(
run("(function (...r) { return Array.isArray(r); })()"),
"true"
);
assert_eq!(
run("(function (...r) { return r instanceof Array; })()"),
"true"
);
assert_eq!(
run("(function (...r) { return Object.getPrototypeOf(r) === Array.prototype; })()"),
"true"
);
assert_eq!(
run("(function (...r) { r.push(9); return r.length; })(1)"),
"2"
);
assert_eq!(
run("(function (...r) { return r === arguments; })(1)"),
"false"
);
assert_eq!(
run("(function () { var g = (...r) => r.length; return g(1, 2); })()"),
"2"
);
}
#[test]
fn length_is_what_the_function_says_it_needs_and_not_how_many_slots_it_has() {
// §20.2.4.1 — the count stops at the first default and never counts a rest parameter. A
// reader who expects 3 from the second row is counting slots, which is a different number
// and the one the *call* uses.
assert_eq!(run("(function (a, b) {}).length"), "2");
assert_eq!(run("(function (a, b = 1, c) {}).length"), "1");
assert_eq!(run("(function (...r) {}).length"), "0");
assert_eq!(run("(function (a, ...r) {}).length"), "1");
assert_eq!(run("(function () {}).length"), "0");
assert_eq!(run("(function (a = 1) { return a; }).length"), "0");
// Not writable and not enumerable, and *configurable* — which is the one that lets a wrapper
// give itself the length of what it wraps.
assert_eq!(
run("Object.getOwnPropertyDescriptor(function (a, b) {}, 'length').writable"),
"false"
);
assert_eq!(
run("Object.getOwnPropertyDescriptor(function (a, b) {}, 'length').enumerable"),
"false"
);
assert_eq!(
run("Object.getOwnPropertyDescriptor(function (a, b) {}, 'length').configurable"),
"true"
);
}
#[test]
fn a_list_that_is_not_simple_gets_an_arguments_object_that_joins_nothing() {
// §10.2.11 step 22 — a *mapped* arguments object only for a simple parameter list. This is
// the row it is all about: with a default anywhere in the list, an index and its parameter
// are two variables again.
assert_eq!(
run("(function (a) { arguments[0] = 7; return a; })(3)"),
"7"
);
assert_eq!(
run("(function (a = 1) { arguments[0] = 7; return a; })(3)"),
"3"
);
assert_eq!(
run("(function (a = 1) { a = 7; return arguments[0]; })(3)"),
"3"
);
assert_eq!(
run("(function (a = 1) { delete arguments[0]; return a; })(3)"),
"3"
);
// It is still an arguments object in every other way — the values it holds are what the call
// actually passed, which is why a default that filled in for a missing argument is not there.
assert_eq!(
run("(function (a = 1) { return arguments.length; })()"),
"0"
);
assert_eq!(
run("(function (a = 1) { return arguments.length; })(9)"),
"1"
);
assert_eq!(
run("(function (a = 1) { return typeof arguments[0]; })()"),
"undefined"
);
assert_eq!(run("(function (a = 1) { return arguments[0]; })(3)"), "3");
assert_eq!(
run("(function (...r) { return arguments.length; })(1, 2)"),
"2"
);
// §20.1.3.6 step 8 tags it `Arguments` because it *has* a parameter map, not because the map
// joins anything — so an unmapped one is tagged the same.
assert_eq!(
run("(function (a = 1) { return Object.prototype.toString.call(arguments); })()"),
"[object Arguments]"
);
assert_eq!(
run("(function (a = 1) { return typeof arguments; })()"),
"object"
);
// §10.4.4.6 step 6 — and its `callee` is poisoned with %ThrowTypeError%, both halves. A
// function with a default is ES2015 code, and `arguments.callee` is what ES2015 closed off.
assert_eq!(
run("(function (a = 1) { try { return arguments.callee; } \
catch (e) { return e.constructor.name; } })(1)"),
"TypeError"
);
// …and the property itself is not enumerable and not configurable, so `for`-`in` walks the
// indices as it does on a mapped object and nothing can define the poison away.
let describe =
"(function (a = 1) { var d = Object.getOwnPropertyDescriptor(arguments, 'callee'); return ";
assert_eq!(run(&format!("{describe}d.enumerable; }})()")), "false");
assert_eq!(run(&format!("{describe}d.configurable; }})()")), "false");
assert_eq!(run(&format!("{describe}typeof d.get; }})()")), "function");
// Both halves are the *same* function object — §10.2.4.1 has one %ThrowTypeError% per realm,
// which is what makes two of them comparable with `===`.
assert_eq!(run(&format!("{describe}d.get === d.set; }})()")), "true");
assert_eq!(
run(
"(function (a = 1) { var r = ''; for (var k in arguments) { r += k; } return r; })(1, 2)"
),
"01"
);
assert_eq!(
run("(function (a = 1) { return Object.getOwnPropertyNames(arguments).join(','); })(1)"),
"0,length,callee"
);
// …while a simple list still has it, and it is the function.
assert_eq!(
run("(function (a) { return arguments.callee !== undefined; })(1)"),
"true"
);
}
#[test]
fn a_parameter_may_be_a_pattern_and_is_taken_apart_after_its_default() {
assert_eq!(run("(function ({a}) { return a; })({a: 1})"), "1");
assert_eq!(
run("(function ({a, b}) { return a + b; })({a: 1, b: 2})"),
"3"
);
assert_eq!(run("(function ([a, b]) { return a + b; })([1, 2])"), "3");
assert_eq!(run("(function ({a: x}) { return x; })({a: 5})"), "5");
assert_eq!(run("(function ({a = 7}) { return a; })({})"), "7");
assert_eq!(run("(function ({a: {b}}) { return b; })({a: {b: 9}})"), "9");
assert_eq!(run("(function ([{a}]) { return a; })([{a: 6}])"), "6");
assert_eq!(
run("(function ([a, ...r]) { return a + ':' + r.join(','); })([1, 2, 3])"),
"1:2,3"
);
assert_eq!(run("(function (x, {a}) { return x + a; })(1, {a: 2})"), "3");
assert_eq!(
run("(function ({a}, [b]) { return a + b; })({a: 1}, [2])"),
"3"
);
assert_eq!(
run("(function () { var f = ({a}) => a; return f({a: 4}); })()"),
"4"
);
assert_eq!(
run("(function () { var f = ([a]) => a; return f([5]); })()"),
"5"
);
// §10.2.11 step 24 — the *default* stands in for a missing argument first, and the pattern
// reads whichever of the two arrived. Written the other way round, a call with nothing would
// try to take `undefined` apart.
assert_eq!(run("(function ({a} = {a: 3}) { return a; })()"), "3");
assert_eq!(run("(function ([a] = [4]) { return a; })()"), "4");
// …and with no default there is nothing to stand in, so `undefined` is what it refuses.
for call in ["(null)", "()"] {
assert_eq!(
run(&format!(
"(function () {{ try {{ return (function ({{a}}) {{ return a; }}){call}; }} \
catch (e) {{ return e.constructor.name; }} }})()"
)),
"TypeError"
);
}
assert_eq!(
run(
"(function () { try { return (function ([a]) { return a; })(5); } \
catch (e) { return e.constructor.name; } })()"
),
"TypeError"
);
// The names are the function's own bindings, so the body's `var`s and its closures share them.
assert_eq!(
run("(function ({a}) { var b = 2; return a + b; })({a: 1})"),
"3"
);
assert_eq!(
run("(function ({a}) { function g() { return a; } return g(); })({a: 8})"),
"8"
);
assert_eq!(
run("(function ({a}) { return typeof a; })({})"),
"undefined"
);
// §20.2.4.1 — a pattern with no default still counts towards `length`, because `length` stops
// at the first *default* and not at the first thing that is not a name.
assert_eq!(run("(function ({a}) { return a; }).length"), "1");
assert_eq!(run("(function ({a} = {}) { return a; }).length"), "0");
assert_eq!(run("(function ([a], b) { return b; }).length"), "2");
// §15.1.4 — a pattern makes the list not simple, so the arguments object joins nothing.
assert_eq!(
run("(function ({a}) { arguments[0] = {a: 9}; return a; })({a: 1})"),
"1"
);
assert_eq!(
run("(function ({a}) { return arguments.length; })({a: 1})"),
"1"
);
}
#[test]
fn a_parameter_is_in_a_dead_zone_until_it_is_bound() {
// §10.2.11 step 21 — with no duplicate names every parameter's binding is created
// *uninitialised*, and `IteratorBindingInitialization` fills it as the parameter is bound. So
// the bindings all exist before any default runs, and a default that reads one not yet bound
// reads nothing. ViperJS had the *call* write straight into the named slots, so there was never
// anything uninitialised and both of these ran.
assert_eq!(
run(
"(function () { try { (function (x = x) {})(); return 'ran'; } \
catch (e) { return e.constructor.name; } })()"
),
"ReferenceError"
);
assert_eq!(
run(
"(function () { try { (function (x = y, y) {})(); return 'ran'; } \
catch (e) { return e.constructor.name; } })()"
),
"ReferenceError"
);
// …and the body is never entered, which is what makes it the *parameter list* throwing.
assert_eq!(
run("var ran = false; try { (function (x = x) { ran = true; })(); } catch (e) {} ran"),
"false"
);
// Left to right: a default may read every parameter *before* it, because those are bound.
assert_eq!(run("(function (x, y = x) { return y; })(3)"), "3");
assert_eq!(run("(function (x = 1, y = x + 1) { return y; })()"), "2");
assert_eq!(
run("(function ({ a }, b = a) { return b; })({ a: 5 })"),
"5"
);
// An argument that was passed ends the dead zone as surely as a default does.
assert_eq!(run("(function (x = 9, y = x) { return y; })(4)"), "4");
// The rest is bound last, after every named parameter — and it is a binding like the others.
assert_eq!(
run("(function (a, ...r) { return a + ':' + r.join(); })(1, 2, 3)"),
"1:2,3"
);
assert_eq!(run("(function (...r) { return r.length; })(1, 2)"), "2");
// A parameter is mutable once bound, which the dead zone must not have taken away.
assert_eq!(run("(function (a = 1) { a = 7; return a; })()"), "7");
// A **simple** list keeps the direct slots and has no order to observe — no defaults, no
// patterns — so these are the rows that say the split did not change the ordinary function.
assert_eq!(run("(function (a, b) { return a + b; })(1, 2)"), "3");
assert_eq!(run("(function (a) { a = 2; return a; })(1)"), "2");
assert_eq!(
run("(function (a, b) { return typeof b; })(1)"),
"undefined"
);
}
#[test]
fn a_rest_parameter_may_be_a_pattern_and_is_taken_apart_like_any_other() {
// §15.1 — `FunctionRestParameter : BindingRestElement`, whose binding may be a pattern. The
// call builds the array either way; a pattern only changes what is done with it afterwards.
assert_eq!(
run("function f(...[a, b]) { return a + ':' + b; } f(1, 2)"),
"1:2"
);
// An element the call did not reach is `undefined`, exactly as it is in any array pattern.
assert_eq!(
run("function f(...[a, b]) { return a + ':' + b; } f(1)"),
"1:undefined"
);
// …and a default inside the pattern fills that in, which is the pattern's rule and not the
// parameter list's.
assert_eq!(
run("function f(...[a = 'x', b = 'y']) { return a + ':' + b; } f(1)"),
"1:y"
);
// An object pattern reads the array's *properties*, so `length` is the one every call has.
assert_eq!(
run("function f(...{length}) { return length; } f('a', 'b', 'c')"),
"3"
);
// A nested rest inside the pattern, which is where the two rules meet.
assert_eq!(
run("function f(...[first, ...others]) { return first + ':' + others.join(); } f(1, 2, 3)"),
"1:2,3"
);
// Named parameters in front of it still take theirs first, and the pattern sees only the rest.
assert_eq!(
run("function f(a, ...[b, c]) { return [a, b, c].join(); } f(1, 2, 3)"),
"1,2,3"
);
// It works wherever a parameter list does — an arrow and a method, not only a declaration.
assert_eq!(run("var g = (...[a]) => a; g(9)"), "9");
assert_eq!(run("var o = { m(...[a]) { return a; } }; o.m(8)"), "8");
// §10.2.11 — the list is not simple, so `arguments` is unmapped and holds what was passed
// rather than what the pattern named.
assert_eq!(
run("function f(...[a]) { return arguments.length + ':' + arguments[1]; } f(1, 2)"),
"2:2"
);
}