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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
//! §15.7 — a class body: its methods, its fields, its statics, and the scope it is.
//!
//! The rows that matter most are the ones that separate a class body from an object literal, because
//! that is where an implementation that reused the literal path would look right and be wrong: a
//! method here is not enumerable, `prototype` cannot be replaced, and the constructor refuses a call
//! without `new`.
//!
//! `extends` and `super` are [`super::inheritance`]. This heading claimed to exclude them long after
//! it had stopped doing so, which is the same failure as a refusal test that no longer refuses.
use super::*;
#[test]
fn a_class_puts_its_methods_on_the_prototype_and_its_statics_on_itself() {
assert_eq!(
run("(function () { class C { m() { return 1; } } return new C().m(); })()"),
"1"
);
assert_eq!(
run("(function () { class C { m() {} } return C.prototype.hasOwnProperty('m'); })()"),
"true"
);
// A static is on the constructor and *not* on the prototype, which is the whole of what the
// keyword does.
assert_eq!(
run("(function () { class C { static s() { return 2; } } return C.s(); })()"),
"2"
);
assert_eq!(
run("(function () { class C { static s() {} } \
return C.hasOwnProperty('s') + ',' + C.prototype.hasOwnProperty('s'); })()"),
"true,false"
);
// …and an instance method is not on the instance either: it is inherited.
assert_eq!(
run("(function () { class C { m() {} } var o = new C(); \
return o.hasOwnProperty('m') + ',' + ('m' in o); })()"),
"false,true"
);
// The same name may be both, and the two do not collide.
assert_eq!(
run(
"(function () { class C { m() { return 'proto'; } static m() { return 'static'; } } \
return C.m() + ',' + new C().m(); })()"
),
"static,proto"
);
}
#[test]
fn a_class_method_is_not_enumerable_and_an_object_literal_method_is() {
// §15.7.14 against §15.4.5 — the single runtime difference between the two bodies, and the one
// an implementation that shared their code path would get wrong in a way nothing else notices.
assert_eq!(
run("(function () { class C { m() {} } return Object.keys(C.prototype).length; })()"),
"0"
);
assert_eq!(run("Object.keys({m: function () {}}).length"), "1");
assert_eq!(
run("(function () { class C { m() {} } var seen = []; \
for (var k in new C()) seen.push(k); return seen.length; })()"),
"0"
);
// Not absent, though — the property is there and is writable and configurable.
assert_eq!(
run("(function () { class C { m() {} } \
var d = Object.getOwnPropertyDescriptor(C.prototype, 'm'); \
return d.writable + ',' + d.enumerable + ',' + d.configurable; })()"),
"true,false,true"
);
// A static method carries the same three.
assert_eq!(
run("(function () { class C { static s() {} } \
var d = Object.getOwnPropertyDescriptor(C, 's'); \
return d.writable + ',' + d.enumerable + ',' + d.configurable; })()"),
"true,false,true"
);
// `constructor` is not enumerable either, and is the reason `Object.keys` above is empty rather
// than one short.
assert_eq!(
run("(function () { class C {} \
var d = Object.getOwnPropertyDescriptor(C.prototype, 'constructor'); \
return d.writable + ',' + d.enumerable + ',' + d.configurable; })()"),
"true,false,true"
);
}
#[test]
fn an_accessor_in_a_class_body_is_one_property_with_two_halves() {
assert_eq!(
run("(function () { class C { get a() { return 3; } } return new C().a; })()"),
"3"
);
assert_eq!(
run("(function () { class C { set a(v) { this.taken = v; } } \
var o = new C(); o.a = 4; return o.taken; })()"),
"4"
);
// Defining one half must not wipe the other, which is `DefineOwnProperty` with only the half
// that was written rather than `CreateDataProperty`.
assert_eq!(
run(
"(function () { class C { get a() { return this.held; } set a(v) { this.held = v * 2; } } \
var o = new C(); o.a = 5; return o.a; })()"
),
"10"
);
assert_eq!(
run("(function () { class C { get a() {} set a(v) {} } \
var d = Object.getOwnPropertyDescriptor(C.prototype, 'a'); \
return (typeof d.get) + ',' + (typeof d.set) + ',' + d.enumerable; })()"),
"function,function,false"
);
// A static accessor lands on the constructor.
assert_eq!(
run("(function () { class C { static get a() { return 6; } } return C.a; })()"),
"6"
);
}
#[test]
fn a_computed_key_runs_where_it_is_written_and_in_order() {
assert_eq!(
run("(function () { class C { ['a' + 'b']() { return 7; } } return new C().ab(); })()"),
"7"
);
// The keys are evaluated as the body is walked, so their side effects come in source order —
// and a static between two instance methods does not jump the queue.
assert_eq!(
run(
"(function () { var order = []; var k = function (n) { order.push(n); return 'm' + n; }; \
class C { [k(1)]() {} static [k(2)]() {} [k(3)]() {} } \
return order.join(''); })()"
),
"123"
);
// Two methods with the same key leave the later one in place.
assert_eq!(
run(
"(function () { class C { m() { return 'first'; } m() { return 'second'; } } \
return new C().m(); })()"
),
"second"
);
}
#[test]
fn the_constructor_is_the_class_and_cannot_be_called_without_new() {
assert_eq!(
run("(function () { class C { constructor(x) { this.x = x; } } return new C(5).x; })()"),
"5"
);
// A class with no constructor written still has one, and it takes no arguments.
assert_eq!(run("(function () { class C {} return C.length; })()"), "0");
assert_eq!(
run("(function () { class C { constructor(a, b) {} } return C.length; })()"),
"2"
);
assert_eq!(
run("(function () { class C {} return typeof C; })()"),
"function"
);
assert_eq!(
run("(function () { class C {} return new C() instanceof C; })()"),
"true"
);
assert_eq!(
run("(function () { class C {} return C.prototype.constructor === C; })()"),
"true"
);
// A written constructor is *not* also defined as a prototype method. `class C {}` cannot show
// this — with no constructor element there is nothing for the loop to skip — so the row needs an
// explicit one, and what it catches is `C.prototype.constructor` becoming a second function
// object compiled from the same body.
assert_eq!(
run(
"(function () { class C { constructor() {} } return C.prototype.constructor === C; })()"
),
"true"
);
assert_eq!(
run(
// Insertion order, which §10.1.11 fixes: `constructor` comes from the class definition
// itself and `m` from the walk over the body. Not sorted, because `Array.prototype.sort`
// does not exist yet and a row should not depend on a slice that has not landed.
"(function () { class C { constructor() { this.n = 1; } m() {} } \
return Object.getOwnPropertyNames(C.prototype).join(','); })()"
),
"constructor,m"
);
// §15.7.14 — the `[[Call]]` exists only to refuse, however the function is reached.
assert_eq!(
run("(function () { class C {} try { C(); return 'called'; } \
catch (e) { return e.constructor.name; } })()"),
"TypeError"
);
assert_eq!(
run(
"(function () { class C {} try { C.prototype.constructor(); return 'called'; } \
catch (e) { return e.constructor.name; } })()"
),
"TypeError"
);
assert_eq!(
run(
"(function () { class C {} try { C.call(null); return 'called'; } \
catch (e) { return e.constructor.name; } })()"
),
"TypeError"
);
// …and a method of a class is an ordinary function, which may be called.
assert_eq!(
run("(function () { class C { m() { return 8; } } return C.prototype.m.call(null); })()"),
"8"
);
}
#[test]
fn a_class_prototype_cannot_be_pointed_somewhere_else() {
// §15.7.14 — non-writable *and* non-configurable, unlike §10.2.5's for an ordinary function,
// which is writable. An instance already inherits from it by the time a script could look.
assert_eq!(
run(
"(function () { class C {} var d = Object.getOwnPropertyDescriptor(C, 'prototype'); \
return d.writable + ',' + d.enumerable + ',' + d.configurable; })()"
),
"false,false,false"
);
// An ordinary function's is writable, which is the row that makes the one above mean something.
assert_eq!(
run("(function () { function f() {} \
var d = Object.getOwnPropertyDescriptor(f, 'prototype'); \
return d.writable + ',' + d.configurable; })()"),
"true,false"
);
assert_eq!(
run(
"(function () { class C {} var was = C.prototype; C.prototype = {}; \
return C.prototype === was; })()"
),
"true"
);
}
#[test]
fn a_class_declaration_is_lexical_and_an_expression_is_a_value() {
// §15.7.11 — hoisted and left uninitialised, so a reference above it is the dead zone rather
// than `undefined`. That is the difference from a function declaration, which is callable above
// its own line.
assert_eq!(
run(
"(function () { try { C; return 'read'; } catch (e) { return e.constructor.name; } \
class C {} })()"
),
"ReferenceError"
);
assert_eq!(
run("(function () { return typeof f; function f() {} })()"),
"function"
);
// An expression is just a value, and needs no name.
assert_eq!(
run("(function () { var K = class { m() { return 9; } }; return new K().m(); })()"),
"9"
);
assert_eq!(run("typeof (class {})"), "function");
assert_eq!(
run("(function () { var a = [class {}, class {}]; return a[0] === a[1]; })()"),
"false"
);
// §15.7.11 binds the name *mutably*, unlike `const`: a class declaration is a `let`-shaped
// binding, so the name may be pointed at something else afterwards. Nothing else in this file
// would notice if it were immutable, which is why this row is here rather than assumed.
assert_eq!(run("(function () { class C {} C = 1; return C; })()"), "1");
assert_eq!(
run("(function () { class C {} var was = C; C = 1; return typeof was; })()"),
"function"
);
// Each evaluation of a class expression makes a *new* constructor and a new prototype.
assert_eq!(
run(
"(function () { var make = function () { return class { m() {} }; }; \
return make().prototype === make().prototype; })()"
),
"false"
);
}
#[test]
fn every_kind_of_class_element_compiles_and_the_refusal_list_is_empty() {
// This was `what_a_class_body_cannot_hold_yet_is_refused_by_name`, and it had been shortened eight
// times — once per slice, each time because a row had come to assert the opposite of what it said.
// There is nothing left to shorten: §15.7 is complete, and so is the pair of forms that read a
// reference back before writing it. A refusal test with no refusals in it is not a test, so what
// remains is the positive half, which is what the rows were guarding all along.
//
// Kept as one test rather than deleted, because the *list* is the useful artefact: the next
// element §15.7 grows gets a row here, and a reader looking for what a class body cannot hold
// finds the answer in one place.
assert_eq!(
run(
"class C { a = 1; static b = 2; ['c'] = 3; static { this.d = 4; } m() {} \
static n() {} get g() { return 5; } #p = 6; #q() { return 7; } \
get #r() { return 8; } static #s = 9; static #t() { return 10; } \
all() { return this.#p + ',' + this.#q() + ',' + this.#r; } \
static statics() { return C.#s + ',' + C.#t(); } } \
C.b + ',' + C.d + ',' + new C().a + ',' + new C().c + ',' + new C().g \
+ ',' + new C().all() + ',' + C.statics()"
),
"2,4,1,3,5,6,7,8,9,10"
);
}
#[test]
fn a_field_is_defined_on_the_instance_and_not_assigned_to_it() {
assert_eq!(
run("(function () { class C { x = 1; } return new C().x; })()"),
"1"
);
// §15.7.14 — a field written without an initialiser is `undefined`, which is not the same as the
// field being absent: the own property is there and `for...in` finds it.
assert_eq!(
run("(function () { class C { x; } var o = new C(); \
return o.hasOwnProperty('x') + ',' + (o.x === undefined); })()"),
"true,true"
);
// Unlike a method, a field *is* enumerable — it is an ordinary data property of the instance.
assert_eq!(
run("(function () { class C { x = 1; } var seen = []; \
for (var p in new C()) seen.push(p); return seen.join(','); })()"),
"x"
);
assert_eq!(
run("(function () { class C { x = 1; } \
var d = Object.getOwnPropertyDescriptor(new C(), 'x'); \
return d.writable + ',' + d.enumerable + ',' + d.configurable; })()"),
"true,true,true"
);
// The row this whole design exists for. §15.7.14 initialises with `CreateDataPropertyOrThrow`,
// which ignores an inherited setter; `this.s = 5` is `[[Set]]`, which would call it. So a field
// *shadows* a prototype setter rather than running it — and an implementation that prepended
// assignment statements to the constructor would pass every other row in this file and fail this
// one.
assert_eq!(
run("(function () { class B {} \
Object.defineProperty(B.prototype, 's', {set: function (v) { this.ran = v; }}); \
class C { s = 5; } var o = new C(); \
return o.hasOwnProperty('s') + ',' + (o.ran === undefined) + ',' + o.s; })()"),
"true,true,5"
);
// …and the assignment form really would have run it, which is what makes the row above mean
// something rather than merely pass.
assert_eq!(
run("(function () { class B {} \
Object.defineProperty(B.prototype, 's', {set: function (v) { this.ran = v; }}); \
var o = Object.create(B.prototype); o.s = 5; \
return o.hasOwnProperty('s') + ',' + o.ran; })()"),
"false,5"
);
}
#[test]
fn fields_run_in_source_order_and_before_the_constructor_body() {
// Each field can see the ones above it through `this`, because they are already defined.
assert_eq!(
run("(function () { class C { x = 1; y = this.x + 1; } return new C().y; })()"),
"2"
);
// §15.7.14 — `InitializeInstanceElements` runs before the constructor's first statement, so the
// body finds every field already there.
assert_eq!(
run(
"(function () { class C { x = 1; constructor() { this.y = this.x + 1; } } \
return new C().y; })()"
),
"2"
);
// The initialisers run in the order they are written, which a side effect can see.
assert_eq!(
run(
"(function () { var order = []; var mark = function (n) { order.push(n); return n; }; \
class C { a = mark(1); b = mark(2); c = mark(3); } new C(); \
return order.join(''); })()"
),
"123"
);
// Once per construction, not once per class.
assert_eq!(
run(
"(function () { var calls = 0; var mark = function () { calls++; return 1; }; \
class C { a = mark(); } new C(); new C(); return calls; })()"
),
"2"
);
// A field and a constructor parameter of the same name do not collide: the field wins, because
// it is initialised before the body could assign anything.
assert_eq!(
run(
"(function () { class C { x = 'field'; constructor(x) { this.taken = x; } } \
var o = new C('argument'); return o.x + ',' + o.taken; })()"
),
"field,argument"
);
// An empty field list emits no prologue at all, which is the branch a class without fields takes.
assert_eq!(
run("(function () { class C { m() { return 1; } } return new C().m(); })()"),
"1"
);
}
#[test]
fn a_class_body_holds_every_kind_of_element_at_once() {
// What was a second copy of the refusal list above, kept for the one thing it did that the other
// did not: check that the element kinds still work when they are written together, rather than
// one per test.
assert_eq!(
run("class C { a = 1; static b = 2; ['c'] = 3; } C.b + ',' + new C().a"),
"2,1"
);
}
#[test]
fn a_class_body_is_a_scope_holding_the_class_name() {
// §15.7.14 steps 4 to 7 — the body gets a scope of its own with a binding for the class's own
// name. A declaration also has an outer binding, and an expression has none, so this is the only
// way either kind can name itself from inside.
assert_eq!(
run("(function () { var K = class C { m() { return C; } }; return new K().m() === K; })()"),
"true"
);
assert_eq!(
run("(function () { class C { m() { return C; } } return new C().m() === C; })()"),
"true"
);
// A static method reaches it too, and so does a computed key — the binding is initialised before
// any element is defined, which is what makes both work.
assert_eq!(
run(
"(function () { var K = class C { static who() { return C; } }; return K.who() === K; })()"
),
"true"
);
assert_eq!(
run("(function () { var seen = []; \
var K = class C { [(seen.push(typeof C), 'm')]() {} }; return seen.join(''); })()"),
"function"
);
// The name does not escape a class expression: outside it, nothing was declared.
assert_eq!(
run("(function () { var K = class C {}; return typeof C; })()"),
"undefined"
);
// Each class has its own, so a class made inside a method names itself and not the outer one.
assert_eq!(
run(
"(function () { class Outer { m() { return class Inner { who() { return Inner; } }; } } \
var I = new Outer().m(); return new I().who() === I; })()"
),
"true"
);
}
#[test]
fn the_inner_class_binding_is_a_different_one_from_the_outer() {
// The observable difference, and the reason this is two bindings rather than one: reassigning the
// outer name leaves what the body closed over alone. A single shared binding would answer `1`.
assert_eq!(
run(
"(function () { class C { m() { return C; } } var was = C; C = 1; \
return new was().m() === was; })()"
),
"true"
);
// …and the outer one really is writable, which is what makes the row above mean something rather
// than merely pass.
assert_eq!(run("(function () { class C {} C = 1; return C; })()"), "1");
// The inner binding is *immutable* — §15.7.14 creates it with `CreateImmutableBinding`, so a body
// that assigns to its own class name is a TypeError rather than a rebinding.
assert_eq!(
run("(function () { var K = class C { m() { \
try { C = 1; return 'assigned'; } catch (e) { return e.constructor.name; } } }; \
return new K().m(); })()"),
"TypeError"
);
// Including from a declaration, where an outer *mutable* binding of the same name exists: the
// body sees the inner one, so this is a TypeError and not a write to the outer name.
assert_eq!(
run("(function () { class C { m() { \
try { C = 1; return 'assigned'; } catch (e) { return e.constructor.name; } } } \
return new C().m() + ',' + (typeof C); })()"),
"TypeError,function"
);
}
#[test]
fn a_class_at_the_top_level_of_a_script_leaves_the_stack_as_it_found_it() {
// Every other row in this file wraps its class in `(function () { … })()`, and that hid a real
// bug: an extra value left on the stack only trips the end-of-chunk balance check of a *script*,
// so a function-wrapped class compiled and ran while 1,084 test262 files failed with
// `UnbalancedStack`. A class written where a script's own statements go is the shape that
// notices.
assert_eq!(run("class C {} typeof C"), "function");
assert_eq!(run("class C { m() { return 1; } } new C().m()"), "1");
assert_eq!(
run("class C { m() { return C; } } new C().m() === C"),
"true"
);
assert_eq!(run("var K = class C {}; typeof K"), "function");
assert_eq!(run("class C { x = 1; } new C().x"), "1");
assert_eq!(
run("class C {} class D {} typeof C + ',' + typeof D"),
"function,function"
);
// …and one after other statements, so the imbalance cannot be absorbed by whatever came before.
assert_eq!(run("var a = 1; class C {} a + 1"), "2");
}
#[test]
fn a_static_field_is_defined_on_the_constructor_when_the_class_is() {
assert_eq!(run("class C { static x = 1; } C.x"), "1");
assert_eq!(
run("class C { static x; } C.hasOwnProperty('x') + ',' + (C.x === undefined)"),
"true,true"
);
// On the constructor and nowhere else.
assert_eq!(
run("class C { static x = 1; } var o = new C(); \
C.hasOwnProperty('x') + ',' + C.prototype.hasOwnProperty('x') + ',' \
+ o.hasOwnProperty('x')"),
"true,false,false"
);
assert_eq!(
run(
"class C { static x = 1; } var d = Object.getOwnPropertyDescriptor(C, 'x'); \
d.writable + ',' + d.enumerable + ',' + d.configurable"
),
"true,true,true"
);
// Once, when the class is defined — not per instance.
assert_eq!(
run("var n = 0; class C { static x = (n++, 1); } new C(); new C(); n"),
"1"
);
// An instance field and a static one of the same class do not reach each other.
assert_eq!(
run("class C { x = 1; static y = 2; } var o = new C(); \
o.x + ',' + C.y + ',' + (o.y === undefined) + ',' + (C.x === undefined)"),
"1,2,true,true"
);
assert_eq!(
run("class C { static a = 1; static b = 2; } C.a + ',' + C.b"),
"1,2"
);
}
#[test]
fn a_static_initialiser_runs_with_this_bound_to_the_constructor() {
// §15.7.14 — and the reason the initialiser is compiled as a body and *called* rather than
// emitted inline: a call is the only thing that binds a receiver, and inline code would take
// whatever `this` the surrounding scope has. A wrong answer, not a missing one.
assert_eq!(run("class C { static x = this; } C.x === C"), "true");
assert_eq!(
run("class C { static m() { return 7; } static x = this.m(); } C.x"),
"7"
);
// The surrounding `this` is something else, which is what makes the rows above mean something.
assert_eq!(
run("var outer = {}; \
var made = function () { class C { static x = this; } return C.x === C; }; \
made.call(outer)"),
"true"
);
// The class body's scope holds the name, so a static initialiser can read a static field defined
// above it through the class itself.
assert_eq!(
run("class C { static x = 1; static y = C.x + 1; } C.y"),
"2"
);
}
#[test]
fn a_static_field_evaluates_its_name_in_the_walk_and_its_initialiser_after_it() {
// The two halves of one static field happen at different times, and this is the row that says so.
// §15.7.14 evaluates a `ClassElementName` during the walk over the elements, and runs a static
// initialiser only once every element has been defined. A first attempt emitted both together and
// produced `21` here.
assert_eq!(
run(
"var order = []; var k = function (n) { order.push(n); return 'k' + n; }; \
class C { static [k(1)] = order.push(2); } order.join('')"
),
"12"
);
// And the name is evaluated *interleaved with the methods*, in source order — not gathered up and
// done at the end. Nothing but a method on each side of a static field can show that.
assert_eq!(
run(
"var order = []; var k = function (n) { order.push(n); return 'k' + n; }; \
class C { [k(1)]() {} static [k(2)] = 0; [k(3)]() {} } order.join('')"
),
"123"
);
// Two static fields keep their own names apart, which they would not if one temporary were shared.
assert_eq!(
run("var names = ['a', 'b']; var at = 0; \
class C { static [names[at++]] = 1; static [names[at++]] = 2; } \
C.a + ',' + C.b"),
"1,2"
);
// Every initialiser runs after every name, so a later name is already evaluated when an earlier
// initialiser runs.
assert_eq!(
run(
"var order = []; var k = function (n) { order.push('k' + n); return 'f' + n; }; \
var v = function (n) { order.push('v' + n); return n; }; \
class C { static [k(1)] = v(1); static [k(2)] = v(2); } order.join(',')"
),
"k1,k2,v1,v2"
);
}
#[test]
fn a_computed_field_name_is_evaluated_once_and_kept() {
assert_eq!(run("class C { ['a' + 'b'] = 1; } new C().ab"), "1");
// The point of keeping it: §15.7.14 evaluates the name once, at definition time, however many
// instances are made. An implementation that re-evaluated it per construction would answer 2.
assert_eq!(
run(
"var n = 0; var k = function () { n++; return 'x'; }; class C { [k()] = 1; } new C(); new C(); n"
),
"1"
);
// Every name is evaluated before any initialiser runs, because the names belong to the class
// definition and the initialisers to the construction.
assert_eq!(
run(
"var order = []; var k = function (n) { order.push('k' + n); return 'f' + n; }; \
class C { [k(1)] = order.push('v1'); [k(2)] = order.push('v2'); } \
new C(); order.join(',')"
),
"k1,k2,v1,v2"
);
// Two computed names keep their own values apart, which they would not if one slot were shared.
assert_eq!(
run("var names = ['a', 'b']; var at = 0; \
class C { [names[at++]] = 1; [names[at++]] = 2; } \
var o = new C(); o.a + ',' + o.b"),
"1,2"
);
// Computed and plain names side by side, in order.
assert_eq!(
run(
"class C { ['a'] = 1; b = 2; ['c'] = 3; } var o = new C(); o.a + ',' + o.b + ',' + o.c"
),
"1,2,3"
);
// And interleaved with the method keys, in source order — the same rule the static fields follow.
assert_eq!(
run(
"var order = []; var k = function (n) { order.push(n); return 'k' + n; }; \
class C { [k(1)]() {} [k(2)] = 0; [k(3)]() {} } order.join('')"
),
"123"
);
// A static and an instance computed name in one class do not disturb each other.
assert_eq!(
run("class C { static ['s'] = 1; ['i'] = 2; } C.s + ',' + new C().i"),
"1,2"
);
}
#[test]
fn a_static_block_runs_once_with_this_bound_to_the_constructor() {
assert_eq!(run("class C { static { this.x = 1; } } C.x"), "1");
// §15.7.14 binds `this` to the constructor, which is what a block is for — it defines nothing on
// its own, so without a receiver it could do nothing at all.
assert_eq!(
run("class C { static { this.self = this; } } C.self === C"),
"true"
);
assert_eq!(
run("class C { static { this.m = function () { return 5; }; } } C.m()"),
"5"
);
// Once, when the class is defined — not per instance.
assert_eq!(
run("var n = 0; class C { static { n++; } } new C(); new C(); n"),
"1"
);
// A body, not an expression: declarations inside it are its own.
assert_eq!(
run("class C { static { var a = 1; this.v = a + 1; } } C.v"),
"2"
);
assert_eq!(
run("class C { static { let a = 2; this.v = a; } } C.v"),
"2"
);
// It sees a static field defined above it, because every static element runs after every element
// has been defined and they run in order.
assert_eq!(
run("class C { static x = 1; static { this.y = this.x + 1; } } C.y"),
"2"
);
// Blocks and fields are one list in source order — nothing distinguishes them at that point, and
// gathering them separately would lose the order between them.
assert_eq!(
run("var order = []; \
class C { static { order.push(1); } static a = order.push(2); \
static { order.push(3); } } order.join('')"),
"123"
);
// Two blocks in one class, each run.
assert_eq!(
run("class C { static { this.a = 1; } static { this.b = this.a + 1; } } C.a + ',' + C.b"),
"1,2"
);
}
#[test]
fn a_class_body_is_a_scope_holding_an_immutable_binding_of_its_own_name() {
// §15.7.14 steps 4 to 7. The binding is a *scope's*, not a level of the one around it — which
// compiled code could not tell apart, because the compiler reads a liveness flag no running
// program can, and which a direct `eval` most certainly could. DR-0018 is the long version.
//
// Every row here passed before the environment existed and passes after it: that is the point
// of them. They say the scope's *semantics* are unchanged, so the change is where the binding
// lives and nothing else.
assert_eq!(
run("class C { m() { return C === D; } } var D = C; C = null; new D().m()"),
"true"
);
// Immutable — §15.7.14 makes the inner binding a `const` in all but name, so a method that
// assigns to it is a TypeError rather than a rebinding the other methods would then see.
assert_eq!(
run(
"class C { m() { try { C = 1; return 'assigned'; } catch (e) { return e.constructor.name; } } } \
new C().m()"
),
"TypeError"
);
// A named class *expression* has no outer binding at all, so this scope is the only way it can
// see itself — and the name is not visible outside it.
assert_eq!(
run("var K = class Inner { m() { return typeof Inner; } }; new K().m()"),
"function"
);
assert_eq!(run("var K = class Inner {}; typeof Inner"), "undefined");
// A static block and a static method reach it by the same chain the instance methods do.
assert_eq!(
run("class C { static m() { return C; } } var D = C; C = null; D.m() === D"),
"true"
);
assert_eq!(
run("var seen; class C { static { seen = C; } } var D = C; C = null; seen === D"),
"true"
);
// A computed field name is evaluated once at definition time and its initialiser runs per
// construction, so the value outlives the definition in a temporary of this same scope — the
// constructor reaches it as any closure reaches an outer variable, one environment further out.
assert_eq!(
run("var calls = 0; function key() { calls++; return 'k'; } \
class C { [key()] = 1; } new C(); new C(); calls"),
"1"
);
assert_eq!(run("class C { ['k'] = 7; } new C().k"), "7");
}
#[test]
fn what_a_class_defines_carries_the_attributes_the_clause_gives_it() {
// §15.7.14 — a class's own `length`, which is what its constructor says it needs. Not writable
// and not enumerable, and *configurable*, which is the set §10.3.3 gives every function's.
assert_eq!(
run(
"var d = Object.getOwnPropertyDescriptor(class C { constructor(a, b) {} }, 'length'); \
[d.value, d.writable, d.enumerable, d.configurable].join(',')"
),
"2,false,false,true"
);
// …and its methods: writable and configurable, and **not enumerable**. That last one is the
// whole run-time difference between a class method and the same syntax in an object literal,
// which is why the two are asserted against each other.
assert_eq!(
run(
"var d = Object.getOwnPropertyDescriptor(class C { m() {} }.prototype, 'm'); \
var o = Object.getOwnPropertyDescriptor({ m() {} }, 'm'); \
[d.writable, d.enumerable, d.configurable].join(',') + '|' + o.enumerable"
),
"true,false,true|true"
);
// An accessor half is defined by its own instruction and carries the same two attributes, so
// each kind is a row: a getter, a setter, and the pair written together.
assert_eq!(
run(
"var g = Object.getOwnPropertyDescriptor(class C { get a() { return 1; } }.prototype, 'a'); \
var s = Object.getOwnPropertyDescriptor(class C { set a(v) {} }.prototype, 'a'); \
var b = Object.getOwnPropertyDescriptor( \
class C { get a() { return 1; } set a(v) {} }.prototype, 'a'); \
[g.enumerable, g.configurable, typeof g.get, typeof g.set].join(',') + '|' + \
[s.enumerable, s.configurable, typeof s.get, typeof s.set].join(',') + '|' + \
[typeof b.get, typeof b.set].join(',')"
),
// §10.1.6.3 leaves an absent field alone, so the pair is one property with both halves.
"false,true,function,undefined|false,true,undefined,function|function,function"
);
}
#[test]
fn a_static_member_named_prototype_is_refused_by_the_ordinary_rules() {
// §15.7.14 gives the constructor a `prototype` that is neither writable nor configurable, and
// §15.4.5 and §15.7.10 both define a static member with a form that **throws** when the define
// is refused. So there is no check for the name anywhere: `static ["prototype"]` collides with
// a property that is already there and cannot be replaced.
//
// Written out it is an early error instead — `static prototype() {}` never parses — which is
// why only the computed form reaches this rule at all.
for member in [
"static ['prototype']() {}",
"static get ['prototype']() {}",
"static set ['prototype'](v) {}",
"static *['prototype']() {}",
"static ['prototype'] = 1;",
] {
assert_eq!(
run(&format!(
"(function () {{ try {{ (class {{ {member} }}); return 'no throw' }} \
catch (e) {{ return e.constructor.name }} }})()"
)),
"TypeError",
"for `{member}`"
);
}
// The **prototype** side takes the same name happily: it is the constructor that already has a
// `prototype`, and `C.prototype.prototype` is an ordinary property nothing has claimed.
assert_eq!(
run(
"(function () { class C { ['prototype']() { return 1; } } \n return typeof C.prototype.prototype; })()"
),
"function"
);
// And a static member under any other name is defined as before, which is what keeps this from
// reading as "a static computed member is refused".
assert_eq!(
run("(function () { class C { static ['x']() { return 1; } } return C.x(); })()"),
"1"
);
}
#[test]
fn a_field_that_lands_on_a_sealed_object_throws_rather_than_vanishing() {
// §15.7.10 defines a public field with `CreateDataPropertyOrThrow`, and the second half of the
// name is reachable: a field initialiser can seal `this` before the next field is defined.
// Discarding the refusal instead made the field silently absent.
assert_eq!(
run(
"(function () { class C { f = Object.freeze(this); g = 1; } \n try { new C(); return 'no throw' } catch (e) { return e.constructor.name } })()"
),
"TypeError"
);
// An object literal reaches the same instruction and can never be refused there — it defines
// onto something made a moment earlier — which is why this behaviour is invisible until a
// class field defines onto a constructor or onto a sealed instance.
assert_eq!(
run("(function () { var o = { a: 1, a: 2, ['b']: 3 }; return o.a + ',' + o.b; })()"),
"2,3"
);
}