-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathspec.emu
3161 lines (2945 loc) · 134 KB
/
spec.emu
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<meta charset="utf8">
<pre class="metadata">
title: Pattern Matching
stage: 1
contributors:
- Daniel Rosenwasser
- Jack Works
- Jordan Harband
- Mark Cohen
- Ross Kirsling
- Tab Atkins
</pre>
<style>
#welcome .secnum, [title="Welcome"] .secnum {
display: none;
}
emu-note[code] > .note, #welcome > h1 {
font-size: 0;
}
emu-note[code] > .note::before {
content: "Example";
font-size: 18px;
}
emu-note[code] pre {
margin: 0;
}
pre.inline {
display: inline;
}
pre.inline code {
display: inline;
font-style: italic;
text-decoration: underline;
}
body.folded .fold, body.folded .todo {
display: none;
}
.show-ao-annotations a.e-user-code::before, .show-ao-annotations span.e-user-code::before {
display: inline-block;
}
#sec-todos {
border-left: 5px solid #ff6600;
padding: 0.5em;
background: #ffeedd;
}
emu-intro {
margin-top: 1em !important;
}
[role="button"] {
cursor: pointer;
}
</style>
<emu-intro id="welcome">
<h1>Welcome</h1>
<emu-intro id="sec-todos">
<h1 class="attributes-tag">TODOs</h1>
<ul>
<li>
Scope and bindings
<ul>
<li>Basic case.</li>
<li>Work with `for` loop (CreatePerIterationEnvironment).</li>
<li>Work with function parameters.</li>
</ul>
</li>
</ul>
</emu-intro>
<emu-intro id="sec-nav">
<h1>Introduction</h1>
<p>This specification consists of the following parts:</p>
<ul>
<li><emu-xref href="#sec-pattern-matching">Patterns in pattern matching</emu-xref></li>
<li>Interesting AOs:
<ul>
<li><emu-xref href="#sec-invoke-custom-matcher" title></emu-xref></li>
<li><emu-xref href="#sec-pattern-match-cache-note">Cache semantics</emu-xref></li>
</ul>
</li>
<li><emu-xref href="#sec-relational-operators" title>The `is` expression</emu-xref></li>
<li><emu-xref href="#sec-match-expression" title></emu-xref></li>
<li>
Non-trivial Built-in %Symbol.customMatcher% methods:
<ul>
<li><emu-xref href="#sec-function.prototype-%symbol.custommatcher%" title></emu-xref></li>
<li><emu-xref href="#sec-regexp.prototype-%symbol.custommatcher%" title></emu-xref></li>
</ul>
</li>
<li>(TODO) Scope analysis changes: <emu-xref href="#sec-syntax-directed-operations" title></emu-xref></li>
<li>`new` semantics changes:
<emu-xref href="#sec-initializeinstance"></emu-xref>,
<emu-xref href="#sec-weakly-hold-targets-processing-model"></emu-xref>, and
<emu-xref href="#sec-runtime-semantics-classdefinitionevaluation"></emu-xref>
</li>
<li>
Possible extensions:
<emu-xref href="#sec-for-in-and-for-of-statements" title></emu-xref> and
<emu-xref href="#sec-try-statement" title></emu-xref>
</li>
</ul>
<p>
Trivia built-in matchers are folded.
<a role="button" id="expand">Click to <span class="fold">not</span> show the trivia sections.</a>
</p>
<script defer async src="./assets/expand.js"></script>
</emu-intro>
<emu-intro id="sec-notes-layering">
<h1>Layering</h1>
<p>The pattern-matching champion group designed this proposal with a layering approach. It does not mean the proposal is an MVP. The champion group wishes to ship the proposal as a whole when possible, but we can drop some features if there is strong pushback from the committee.</p>
<p>This approach allows the champion group to consider how all features combine and also how the proposal should behave if any of the features are missing.</p>
<p>A feature will have a note if</p>
<ul>
<li>it is a convenient feature instead of a necessary feature.</li>
<li>not all champion group members represent the hope to include it.</li>
</ul>
</emu-intro>
</emu-intro>
<emu-clause class="fold" id="sec-overview" number="4">
<h1>Overview</h1>
<emu-clause id="sec-organization-of-this-specification" number="5">
<h1>Organization of This Specification</h1>
<p><ins>Clause <emu-xref href="#sec-pattern-matching"></emu-xref> describes the pattern-matching feature.</ins></p>
</emu-clause>
</emu-clause>
<emu-clause class="fold" id="sec-ecmascript-data-types-and-values" aoid="Type" number="6">
<h1>ECMAScript Data Types and Values</h1>
<emu-clause id="sec-ecmascript-language-types">
<h1>ECMAScript Language Types</h1>
<emu-clause id="sec-ecmascript-language-types-symbol-type" number="5">
<h1>The Symbol Type</h1>
<emu-clause id="sec-well-known-symbols">
<h1>Well-Known Symbols</h1>
<emu-table id="table-1" caption="Well-known Symbols">
<table>
<tbody>
<tr>
<th>
Specification Name
</th>
<th>
[[Description]]
</th>
<th>
Value and Purpose
</th>
</tr>
<tr>
<td>
<ins><dfn>%Symbol.customMatcher%</dfn></ins>
</td>
<td>
<ins>`"Symbol.customMatcher"`</ins>
</td>
<td>
<ins>A method that performs custom pattern matching semantics. Called by the semantics of the pattern-matching features.</ins>
</td>
</tr>
</tbody>
</table>
</emu-table>
</emu-clause>
</emu-clause>
<emu-clause id="sec-object-internal-methods-and-internal-slots" number="7">
<h1>Object Internal Methods and Internal Slots</h1>
<p><ins>All objects have an internal slot named [[ConstructedBy]], which is a List of ECMAScript language values.
This List represents the origin of the object. Initially, it is an empty List.</ins></p>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-abstract-operations" number="7">
<h1>Abstract Operations</h1>
<emu-clause id="sec-operations-on-objects" number="3">
<h1>Operations on Objects</h1>
<emu-clause oldids="sec-initializeinstanceelements" id="sec-initializeinstance" type="abstract operation" number="34">
<h1>
InitializeInstanceElements (
_O_: an Object,
_constructor_: an ECMAScript function object,
): either a normal completion containing ~unused~ or a throw completion
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _methods_ be the value of _constructor_.[[PrivateMethods]].
1. For each PrivateElement _method_ of _methods_, do
1. Perform ? PrivateMethodOrAccessorAdd(_O_, _method_).
1. Let _fields_ be the value of _constructor_.[[Fields]].
1. For each element _fieldRecord_ of _fields_, do
1. Perform ? DefineField(_O_, _fieldRecord_).
1. <ins>Append _constructor_ to _O_.[[ConstructedBy]].</ins>
1. Return ~unused~.
</emu-alg>
<emu-note type="editor">Rename this abstract operation to <dfn>InitializeInstance</dfn>.</emu-note>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause class="todo" id="sec-syntax-directed-operations">
<h1>Syntax-Directed Operations</h1>
<emu-clause id="sec-syntax-directed-operations-scope-analysis" number="2">
<h1>Scope Analysis</h1>
<emu-clause id="sec-static-semantics-boundnames" type="sdo" number="1">
<h1>Static Semantics: BoundNames ( ): a List of Strings</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-declarationpart" type="sdo" number="2">
<h1>Static Semantics: DeclarationPart ( ): a Parse Node</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-isconstantdeclaration" type="sdo" number="3">
<h1>Static Semantics: IsConstantDeclaration ( ): a Boolean</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-lexicallydeclarednames" type="sdo" number="4">
<h1>Static Semantics: LexicallyDeclaredNames ( ): a List of Strings</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-lexicallyscopeddeclarations" type="sdo" number="5">
<h1>Static Semantics: LexicallyScopedDeclarations ( ): a List of Parse Nodes</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-vardeclarednames" type="sdo" number="6">
<h1>Static Semantics: VarDeclaredNames ( ): a List of Strings</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-varscopeddeclarations" type="sdo" number="7">
<h1>Static Semantics: VarScopedDeclarations ( ): a List of Parse Nodes</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-toplevellexicallydeclarednames" type="sdo" number="8">
<h1>Static Semantics: TopLevelLexicallyDeclaredNames ( ): a List of Strings</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-toplevellexicallyscopeddeclarations" type="sdo" number="9">
<h1>Static Semantics: TopLevelLexicallyScopedDeclarations ( ): a List of Parse Nodes</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-toplevelvardeclarednames" type="sdo" number="10">
<h1>Static Semantics: TopLevelVarDeclaredNames ( ): a List of Strings</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-static-semantics-toplevelvarscopeddeclarations" type="sdo" number="11">
<h1>Static Semantics: TopLevelVarScopedDeclarations ( ): a List of Parse Nodes</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
</emu-clause>
<emu-clause id="sec-syntax-directed-operations-miscellaneous" number="6">
<h1>Miscellaneous</h1>
<emu-clause id="sec-runtime-semantics-bindinginitialization" type="sdo" number="2">
<h1>
Runtime Semantics: BindingInitialization (
_value_: an ECMAScript language value,
_environment_: an Environment Record or *undefined*,
): either a normal completion containing ~unused~ or an abrupt completion
</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
<emu-clause id="sec-runtime-semantics-iteratorbindinginitialization" type="sdo" number="3">
<h1>
Runtime Semantics: IteratorBindingInitialization (
_iteratorRecord_: an Iterator Record,
_environment_: an Environment Record or *undefined*,
): either a normal completion containing ~unused~ or an abrupt completion
</h1>
<dl class="header">
</dl>
<emu-note type="editor">
TODO: Scope Analysis.
</emu-note>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause class="fold" id="sec-executable-code-and-execution-contexts" number="9">
<h1>Executable Code and Execution Contexts</h1>
<emu-clause id="sec-weakly-hold-targets-processing-model" oldids="sec-weakref-processing-model" number="10">
<h1>Processing Model of <del>WeakRef and FinalizationRegistry</del><ins>weakly hold</ins> Targets</h1>
<emu-clause id="sec-weakly-hold-execution" oldids="sec-weakref-execution" number="3">
<h1>Execution</h1>
<p>At any time, if a set of objects and/or symbols _S_ is not live, an ECMAScript implementation may perform the following steps atomically:</p>
<emu-alg>
1. For each element _value_ of _S_, do
1. For each WeakRef _ref_ such that _ref_.[[WeakRefTarget]] is _value_, do
1. Set _ref_.[[WeakRefTarget]] to ~empty~.
1. For each FinalizationRegistry _fg_ such that _fg_.[[Cells]] contains a Record _cell_ such that _cell_.[[WeakRefTarget]] is _value_, do
1. Set _cell_.[[WeakRefTarget]] to ~empty~.
1. Optionally, perform HostEnqueueFinalizationRegistryCleanupJob(_fg_).
1. For each WeakMap _map_ such that _map_.[[WeakMapData]] contains a Record _r_ such that _r_.[[Key]] is _value_, do
1. Set _r_.[[Key]] to ~empty~.
1. Set _r_.[[Value]] to ~empty~.
1. For each WeakSet _set_ such that _set_.[[WeakSetData]] contains _value_, do
1. Replace the element of _set_.[[WeakSetData]] whose value is _value_ with an element whose value is ~empty~.
1. <ins>For each Object _o_ such that _o_.[[ConstructedBy]] contains _value_, do</ins>
1. <ins>Remove _value_ from _o_.[[ConstructedBy]].</ins>
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause class="fold" id="sec-ecmascript-language-lexical-grammar" number="12">
<h1>ECMAScript Language: Lexical Grammar</h1>
<emu-clause id="sec-automatic-semicolon-insertion" number="10">
<h1>Automatic Semicolon Insertion</h1>
<emu-clause id="sec-rules-of-automatic-semicolon-insertion" number="1">
<h1>Rules of Automatic Semicolon Insertion</h1>
<emu-note>
<p>The following are the additions of the restricted productions in the grammar:</p>
<emu-grammar>
RelationalExpression[In, Yield, Await] :
RelationalExpression [no LineTerminator here] `is` MatchPattern
MatchExpression[Yield, Await] :
CoverCallExpressionAndAsyncArrowHead[?Yield, ?Await] [no LineTerminator here] `{` MatchExpressionClauses[?Yield, ?Await] `;` `}`
MatchHead :
`match` [no LineTerminator here] `(` Expression `)`
</emu-grammar>
</emu-note>
</emu-clause>
<emu-clause id="sec-interesting-cases-of-automatic-semicolon-insertion" number="3">
<h1>Interesting Cases of Automatic Semicolon Insertion</h1>
<emu-clause id="sec-asi-cases-with-no-lineterminator-here" number="2">
<h1>Cases of Automatic Semicolon Insertion and “[no |LineTerminator| here]”</h1>
<emu-clause id="sec-no-lineterminator-here-automatic-semicolon-insertion-list" number="1">
<h1>List of Grammar Productions with Optional Operands and “[no |LineTerminator| here]”</h1>
<ul>
<li><ins>|MatchExpression|.</ins></li>
</ul>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-ecmascript-language-expressions" number="13">
<h1>ECMAScript Language: Expressions</h1>
<emu-clause id="sec-primary-expression" number="2">
<h1>Primary Expression</h1>
<h2>Syntax</h2>
<emu-grammar type="definition">
PrimaryExpression[Yield, Await] :
RegularExpressionLiteral
<ins>MatchExpression[?Yield, ?Await]</ins>
</emu-grammar>
<emu-clause id="sec-primary-expression-match-expression" number="10">
<h1>Match Expression</h1>
<p>See <emu-xref href="#sec-match-expression" title></emu-xref> for <emu-grammar>PrimaryExpression : MatchExpression</emu-grammar></p>
</emu-clause>
</emu-clause>
<emu-clause id="sec-relational-operators" number="10">
<h1>Relational Operators</h1>
<h2>Syntax</h2>
<emu-grammar type="definition">
RelationalExpression[In, Yield, Await] :
<ins>RelationalExpression[?In, ?Yield, ?Await] [no LineTerminator here] `is` MatchPattern[?Yield, ?Await]</ins>
</emu-grammar>
<emu-note code>
<pre><code class="javascript">
const isOk = response is { ok: true, status: > 200 and < 400 };
</code></pre>
</emu-note>
<emu-note type="editor">
This feature can be replaced by <emu-xref href="#sec-match-expression" title></emu-xref>.
The code example above can be written as:
<pre><code class="javascript">
const isOk = match (response) {
{ ok: true, status: > 200 and < 400 }: true,
default: false
};
</code></pre>
</emu-note>
<emu-note type="editor">
We may need to use a non-contextual keyword like `~=` instead of a contextual keyword like `is`. See <a
href="https://github.com/waldemarhorwat/syntax/blob/main/contextual-keywords.md?rgh-link-date=2024-09-06T16%3A43%3A25Z">waldemarhorwat/syntax@main/contextual-keywords.md</a>
and <a href="https://github.com/tc39/proposal-pattern-matching/issues/323">Syntax effects on rest of the language</a>.
</emu-note>
<emu-clause id="sec-relational-operators-runtime-semantics-evaluation" number="1">
<h1>Runtime Semantics: Evaluation</h1>
<emu-grammar>
<ins>RelationalExpression : RelationalExpression `is` MatchPattern</ins>
</emu-grammar>
<emu-alg>
1. Let _lref_ be ? Evaluation of |RelationalExpression|.
1. Let _lval_ be ? GetValue(_lref_).
1. Let _cacheGroup_ be CreateMatchCache().
1. Let _matchCompletion_ be ? MatchPatternMatches of |MatchPattern| with argument _lval_ and _cacheGroup_.
1. If _matchCompletion_ is a normal completion, then
1. If _matchCompletion_.[[Value]] is ~not-matched~, set _matchCompletion_ to NormalCompletion(*false*).
1. Else, set _matchCompletion_ to NormalCompletion(*true*).
1. Let _result_ be Completion(FinishMatch(_matchCompletion_, _cacheGroup_)).
1. Assert: _result_ is a normal completion or an abrupt completion.
1. Return _result_.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-ecmascript-language-statements-and-declarations" number="14">
<h1>ECMAScript Language: Statements and Declarations</h1>
<h2>Syntax</h2>
<emu-clause id="sec-iteration-statements" number="7">
<h1>Iteration Statements</h1>
<emu-clause id="sec-for-in-and-for-of-statements">
<h1>The `for`-`in`, `for`-`of`, and `for`-`await`-`of` Statements</h1>
<emu-note type="editor">
It is possible to add pattern-matching to the `for` iteration statements. It might look like this:
<pre><code class="javascript">
for (const response of responses) {
if (item is { ok: true, let body }) {
}
}
// can be written as
for (const response is { ok: true, let body } of responses) {
}
// or
for (const response of responses matches { ok: true, let body }) {
}
</code></pre>
</emu-note>
</emu-clause>
</emu-clause>
<emu-clause id="sec-try-statement" number="14">
<h1>The `try` Statement</h1>
<emu-note type="editor">
It is possible to add pattern-matching to the `try` statement. It might look like this:
<pre><code class="javascript">
try { }
catch (error) {
if (error is { message: /JSON/ }) { return null; }
throw error;
}
// can be written as
try { }
catch (error is { message: /JSON/ }) { return null; }
// unmatched error will be re-thrown.
</code></pre>
</emu-note>
</emu-clause>
</emu-clause>
<emu-clause id="sec-ecmascript-language-functions-and-classes" number="15">
<h1>ECMAScript Language: Functions and Classes</h1>
<emu-clause id="sec-class-definitions" number="7">
<h1>Class Definitions</h1>
<emu-clause id="sec-runtime-semantics-classdefinitionevaluation" type="sdo" number="14">
<h1>
Runtime Semantics: ClassDefinitionEvaluation (
_classBinding_: a String or *undefined*,
_className_: a property key or a Private Name,
): either a normal completion containing a function object or an abrupt completion
</h1>
<dl class="header">
</dl>
<emu-note type="editor">
See <a href="https://github.com/tc39/ecma262/pull/3212/files" target="_blank">Editorial: call MakeClassConstructor on default class constructor</a>.
</emu-note>
</emu-clause>
</emu-clause>
<emu-clause class="fold" id="sec-tail-position-calls" number="10">
<h1>Tail Position Calls</h1>
<emu-clause id="sec-static-semantics-hascallintailposition" number="2" type="sdo">
<h1>
Static Semantics: HasCallInTailPosition (
_call_: a |CallExpression| Parse Node, a |MemberExpression| Parse Node, or an |OptionalChain| Parse Node,
): a Boolean
</h1>
<dl class="header">
</dl>
<emu-grammar>
<ins>RelationalExpression : RelationalExpression `is` MatchPattern</ins>
</emu-grammar>
<emu-alg>
1. Return *false*.
</emu-alg>
<emu-grammar>
<ins>PrimaryExpression : MatchExpression</ins>
</emu-grammar>
<emu-alg>
1. Return HasCallInTailPosition of |MatchExpression| with argument _call_.
</emu-alg>
<emu-grammar>
<ins>MatchExpression : CoverCallExpressionAndAsyncArrowHead `{` MatchExpressionClauses `;` `}`</ins>
</emu-grammar>
<emu-alg>
1. Return HasCallInTailPosition of |MatchExpressionClauses| with argument _call_.
</emu-alg>
<emu-grammar>
<ins>MatchExpressionClauses : MatchExpressionClause</ins>
</emu-grammar>
<emu-alg>
1. Return HasCallInTailPosition of |MatchExpressionClause| with argument _call_.
</emu-alg>
<emu-grammar>
<ins>MatchExpressionClauses : MatchExpressionClauses `;` MatchExpressionClause</ins>
</emu-grammar>
<emu-alg>
1. Let _result_ be HasCallInTailPosition of |MatchExpressionClauses| with argument _call_.
1. If _result_ is *true*, return *true*.
1. Return HasCallInTailPosition of |MatchExpressionClause| with argument _call_.
</emu-alg>
<emu-grammar>
<ins>MatchExpressionClauses : MatchExpressionClauses `;` `default` `:` Expression</ins>
</emu-grammar>
<emu-alg>
1. Let _result_ be HasCallInTailPosition of |MatchExpressionClauses| with argument _call_.
1. If _result_ is *true*, return *true*.
1. Return HasCallInTailPosition of |Expression| with argument _call_.
</emu-alg>
<emu-grammar>
<ins>MatchExpressionClauses : `default` `:` Expression</ins>
</emu-grammar>
<emu-alg>
1. Return HasCallInTailPosition of |Expression| with argument _call_.
</emu-alg>
<emu-grammar>
<ins>MatchExpressionClause : MatchPattern `:` Expression</ins>
</emu-grammar>
<emu-alg>
1. Return HasCallInTailPosition of |Expression| with argument _call_.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-fundamental-objects" number="20">
<h1>Fundamental Objects</h1>
<emu-clause class="fold" id="sec-object-objects" number="1">
<h1>Object Objects</h1>
<emu-clause id="sec-properties-of-the-object-constructor" number="2">
<h1>Properties of the Object Constructor</h1>
<emu-clause id="sec-object-%symbol.custommatcher%" number="24">
<h1>Object [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_, ~boolean~).
1. If _subject_ is not an Object, return *false*.
1. Return *true*.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-function-objects" number="2">
<h1>Function Objects</h1>
<emu-clause id="sec-properties-of-the-function-constructor" number="2">
<h1>Properties of the Function Constructor</h1>
<emu-clause id="sec-function-%symbol.custommatcher%" number="2">
<h1>Function [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_, ~boolean~).
1. Return IsCallable(_subject_).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-the-function-prototype-object" number="3">
<h1>Properties of the Function Prototype Object</h1>
<emu-clause id="sec-function.prototype-%symbol.custommatcher%" number="7">
<h1>Function.prototype [ %Symbol.customMatcher% ] ( _subject_, _hint_, _receiver_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_).
1. Let _func_ be the *this* value.
1. If IsCallable(_func_) is false, throw a *TypeError* exception.
1. If _subject_.[[ConstructedBy]] contains _func_, return *true*.
1. If _func_ does not have a [[IsClassConstructor]] internal slot or _func_.[[IsClassConstructor]] is *false*, return ? Call(_func_, _receiver_, « _subject_, _hint_ »).
1. Return *false*.
</emu-alg>
<emu-note code><pre><code class="javascript">
// For non-class functions.
[] is Array.isArray; // true, by Array.isArray(expr)
// For objects created by `new`, it uses private-field-like semantics.
class MyError extends Error {}
const myError = new MyError();
myError is MyError; // true
myError is Error; // true
Object.create(MyError.prototype) is MyError; // false
// Also works for normal functions
function ES5StyleClass() {}
new ES5StyleClass() is ES5StyleClass; // true
Object.create(ES5StyleClass.prototype) is ES5StyleClass; // false
</code></pre></emu-note>
<emu-note type="editor">
<p>This does not work with ES5 style class inherit.</p>
<pre><code class="javascript">
function MyError() {
Error.call(this);
}
MyError.prototype = Object.create(Error.prototype);
var error = new MyError();
error is MyError; // true
error is Error; // false
</code></pre>
</emu-note>
<emu-note type="editor">
<p>Not everyone in the champion group agrees with private-field-like brand check semantics.</p>
<p>
There are
<a href="https://github.com/tc39/proposal-pattern-matching/pull/293#issuecomment-1725097699" target="_blank">performance concerns</a>,
<a href="https://github.com/tc39/proposal-pattern-matching/pull/293#issuecomment-1725097699" target="_blank">"hackable" concerns</a>, and
<a href="https://github.com/tc39/proposal-pattern-matching/pull/293#issuecomment-1725097699" target="_blank">interaction
with %Symbol.hasInstance% concerns</a>.
</p>
<p>Another approach is to use the `instanceof` semantics.</p>
</emu-note>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause class="fold" id="sec-boolean-objects" number="3">
<h1>Boolean Objects</h1>
<emu-clause id="sec-properties-of-the-boolean-constructor" number="2">
<h1>Properties of the Boolean Constructor</h1>
<emu-clause id="sec-boolean-%symbol.custommatcher%" number="2">
<h1>Boolean [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_).
1. If _subject_ is not a Boolean and does not have a [[BooleanData]] internal slot, return *false*.
1. If _hint_ is *"boolean"*, return *true*.
1. If _subject_ is a Boolean, return CreateArrayFromList(« _subject_ »).
1. Return CreateArrayFromList(« _subject_.[[BooleanData]] »).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-note type="editor">
Another approach is to ignore boxed primitives and only match primitive values.
</emu-note>
</emu-clause>
<emu-clause class="fold" id="sec-symbol-objects" number="4">
<h1>Symbol Objects</h1>
<emu-clause id="sec-properties-of-the-symbol-constructor" number="2">
<h1>Properties of the Symbol Constructor</h1>
<emu-clause id="sec-symbol.custommatcher" number="17">
<h1>Symbol.customMatcher</h1>
<p>The initial value of `Symbol.customMatcher` is the well-known symbol %Symbol.customMatcher% (<emu-xref
href="#table-well-known-symbols"></emu-xref>).</p>
<p>This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *false* }.</p>
</emu-clause>
<emu-clause id="sec-symbol-%symbol.custommatcher%">
<h1>Symbol [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_).
1. If _subject_ is not a Symbol and does not have a [[SymbolData]] internal slot, return *false*.
1. If _hint_ is *"boolean"*, return *true*.
1. If _subject_ is a Symbol, return CreateArrayFromList(« _subject_ »).
1. Return CreateArrayFromList(« _subject_.[[SymbolData]] »).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-note type="editor">
Another approach is to ignore boxed primitives and only match primitive values.
</emu-note>
</emu-clause>
<emu-clause class="fold" id="sec-error-objects" number="5">
<h1>Error Objects</h1>
<emu-clause id="sec-error-constructor" number="1">
<h1>The Error Constructor</h1>
<emu-clause id="sec-error-message">
<h1>Error ( _message_ [ , _options_ ] )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. If NewTarget is *undefined*, let _newTarget_ be the active function object; else let _newTarget_ be NewTarget.
1. Let _O_ be ? OrdinaryCreateFromConstructor(_newTarget_, *"%Error.prototype%"*, « [[ErrorData]] »).
1. <ins>Set _O_.[[ErrorData]] to *"Error"*.</ins>
1. If _message_ is not *undefined*, then
1. Let _msg_ be ? ToString(_message_).
1. Perform CreateNonEnumerableDataPropertyOrThrow(_O_, *"message"*, _msg_).
1. Perform ? InstallErrorCause(_O_, _options_).
1. Return _O_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-the-error-constructors" number="2">
<h1>Properties of the Error Constructor</h1>
<emu-clause id="sec-error-%symbol.custommatcher%" number="2">
<h1>Error [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_, ~boolean~).
1. If _subject_ does not have a [[ErrorData]] internal slot, return *false*.
1. Return *true*.
</emu-alg>
</emu-clause>
<emu-note type="editor">
It is possible to provide extractor semantics for Error matchers.
<pre><code class="javascript">
if (expr is Error(let message, { let cause })) {}
</code></pre>
</emu-note>
</emu-clause>
<emu-clause id="sec-properties-of-error-instances" number="4">
<h1>Properties of Error Instances</h1>
<p>Error instances are ordinary objects that inherit properties from the Error prototype object and have an
[[ErrorData]] internal slot whose value is <del>*undefined*</del> <ins>a String</ins>. The only specified uses of
[[ErrorData]] is to identify Error, AggregateError, and _NativeError_ instances as Error objects within
`Object.prototype.toString` <ins>and their %Symbol.customMatcher% methods</ins>.</p>
<emu-note type="editor">
Rename this internal slot to [[ErrorKind]].
</emu-note>
</emu-clause>
<emu-clause id="sec-nativeerror-object-structure" number="6">
<h1>_NativeError_ Object Structure</h1>
<emu-clause id="sec-nativeerror-constructors" number="1">
<h1>The _NativeError_ Constructors</h1>
<emu-clause id="sec-nativeerror" number="1">
<h1>_NativeError_ ( _message_ [ , _options_ ] )</h1>
<p>Each _NativeError_ function performs the following steps when called:</p>
<emu-alg>
1. If NewTarget is *undefined*, let _newTarget_ be the active function object; else let _newTarget_ be NewTarget.
1. Let _O_ be ? OrdinaryCreateFromConstructor(_newTarget_, <code>"%<var>NativeError</var>.prototype%"</code>, « [[ErrorData]] »).
1. <ins>Set _O_.[[ErrorData]] to _NativeError_.</ins>
1. If _message_ is not *undefined*, then
1. Let _msg_ be ? ToString(_message_).
1. Perform CreateNonEnumerableDataPropertyOrThrow(_O_, *"message"*, _msg_).
1. Perform ? InstallErrorCause(_O_, _options_).
1. Return _O_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-the-nativeerror-constructors" number="2">
<h1>Properties of the _NativeError_ Constructors</h1>
<emu-clause id="sec-nativeerror-%symbol.custommatcher%" number="2">
<h1>_NativeError_ [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_, ~boolean~).
1. If _subject_ does not have a [[ErrorData]] internal slot or _subject_.[[ErrorData]] is not _NativeError_, return *false*.
1. Return *true*.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-nativeerror-instances" number="4">
<h1>Properties of _NativeError_ Instances</h1>
<p>_NativeError_ instances are ordinary objects that inherit properties from their _NativeError_ prototype object and
have an [[ErrorData]] internal slot whose value is <del>*undefined*</del> <ins>a String</ins>. The only specified use
of [[ErrorData]] is by `Object.prototype.toString` (<emu-xref href="#sec-object.prototype.tostring"></emu-xref>)
<ins>and their %Symbol.customMatcher% methods</ins> to identify Error, AggregateError, or _NativeError_ instances.
</p>
</emu-clause>
</emu-clause>
<emu-clause id="sec-aggregate-error-objects" number="7">
<h1>AggregateError Objects</h1>
<emu-clause id="sec-aggregate-error-constructor" number="1">
<h1>The AggregateError Constructor</h1>
<emu-clause id="sec-aggregate-error">
<h1>AggregateError ( _errors_, _message_ [ , _options_ ] )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. If NewTarget is *undefined*, let _newTarget_ be the active function object; else let _newTarget_ be NewTarget.
1. Let _O_ be ? OrdinaryCreateFromConstructor(_newTarget_, *"%AggregateError.prototype%"*, « [[ErrorData]] »).
1. <ins>Set _O_.[[ErrorData]] to *"AggregateError"*.</ins>
1. If _message_ is not *undefined*, then
1. Let _msg_ be ? ToString(_message_).
1. Perform CreateNonEnumerableDataPropertyOrThrow(_O_, *"message"*, _msg_).
1. Perform ? InstallErrorCause(_O_, _options_).
1. Let _errorsList_ be ? IteratorToList(? GetIterator(_errors_, ~sync~)).
1. Perform ! DefinePropertyOrThrow(_O_, *"errors"*, PropertyDescriptor { [[Configurable]]: *true*, [[Enumerable]]: *false*, [[Writable]]: *true*, [[Value]]: CreateArrayFromList(_errorsList_) }).
1. Return _O_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-the-aggregate-error-constructors" number="2">
<h1>Properties of the AggregateError Constructor</h1>
<emu-clause id="sec-aggregate-error-%symbol.custommatcher%" number="2">
<h1>AggregateError [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_, ~boolean~).
1. If _subject_ does not have a [[ErrorData]] internal slot or _subject_.[[ErrorData]] is not *"AggregateError"*, return *false*.
1. Return *true*.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-aggregate-error-instances" number="4">
<h1>Properties of AggregateError Instances</h1>
<p>AggregateError instances are ordinary objects that inherit properties from their AggregateError prototype object and
have an [[ErrorData]] internal slot whose value is <del>*undefined*</del> <ins>a String</ins>. The only specified use
of [[ErrorData]] is by `Object.prototype.toString` (<emu-xref href="#sec-object.prototype.tostring"></emu-xref>)
<ins>and their %Symbol.customMatcher% methods</ins> to identify Error, AggregateError, or _NativeError_ instances.
</p>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause class="fold" id="sec-numbers-and-dates" number="21">
<h1>Numbers and Dates</h1>
<emu-clause id="sec-number-objects" number="1">
<h1>Number Objects</h1>
<emu-clause id="sec-properties-of-the-number-constructor" number="2">
<h1>Properties of the Number Constructor</h1>
<emu-clause id="sec-number-%symbol.custommatcher%" number="16">
<h1>Number [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_).
1. If _subject_ is not a Number and does not have a [[NumberData]] internal slot, return *false*.
1. If _hint_ is *"boolean"*, return *true*.
1. If _subject_ is a Number, return CreateArrayFromList(« _subject_ »).
1. Return CreateArrayFromList(« _subject_.[[NumberData]] »).
</emu-alg>
</emu-clause>
<emu-note type="editor">
Another approach is to ignore boxed primitives and only match primitive values.
</emu-note>
</emu-clause>
</emu-clause>
<emu-clause id="sec-bigint-objects" number="2">
<h1>BigInt Objects</h1>
<emu-clause id="sec-properties-of-the-bigint-constructor" number="2">
<h1>Properties of the BigInt Constructor</h1>
<emu-clause id="sec-bigint-%symbol.custommatcher%" number="4">
<h1>BigInt [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_).
1. If _subject_ is not a BigInt and does not have a [[BigIntData]] internal slot, return *false*.
1. If _hint_ is *"boolean"*, return *true*.
1. If _subject_ is a BigInt, return CreateArrayFromList(« _subject_ »).
1. Return CreateArrayFromList(« _subject_.[[BigIntData]] »).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-note type="editor">
Another approach is to ignore boxed primitives and only match primitive values.
</emu-note>
</emu-clause>
<emu-clause id="sec-date-objects" number="4">
<h1>Date Objects</h1>
<emu-clause id="sec-properties-of-the-date-constructor" number="3">
<h1>Properties of the Date Constructor</h1>
<emu-clause id="sec-date-%symbol.custommatcher%" number="5">
<h1>Date [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_, ~boolean~).
1. If _subject_ does not have a [[DateValue]] internal slot, return *false*.
1. Return *true*.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause class="fold" id="sec-text-processing" number="22">
<h1>Text Processing</h1>
<emu-clause id="sec-string-objects" number="1">
<h1>String Objects</h1>
<emu-clause id="sec-properties-of-the-string-constructor" number="2">
<h1>Properties of the String Constructor</h1>
<emu-clause id="sec-string-%symbol.custommatcher%" number="5">
<h1>String [ %Symbol.customMatcher% ] ( _subject_, _hint_ )</h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Perform ? ValidateCustomMatcherHint(_hint_).
1. If _subject_ is not a String and does not have a [[StringData]] internal slot, return *false*.
1. If _hint_ is *"boolean"*, return *true*.
1. If _subject_ is a String, return CreateArrayFromList(« _subject_ »).
1. Return CreateArrayFromList(« _subject_.[[StringData]] »).
</emu-alg>