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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! §10.4.4 — the arguments object, and the parameter map that makes it exotic.
//!
//! Every row was checked against V8 first. The interesting ones are not about `arguments.length`;
//! they are about `arguments[0]` and the first parameter being *one variable*, and about the three
//! ways a program can separate them again.
use super::*;
#[test]
fn an_arguments_object_holds_what_the_call_was_given() {
assert_eq!(
run("function f() { return arguments.length; } f(1, 2, 3)"),
"3"
);
assert_eq!(run("function f() { return arguments.length; } f()"), "0");
// The count is what was *passed*, not what was declared — which is the whole reason the
// object exists.
assert_eq!(
run("function f(a, b, c) { return arguments.length; } f(1)"),
"1"
);
assert_eq!(
run("function f() { return arguments[0]; } f('a', 'b')"),
"a"
);
assert_eq!(
run("function f() { return typeof arguments[5]; } f(1)"),
"undefined"
);
assert_eq!(
run("function f() { return typeof arguments; } f()"),
"object"
);
// §10.4.4.4 step 15 — a mapped arguments object has a `callee`, and it is the function.
assert_eq!(
run("function f() { return arguments.callee === f; } f()"),
"true"
);
// §20.1.3.6 step 8 — tagged by its parameter map, which is the only thing telling it from an
// ordinary object with numeric keys.
assert_eq!(
run("function f() { return Object.prototype.toString.call(arguments); } f()"),
"[object Arguments]"
);
// Ordinary in every other way: an ordinary prototype, and `length` and `callee` are §17
// properties that `for`-`in` does not walk.
assert_eq!(
run("function f(a) { return arguments instanceof Object; } f(1)"),
"true"
);
assert_eq!(
run(
"function f() { var r = ''; for (var k in arguments) { r = r + k; } return r; } f(1, 2)"
),
"01"
);
assert_eq!(
run(
"function f() { var d = Object.getOwnPropertyDescriptor(arguments, 'length'); \
return d.enumerable; } f(1)"
),
"false"
);
// At the top level of a script there is no call, so the name is an ordinary global that is
// not there.
assert_eq!(run("typeof arguments"), "undefined");
}
#[test]
fn an_index_and_its_parameter_are_one_variable() {
// The whole of §10.4.4, in four rows. Nothing is copied in either direction: writing one name
// is writing the other, because they are the same binding.
assert_eq!(
run("function f(a) { arguments[0] = 2; return a; } f(1)"),
"2"
);
assert_eq!(
run("function f(a) { a = 5; return arguments[0]; } f(1)"),
"5"
);
assert_eq!(
run("function f(a, b) { arguments[1] = 'x'; return b; } f(1, 2)"),
"x"
);
assert_eq!(
run("function f(a) { arguments[0] = 'w'; return arguments[0] + ',' + a; } f('v')"),
"w,w"
);
// §10.4.4.1 — and a *descriptor* reports the parameter's value too, which is what says the
// object is not holding a copy that happens to agree.
assert_eq!(
run("function f(a) { a = 8; \
return Object.getOwnPropertyDescriptor(arguments, '0').value; } f(1)"),
"8"
);
// The map reaches only as far as there are parameters: an argument past the last one is an
// ordinary property with nothing behind it.
assert_eq!(
run("function f(a) { arguments[1] = 'x'; return arguments[1]; } f(1, 2)"),
"x"
);
// …and only the canonical spelling of an index is joined. `"01"` is not the key `"1"`, so it
// is not a property of the object at all.
assert_eq!(run("function f(a) { return arguments['0']; } f('z')"), "z");
assert_eq!(
run("function f(a) { return typeof arguments['01']; } f('z')"),
"undefined"
);
}
#[test]
fn the_link_can_be_broken_and_never_comes_back() {
// §10.4.4.5 step 4 — deleting the index separates the two names for good: the parameter goes
// on changing and the object no longer hears about it.
assert_eq!(
run("function f(a) { delete arguments[0]; a = 9; return typeof arguments[0]; } f(1)"),
"undefined"
);
// …and the parameter is untouched by the delete, because what was removed is a property and
// not a variable.
assert_eq!(
run("function f(a) { delete arguments[0]; return a; } f(1)"),
"1"
);
// §10.4.4.2 step 5 — redefining the index as an accessor breaks it too, a parameter being no
// sort of accessor.
assert_eq!(
run("function f(a) { Object.defineProperty(arguments, '0', \
{get: function () { return 3; }}); return a; } f(1)"),
"1"
);
// …and so does making it unwritable. The order in §10.4.4.2 is the interesting part: the
// value is written through *first* and the link is broken after, so a define that does both
// leaves the parameter changed.
assert_eq!(
run("function f(a) { Object.defineProperty(arguments, '0', \
{value: 7, writable: false}); return a; } f(1)"),
"7"
);
assert_eq!(
run(
"function f(a) { Object.defineProperty(arguments, '0', {writable: false}); \
a = 9; return arguments[0]; } f(1)"
),
"1"
);
// An ordinary define still writes through, which is the case the three above are exceptions
// to rather than the rule — and the link survives it, so the parameter goes on being read
// through the index afterwards.
assert_eq!(
run("function f(a) { Object.defineProperty(arguments, '0', {value: 7}); return a; } f(1)"),
"7"
);
assert_eq!(
run(
"function f(a) { Object.defineProperty(arguments, '0', {value: 7}); a = 9; return arguments[0]; } f(1)"
),
"9"
);
// Only §10.4.4.2's two changes break it. Making the property non-configurable is neither of
// them, so the two names are still one variable afterwards.
assert_eq!(
run(
"function f(a) { Object.defineProperty(arguments, '0', {configurable: false}); a = 9; return arguments[0]; } f(1)"
),
"9"
);
// §10.4.4.2 step 3 — a define the ordinary rules *refuse* changes nothing, including the
// mapping. Non-configurable and then asked to become an accessor is the one refusal a still
// mapped index can be handed.
assert_eq!(
run(
"function f(a) { Object.defineProperty(arguments, '0', {configurable: false}); try { Object.defineProperty(arguments, '0', {get: function () { return 3; }}); } catch (e) {} a = 9; return arguments[0]; } f(1)"
),
"9"
);
// …and once an accessor define is *allowed*, a later define that puts a plain value back does
// not rejoin them. Nothing does.
assert_eq!(
run(
"function f(a) { Object.defineProperty(arguments, '0', {get: function () { return 3; }}); Object.defineProperty(arguments, '0', {value: 5, writable: true, configurable: true}); return a; } f(1)"
),
"1"
);
// Nor does assigning the index again after a delete: the property comes back, the link does
// not.
assert_eq!(
run("function f(a) { delete arguments[0]; arguments[0] = 4; return a; } f(1)"),
"1"
);
assert_eq!(
run(
"function f(a) { Object.defineProperty(arguments, '0', {value: 7, writable: false}); a = 9; return arguments[0]; } f(1)"
),
"7"
);
}
#[test]
fn each_call_has_its_own_and_an_arrow_has_none() {
// A nested *function* has an arguments object of its own, so the outer one is not what it
// reads.
assert_eq!(
run("function f(a) { return (function () { return arguments[0]; })('inner'); } f('outer')"),
"inner"
);
assert_eq!(
run(
"function f(a) { var g = function () { return arguments.length; }; return g(1, 2); } f(9)"
),
"2"
);
// §15.3 — an arrow has none, so the name reaches the function it is written inside, exactly
// as `this` does. This is why the enclosing function has to be told that an arrow within it
// used the name: it is the one that builds the object.
assert_eq!(
run("function outer(a) { var g = () => arguments[0]; return g(); } outer('lex')"),
"lex"
);
assert_eq!(
run("function outer(a) { var g = () => arguments.length; return g(); } outer(1, 2, 3)"),
"3"
);
// §10.2.11 step 19 — a parameter or a `var` of that name takes it, and then there is no
// arguments object at all.
assert_eq!(
run("function f(arguments) { return arguments; } f('shadowed')"),
"shadowed"
);
assert_eq!(
run("function f() { var arguments = 5; return arguments; } f(1)"),
"5"
);
// …but a bare `var arguments` does not, and this is the half of step 19 that surprises. It
// names the parameters, the hoisted functions and the lexical declarations; `var` is none of
// them, so the object is made and the declaration finds it already in the slot.
assert_eq!(
run("function f() { var arguments; return typeof arguments; } f(1)"),
"object"
);
assert_eq!(
run("function f(a) { var arguments; return arguments[0]; } f(7)"),
"7"
);
// A declaration that does take the name puts its own thing there, whichever kind it is.
assert_eq!(
run("function f() { function arguments() {} return typeof arguments; } f(1)"),
"function"
);
assert_eq!(
run("function f() { let arguments; return typeof arguments; } f(1)"),
"undefined"
);
// It outlives the call, which is why the collector has to keep the call's variables: the
// object handed back is still reading them.
assert_eq!(
run("function f(a) { return arguments; } typeof f(1)"),
"object"
);
assert_eq!(
run("function f(a) { return arguments; } f('kept')[0]"),
"kept"
);
}
#[test]
fn a_sloppy_function_names_the_one_that_called_it() {
// DR-0028 — ES5 §15.3.5.4's legacy reflection, which ES2015 deleted and every engine kept.
// The value is the function whose call is directly underneath this one on the stack.
assert_eq!(
run("function inner() { return inner.caller; } \
function outer() { return inner(); } \
outer() === outer"),
"true"
);
// Called from the script rather than from a function, there is nothing underneath: `null`,
// which is the extension's own answer and **not** `undefined` — the two are told apart by
// `10.6-13-a-2.js`, which reads `undefined` as "this host has not got the extension" and would
// then try to call whatever it did get.
assert_eq!(run("function f() { return f.caller; } f()"), "null");
// Not executing at all is the same absence.
assert_eq!(run("function f() {} f.caller"), "null");
// Recursion names the level below, so every frame but the outermost answers with the function
// itself. This is the row that says the *topmost* invocation is the one described.
assert_eq!(
run("var seen = []; \
function f(n) { seen.push(f.caller === f); if (n > 0) { f(n - 1); } } \
f(2); seen.join(',')"),
"false,true,true"
);
// §15.3.5.4's own rule, and the reason the property was ever thought a hole: a **strict**
// caller is not handed over. It is `null` rather than a throw, because a throw here would fail
// the very tests the extension exists for — `15.3.5.4_2-75gs.js` calls a sloppy function from a
// strict one and asserts nothing except that nothing was raised.
assert_eq!(
run("function inner() { return inner.caller; } \
function outer() { 'use strict'; return inner(); } \
outer()"),
"null"
);
// `arguments` is the other half of the pair and answers `null` always — see the getter's own
// documentation for why an activation cannot be named from outside the call. What matters here
// is that it is an *answer*: the inherited property throws, and a program feature-testing the
// pair must find both halves behaving alike.
assert_eq!(run("function f() { return f.arguments; } f(1, 2)"), "null");
// The pair is own, not enumerable, configurable, and getter-only — so a sloppy write is
// discarded and a strict one throws. Deleting it uncovers §10.2.4's, which is the escape hatch
// that keeps the standard behaviour reachable.
assert_eq!(
run("function f() {} \
var d = Object.getOwnPropertyDescriptor(f, 'caller'); \
[typeof d.get, typeof d.set, d.enumerable, d.configurable].join(',')"),
"function,undefined,false,true"
);
assert_eq!(
run("function f() {} f.caller = 1; typeof f.caller"),
"object",
"a sloppy write is discarded rather than replacing the accessor"
);
assert_eq!(
run("function f() {} delete f.caller; \
try { f.caller; 'read' } catch (e) { e.constructor.name }"),
"TypeError",
"and deleting it reaches the poisoned pair underneath"
);
// One getter per realm, shared by every function that has the pair — the same arrangement
// §10.2.4 makes for %ThrowTypeError%, and a program can see it.
assert_eq!(
run("function f() {} function g() {} \
Object.getOwnPropertyDescriptor(f, 'caller').get === \
Object.getOwnPropertyDescriptor(g, 'caller').get"),
"true"
);
// The getters are shared, so one can be taken off a function and applied to another. Applied
// to a kind that never had the pair it refuses, in the inherited pair's own words, so that a
// program cannot tell which route it reached.
assert_eq!(
run(
"function f() {} function g() { return g.caller; } function h() { return g(); } \
var get = Object.getOwnPropertyDescriptor(f, 'caller').get; \
(function () { return get.call(g); })() === null"
),
"true"
);
assert_eq!(
run("function f() {} \
var get = Object.getOwnPropertyDescriptor(f, 'caller').get; \
try { get.call(() => {}); 'read' } catch (e) { e.message }"),
"this property may not be read or written"
);
}
#[test]
fn only_a_sloppy_function_declaration_or_expression_gets_the_legacy_pair() {
// DR-0028's exclusions, each of which is a test262 file or a claim this repository already
// makes. A `hasOwnProperty` row rather than a `try`/`catch` one, because the two questions come
// apart: an excluded kind must not merely *throw*, it must have no own property at all —
// `GeneratorFunction/instance-restricted-properties.js` asks exactly that.
let has = "function own(f) { \
return Object.prototype.hasOwnProperty.call(f, 'caller') + '' + \
Object.prototype.hasOwnProperty.call(f, 'arguments'); }";
assert_eq!(run(&format!("{has} own(function () {{}})")), "truetrue");
assert_eq!(
run(&format!("{has} own(function () {{ 'use strict'; }})")),
"falsefalse"
);
assert_eq!(run(&format!("{has} own(() => {{}})")), "falsefalse");
assert_eq!(run(&format!("{has} own(function* () {{}})")), "falsefalse");
assert_eq!(
run(&format!("{has} own(async function () {{}})")),
"falsefalse"
);
assert_eq!(run(&format!("{has} own(({{ m() {{}} }}).m)")), "falsefalse");
assert_eq!(
run(&format!("{has} own(class {{}})")),
"falsefalse",
"a class body is strict by §11.2.2, so the constructor is excluded twice over"
);
assert_eq!(
run(&format!("{has} own((function () {{}}).bind(null))")),
"falsefalse",
"a bound function has no body of its own — S15.3.4.5_A2.js is what notices"
);
assert_eq!(
run(&format!("{has} own(Math.max)")),
"falsefalse",
"and neither has a built-in"
);
// `new Function()` is the site that makes strictness insufficient on its own: what it builds is
// **sloppy**, so a host asking only about that would give a dynamic generator the pair and fail
// `GeneratorFunction/instance-restricted-properties.js`.
assert_eq!(run(&format!("{has} own(new Function(''))")), "truetrue");
assert_eq!(
run(&format!(
"{has} var G = Object.getPrototypeOf(function* () {{}}).constructor; own(new G())"
)),
"falsefalse"
);
}
#[test]
fn caller_and_arguments_are_refused_through_every_function_the_pair_is_not_shadowed_on() {
// §10.2.4 `AddRestrictedFunctionProperties` — ES5's two ways of walking the call stack from
// inside a function, closed off. What replaced them is not their *absence*: they are accessor
// properties whose getter and setter both throw, so a program that asks gets a TypeError. The
// difference is what a feature test can see — `undefined` would say this engine has not got
// round to them.
//
// **This test said "through any function" and was renamed**, because DR-0028 gives a *sloppy*
// function a pair of its own that shadows this one. Everything below is a kind that gets no
// such pair, and the four rows that used to use a plain `function f() {}` say `'use strict'`
// now. That is the whole of what the extension changed here: the refusal is still the answer
// everywhere the extension does not reach, and there are more of those than there are of the
// other.
assert_eq!(
run(
"function f() { 'use strict'; } try { f.caller; 'read' } catch (e) { e.constructor.name }"
),
"TypeError"
);
assert_eq!(
run(
"function f() { 'use strict'; } try { f.arguments; 'read' } \
catch (e) { e.constructor.name }"
),
"TypeError"
);
assert_eq!(
run(
"function f() { 'use strict'; } try { f.caller = 1; 'wrote' } \
catch (e) { e.constructor.name }"
),
"TypeError"
);
// Through an arrow, a method and a bound function too, since the pair is on the prototype they
// all share rather than on any of them — and none of the three is a kind DR-0028 reaches.
assert_eq!(
run("try { (() => {}).caller; 'read' } catch (e) { e.constructor.name }"),
"TypeError"
);
assert_eq!(
run("var o = { m() {} }; try { o.m.caller; 'read' } catch (e) { e.constructor.name }"),
"TypeError"
);
assert_eq!(
run("try { (function* () {}).caller; 'read' } catch (e) { e.constructor.name }"),
"TypeError"
);
assert_eq!(
run("try { (async function () {}).arguments; 'read' } catch (e) { e.constructor.name }"),
"TypeError"
);
assert_eq!(
run("try { (function () {}).bind(null).arguments; 'read' } \
catch (e) { e.constructor.name }"),
"TypeError"
);
// §10.2.4 puts them on `Function.prototype` **and on no individual function**, which is where
// ES2015 moved them: ES5 gave every strict function a pair of its own, and this is the check
// that tells the two arrangements apart.
assert_eq!(
run("function f() { 'use strict'; } \
Object.prototype.hasOwnProperty.call(f, 'caller') + ',' + \
Object.prototype.hasOwnProperty.call(Function.prototype, 'caller')"),
"false,true"
);
// The descriptor: an accessor, both halves the same function, not enumerable, and
// **configurable** — the one attribute these two do not share with §17's usual shape, so that
// a host or a script may replace them.
assert_eq!(
run(
"var d = Object.getOwnPropertyDescriptor(Function.prototype, 'caller'); \
[typeof d.get, d.get === d.set, d.enumerable, d.configurable].join(',')"
),
"function,true,false,true"
);
}
#[test]
fn there_is_one_function_that_refuses_and_every_restricted_property_shares_it() {
// §10.2.4.1 %ThrowTypeError% is a single object per realm, and a program can see that it is:
// both halves of both restricted accessors are the same function, and so is the poisoned
// `callee` of an unmapped arguments object. Anything else would be four functions that behave
// alike, which is not what the specification says and is observable with `===`.
assert_eq!(
run(
"var caller = Object.getOwnPropertyDescriptor(Function.prototype, 'caller'); \
var args = Object.getOwnPropertyDescriptor(Function.prototype, 'arguments'); \
var callee = Object.getOwnPropertyDescriptor( \
function () { 'use strict'; return arguments; }(), 'callee'); \
[caller.get === caller.set, caller.get === args.get, args.get === args.set, \
caller.get === callee.get, callee.get === callee.set].join(',')"
),
"true,true,true,true,true"
);
// Its own shape, which is stricter than any other built-in's. `name` is the **empty string**
// and not `"ThrowTypeError"` — that is the specification's name for it, not one a program may
// read — and `length` and `name` are non-writable *and* non-configurable where §17 makes every
// other built-in's configurable.
assert_eq!(
run(
"var T = Object.getOwnPropertyDescriptor(Function.prototype, 'caller').get; \
var n = Object.getOwnPropertyDescriptor(T, 'name'); \
var l = Object.getOwnPropertyDescriptor(T, 'length'); \
[n.value === '', n.writable, n.configurable, l.value, l.writable, l.configurable] \
.join(',')"
),
"true,false,false,0,false,false"
);
// …and it is shut: not extensible, and frozen, so nothing can be hung on the one function every
// restricted property in the realm shares. It also has no `prototype` of its own and is not a
// constructor.
assert_eq!(
run(
"var T = Object.getOwnPropertyDescriptor(Function.prototype, 'caller').get; \
[Object.isExtensible(T), Object.isFrozen(T), \
Object.prototype.hasOwnProperty.call(T, 'prototype'), \
Object.getPrototypeOf(T) === Function.prototype].join(',')"
),
"false,true,false,true"
);
// Calling it is the refusal itself, which is what the accessors are for.
assert_eq!(
run(
"var T = Object.getOwnPropertyDescriptor(Function.prototype, 'caller').get; \
try { T(); 'called' } catch (e) { e.constructor.name }"
),
"TypeError"
);
}
#[test]
fn a_strict_functions_arguments_is_not_joined_to_its_parameters() {
// §10.2.11 step 22 asks **two** questions: the parameter list must be simple *and* the code
// must be sloppy. Asking only about the list gave a strict function the mapped object, and the
// join is observable in both directions.
assert_eq!(
run("function f(a) { 'use strict'; a = 2; return arguments[0]; } f(1)"),
"1"
);
assert_eq!(
run("function f(a) { 'use strict'; arguments[0] = 2; return a; } f(1)"),
"1"
);
// …where a sloppy one with the same parameters is one variable seen two ways, which is what
// makes this a test of the condition rather than of mapping being gone.
assert_eq!(
run("function g(a) { a = 2; return arguments[0]; } g(1)"),
"2"
);
assert_eq!(
run("function g(a) { arguments[0] = 2; return a; } g(1)"),
"2"
);
// Strictness inherited from the code around it counts too — §11.2.1 makes it a property of the
// body, not of the directive being written in that body.
assert_eq!(
run("'use strict'; function f(a) { a = 2; return arguments[0]; } f(1)"),
"1"
);
// And `callee` follows the same split: the function on a mapped object, poisoned on an
// unmapped one — §10.4.4.6 step 6, and the idiom ES2015 was closing off.
assert_eq!(
run("function g(a) { return arguments.callee === g; } g(1)"),
"true"
);
assert_eq!(
run(
"function f(a) { 'use strict'; try { return arguments.callee; } \
catch (e) { return e.constructor.name; } } f(1)"
),
"TypeError"
);
}
#[test]
fn arguments_is_the_running_functions_however_many_scopes_are_between() {
// §10.2.11 step 19 gives the *function* the object, and the compiler decides whether to build
// one by whether the name resolved to its slot. That comparison used to be against depth zero,
// which is only right when nothing has opened a scope since — so an ordinary block with a `let`
// in it made the read resolve one hop out, told the **enclosing** function to build an object,
// and left this one reading a slot nothing had filled. It threw.
assert_eq!(
run("function f() { { let z = 1; return arguments[0]; } } f(7)"),
"7"
);
assert_eq!(
run("function f() { { let z = 1; return typeof arguments; } } f()"),
"object"
);
// Every kind of scope that adds a hop, since each reaches the same comparison.
assert_eq!(
run("function f() { for (let i = 0; i < 1; i++) { return arguments[0]; } } f(4)"),
"4"
);
assert_eq!(
run("function f() { switch (1) { case 1: let q = 1; return arguments[0]; } } f(5)"),
"5"
);
assert_eq!(
run("function f() { try { throw 1; } catch (e) { return arguments[0]; } } f(6)"),
"6"
);
assert_eq!(
run("function f() { var o = {}; with (o) { return arguments[0]; } } f(7)"),
"7"
);
// …and `typeof` is a read like any other, which is a second path to the same question and did
// not ask it: `with (o) { typeof arguments }` answered `"undefined"` with the object in reach.
assert_eq!(
run("function f() { var o = {}; with (o) { return typeof arguments; } } f()"),
"object"
);
// The other side of the comparison still holds: an **arrow** has none of its own, so its read
// resolves further out and the function around it is the one that builds the object.
assert_eq!(
run("function f(a) { { let z = 1; return (() => arguments[0])(); } } f(3)"),
"3"
);
}
#[test]
fn an_arguments_object_can_be_walked_and_spread() {
// §10.4.4.4 step 16 and §10.4.4.6 step 7 — `%Symbol.iterator%` is `%Array.prototype.values%`,
// and it was missing entirely. So `[...arguments]` and `for (x of arguments)` both threw
// "what was called is not a function": §7.4.4 asking an object with no `@@iterator` for one.
// Every other part of the object said array-like; this is the property that makes it walkable.
assert_eq!(
run("function f() { return [...arguments].join(',') } f(1, 2, 3)"),
"1,2,3"
);
assert_eq!(
run(
"function f() { var out = []; for (var x of arguments) { out.push(x) } return out.join(',') } f('a', 'b')"
),
"a,b"
);
// **Mapped and unmapped alike**, which is why the two clauses say the same thing: a strict
// function and one with a default parameter both get an unmapped object, and both walk.
assert_eq!(
run(
"function f(a) { 'use strict'; return [...arguments].length } \
function g(a = 1) { return [...arguments].length } \
f(1, 2) + ',' + g(7, 8, 9)"
),
"2,3"
);
// It **is** `%Array.prototype.values%`, by identity, and §17's ordinary attributes rather than
// the fixed ones `callee` gets.
assert_eq!(
run("function f() { \
var d = Object.getOwnPropertyDescriptor(arguments, Symbol.iterator); \
return (arguments[Symbol.iterator] === Array.prototype.values) \
+ '|' + d.writable + ',' + d.enumerable + ',' + d.configurable } f()"),
"true|true,false,true"
);
// Held by *identity*, not read off `Array.prototype` when the object is made: the clause names
// the intrinsic, so a script that replaces the method leaves the walk alone. Reading it at each
// call would pass every test above and this one is the only thing that tells them apart.
assert_eq!(
run(
"Array.prototype.values = function () { throw 'replaced' }; \
function f() { return [...arguments].join(',') } f(4, 5)"
),
"4,5"
);
}