This repository was archived by the owner on Aug 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathspec.html
1357 lines (1274 loc) · 74.5 KB
/
spec.html
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="utf-8">
<pre class="metadata">
title: 'Top-Level Await'
status: proposal
stage: 3
location: https://tc39.github.io/proposal-top-level-await/
copyright: false
contributors: Myles Borins, Yulia Startsev, Daniel Ehrenberg, Guy Bedford, Ms2ger, and others.
</pre>
<script src="ecmarkup.js" defer></script>
<link rel="stylesheet" href="ecmarkup.css">
<emu-intro id="introduction">
<h1>Introduction</h1>
<p>Top-Level Await allows the `await` keyword to be used at the top level of the module goal. See <a href="https://github.com/tc39/proposal-top-level-await/blob/HEAD/README.md">the explainer</a> for the motivation, context, and high-level semantics.</p>
</emu-intro>
<emu-clause id="sec-async-functions-abstract-operations-async-function-start" aoid="AsyncFunctionStart">
<h1>AsyncFunctionStart ( _promiseCapability_, _asyncFunctionBody_ )</h1>
<p>The abstract operation AsyncFunctionStart takes arguments _promiseCapability_ and _asyncFunctionBody_. It performs the following steps when called:</p>
<emu-alg>
1. Let _runningContext_ be the running execution context.
1. Let _asyncContext_ be a copy of _runningContext_.
1. NOTE: Copying the execution state is required for <del>the step below</del><ins>AsyncBlockStart</ins> to resume its execution. It is ill-defined to resume a currently executing context.
1. <ins>Perform ! AsyncBlockStart(_promiseCapability_, _asyncFunctionBody_, _asyncContext_).</ins>
1. <del>Set the code evaluation state of _asyncContext_ such that when evaluation is resumed for that execution context the following steps will be performed:</del>
1. <del>Let _result_ be the result of evaluating _asyncFunctionBody_.</del>
1. <del>Assert: If we return here, the async function either threw an exception or performed an implicit or explicit return; all awaiting is done.</del>
1. <del>Remove _asyncContext_ from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.</del>
1. <del>If _result_.[[Type]] is ~normal~, then</del>
1. <del>Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « *undefined* »).</del>
1. <del>Else if _result_.[[Type]] is ~return~, then</del>
1. <del>Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _result_.[[Value]] »).</del>
1. <del>Else,</del>
1. <del>Assert: _result_.[[Type]] is ~throw~.</del>
1. <del>Perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _result_.[[Value]] »).</del>
1. <del>Return.</del>
1. <del>Push _asyncContext_ onto the execution context stack; _asyncContext_ is now the running execution context.</del>
1. <del>Resume the suspended evaluation of _asyncContext_. Let _result_ be the value returned by the resumed computation.</del>
1. <del>Assert: When we return here, _asyncContext_ has already been removed from the execution context stack and _runningContext_ is the currently running execution context.</del>
1. <del>Assert: _result_ is a normal completion with a value of *undefined*. The possible sources of completion values are Await or, if the async function doesn't await anything, the step 4.g above.</del>
1. Return.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asyncblockstart" aoid="AsyncBlockStart">
<h1><ins>AsyncBlockStart ( _promiseCapability_, _asyncBody_, _asyncContext_ )</ins></h1>
<emu-alg>
1. Assert: _promiseCapability_ is a PromiseCapability Record.
1. Let _runningContext_ be the running execution context.
1. Set the code evaluation state of _asyncContext_ such that when evaluation is resumed for that execution context the following steps will be performed:
1. Let _result_ be the result of evaluating _asyncBody_.
1. Assert: If we return here, the async function either threw an exception or performed an implicit or explicit return; all awaiting is done.
1. Remove _asyncContext_ from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
1. If _result_.[[Type]] is ~normal~, then
1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « *undefined* »).
1. Else if _result_.[[Type]] is ~return~, then
1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _result_.[[Value]] »).
1. Else,
1. Assert: _result_.[[Type]] is ~throw~.
1. Perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _result_.[[Value]] »).
1. Return.
1. Push _asyncContext_ onto the execution context stack; _asyncContext_ is now the running execution context.
1. Resume the suspended evaluation of _asyncContext_. Let _result_ be the value returned by the resumed computation.
1. Assert: When we return here, _asyncContext_ has already been removed from the execution context stack and _runningContext_ is the currently running execution context.
1. Assert: _result_ is a normal completion with a value of *undefined*. The possible sources of completion values are Await or, if the async function doesn't await anything, the step 2.g above.
</emu-alg>
</emu-clause>
<emu-clause id="sec-hostimportmoduledynamically" aoid="HostImportModuleDynamically">
<h1>HostImportModuleDynamically ( _referencingScriptOrModule_, _specifier_, _promiseCapability_ )</h1>
<p>HostImportModuleDynamically is a host-defined abstract operation that performs any necessary setup work in order to make available the module corresponding to the |ModuleSpecifier| String, _specifier_, occurring within the context of the script or module represented by the Script Record or Module Record _referencingScriptOrModule_. (_referencingScriptOrModule_ may also be *null*, if there is no active script or module when the <emu-xref href="#sec-import-calls">`import()`</emu-xref> expression occurs.) It then performs FinishDynamicImport to finish the dynamic import process.</p>
<p>The implementation of HostImportModuleDynamically must conform to the following requirements:</p>
<ul>
<li>
The abstract operation must always complete normally with *undefined*. Success or failure must instead be signaled as discussed below.
</li>
<li>
The host environment must conform to one of the two following sets of requirements:
<dl>
<dt>Success path</dt>
<dd>
<ul>
<li>At some future time, the host environment must perform FinishDynamicImport(_referencingScriptOrModule_, _specifier_, _promiseCapability_, <del>NormalCompletion(*undefined*))</del><ins>_promise_), where _promise_ is a Promise resolved with *undefined*.</ins></li>
<li>Any subsequent call to HostResolveImportedModule after FinishDynamicImport has completed, given the arguments _referencingScriptOrModule_ and _specifier_, must complete normally.</li>
<li>The completion value of any subsequent call to HostResolveImportedModule after FinishDynamicImport has completed, given the arguments _referencingScriptOrModule_ and _specifier_, must be a module which has already been evaluated, i.e. whose Evaluate concrete method has already been called and returned a normal completion.</li>
</ul>
</dd>
<dt>Failure path</dt>
<dd>
<ul>
<li>At some future time, the host environment must perform FinishDynamicImport(_referencingScriptOrModule_, _specifier_, _promiseCapability_, <del>an abrupt completion), with the abrupt completion representing the cause of failure.</del><ins>_promise_), where _promise_ is a Promise rejected with an error representing the cause of failure.</ins></li>
</ul>
</dd>
</dl>
</li>
<li>
If the host environment takes the success path once for a given _referencingScriptOrModule_, _specifier_ pair, it must always do so for subsequent calls.
</li>
<li>
The operation must not call _promiseCapability_.[[Resolve]] or _promiseCapability_.[[Reject]], but instead must treat _promiseCapability_ as an opaque identifying value to be passed through to FinishDynamicImport.
</li>
</ul>
<p>The actual process performed is host-defined, but typically consists of performing whatever I/O operations are necessary to allow HostResolveImportedModule to synchronously retrieve the appropriate Module Record, and then calling its Evaluate concrete method. This might require performing similar normalization as HostResolveImportedModule does.</p>
</emu-clause>
<emu-clause id="sec-finishdynamicimport" aoid="FinishDynamicImport">
<h1>FinishDynamicImport ( _referencingScriptOrModule_, _specifier_, _promiseCapability_, <del>_completion_</del><ins>_innerPromise_</ins> )</h1>
<p>The abstract operation FinishDynamicImport takes arguments _referencingScriptOrModule_, _specifier_, _promiseCapability_ (a PromiseCapability Record), and _innerPromise_. FinishDynamicImport completes the process of a dynamic import originally started by an <emu-xref href="#sec-import-calls">`import()`</emu-xref> call, resolving or rejecting the promise returned by that call as appropriate according to _innerPromise_. It is performed by host environments as part of HostImportModuleDynamically. It performs the following steps when called:</p>
<emu-alg>
1. <del>If _completion_ is an abrupt completion, then perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _completion_.[[Value]] »).</del>
1. <del>Else,</del>
1. <del>Assert: _completion_ is a normal completion and _completion_.[[Value]] is *undefined*.</del>
1. <del>Let _moduleRecord_ be ! HostResolveImportedModule(_referencingScriptOrModule_, _specifier_).</del>
1. <del>Assert: Evaluate has already been invoked on _moduleRecord_ and successfully completed.</del>
1. <del>Let _namespace_ be GetModuleNamespace(_moduleRecord_).</del>
1. <del>If _namespace_ is an abrupt completion, perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _namespace_.[[Value]] »).</del>
1. <del>Else, perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _namespace_.[[Value]] »).</del>
1. <ins>Let _stepsFulfilled_ be the steps of a CallDynamicImportResolved function as specified below.</ins>
1. <ins>Let _onFulfilled_ be CreateBuiltinFunction(_stepsFulfilled_, « »).</ins>
1. <ins>Let _stepsRejected_ be the steps of a CallDynamicImportRejected function as specified below.</ins>
1. <ins>Let _onRejected_ be CreateBuiltinFunction(_stepsRejected_, « »).</ins>
1. <ins>Perform ! PerformPromiseThen(_innerPromise_.[[Promise]], _onFulfilled_, _onRejected_).</ins>
</emu-alg>
<p><ins>A CallDynamicImportResolved function is an anonymous built-in function with no internal slots and has one argument _result_. When a CallDynamicImportResolved function is called it performs the following steps:</ins></p>
<emu-alg>
1. <ins>Assert: _result_ is *undefined*.</ins>
1. <ins>Let _moduleRecord_ be ! HostResolveImportedModule(_referencingScriptOrModule_, _specifier_).</ins>
1. <ins>Assert: Evaluate has already been invoked on _moduleRecord_ and successfully completed.</ins>
1. <ins>Let _namespace_ be GetModuleNamespace(_moduleRecord_).</ins>
1. <ins>If _namespace_ is an abrupt completion, perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _namespace_.[[Value]] »).</ins>
1. <ins>Else, perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _namespace_.[[Value]] »).</ins>
</emu-alg>
<p><ins>A CallDynamicImportRejected function is an anonymous built-in function with no internal slots and has one argument _error_. When a CallDynamicImportRejected function is called it performs the following steps:</ins></p>
<emu-alg>
1. <ins>Perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _error_ »).</ins>
</emu-alg>
</emu-clause>
<emu-clause id="sec-abstract-module-records">
<h1>Abstract Module Records</h1>
<emu-table id="table-36" caption="Module Record Fields">
<table>
<thead>
<tr>
<th>
Field Name
</th>
<th>
Value Type
</th>
<th>
Meaning
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
[[Realm]]
</td>
<td>
Realm Record
</td>
<td>
The Realm within which this module was created.
</td>
</tr>
<tr>
<td>
[[Environment]]
</td>
<td>
module Environment Record | ~empty~
</td>
<td>
The Environment Record containing the top level bindings for this module. This field is set when the module is linked.
</td>
</tr>
<tr>
<td>
[[Namespace]]
</td>
<td>
Object | ~empty~
</td>
<td>
The Module Namespace Object (<emu-xref href="#sec-module-namespace-objects"></emu-xref>) if one has been created for this module.
</td>
</tr>
<tr>
<td>
[[HostDefined]]
</td>
<td>
Any, default value is *undefined*.
</td>
<td>
Field reserved for use by host environments that need to associate additional information with a module.
</td>
</tr>
</tbody>
</table>
</emu-table>
<emu-table id="table-37" caption="Abstract Methods of Module Records">
<table>
<tbody>
<tr>
<th>
Method
</th>
<th>
Purpose
</th>
</tr>
<tr>
<td>
GetExportedNames(_exportStarSet_)
</td>
<td>
Return a list of all names that are either directly or indirectly exported from this module.
</td>
</tr>
<tr>
<td>
ResolveExport(_exportName_, _resolveSet_)
</td>
<td>
<p>Return the binding of a name exported by this module. Bindings are represented by a <dfn id="resolvedbinding-record">ResolvedBinding Record</dfn>, of the form { [[Module]]: Module Record, [[BindingName]]: String }. If the export is a Module Namespace Object without a direct binding in any module, [[BindingName]] will be set to *"\*namespace\*"*. Return *null* if the name cannot be resolved, or *"ambiguous"* if multiple bindings were found.</p>
<p>Each time this operation is called with a specific _exportName_, _resolveSet_ pair as arguments it must return the same result if it completes normally.</p>
</td>
</tr>
<tr>
<td>
Link()
</td>
<td>
<p>Prepare the module for evaluation by transitively resolving all module dependencies and creating a module Environment Record.</p>
</td>
</tr>
<tr>
<td>
Evaluate()
</td>
<td>
<p><ins>Returns a promise for the evaluation of this module and its dependencies, resolving on successful evaluation or if it has already been evaluated successfully, and rejecting for an evaluation error or </ins><del>If this module has already been evaluated successfully, return *undefined*; </del>if it has already been evaluated unsuccessfully<del>, throw the exception that was produced. Otherwise, Transitively evaluate all module dependencies of this module and then evaluate this module.</del><ins> If the promise is rejected, hosts are expected to handle the promise rejection and rethrow the evaluation error.</ins></p>
<p>Link must have completed successfully prior to invoking this method.</p>
</td>
</tr>
</tbody>
</table>
</emu-table>
</emu-clause>
<emu-clause id="sec-cyclic-module-records">
<h1>Cyclic Module Records</h1>
<p>A <dfn id="cyclic-module-record">Cyclic Module Record</dfn> is used to represent information about a module that can participate in dependency cycles with other modules that are subclasses of the Cyclic Module Record type. Module Records that are not subclasses of the Cyclic Module Record type must not participate in dependency cycles with Source Text Module Records.</p>
<p>In addition to the fields defined in <emu-xref href="#table-36"></emu-xref> Cyclic Module Records have the additional fields listed in <emu-xref href="#table-cyclic-module-fields"></emu-xref></p>
<emu-table id="table-cyclic-module-fields" caption="Additional Fields of Cyclic Module Records">
<table>
<tbody>
<tr>
<th>
Field Name
</th>
<th>
Value Type
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
[[Status]]
</td>
<td>
~unlinked~ | ~linking~ | ~linked~ | ~evaluating~<ins> | ~evaluating-async~</ins> | ~evaluated~
</td>
<td>
Initially ~unlinked~. Transitions to ~linking~, ~linked~, ~evaluating~, <ins>possibly ~evaluating-async~, </ins>~evaluated~ (in that order) as the module progresses throughout its lifecycle.
<ins>~evaluating-async~ indicates this module is queued to execute on completion of its async dependencies or it is an async module that has been executed and is pending top-level completion.</ins>
</td>
</tr>
<tr>
<td>
[[EvaluationError]]
</td>
<td>
An abrupt completion | *undefined*
</td>
<td>
A completion of type ~throw~ representing the exception that occurred during evaluation. *undefined* if no exception occurred or if [[Status]] is not ~evaluated~.
</td>
</tr>
<tr>
<td>
[[DFSIndex]]
</td>
<td>
Integer | ~empty~
</td>
<td>
Auxiliary field used during Link and Evaluate only.
If [[Status]] is ~linking~ or ~evaluating~, this nonnegative number records the point at which the module was first visited during the depth-first traversal of the dependency graph.
</td>
</tr>
<tr>
<td>
[[DFSAncestorIndex]]
</td>
<td>
Integer | ~empty~
</td>
<td>
Auxiliary field used during Link and Evaluate only.
If [[Status]] is ~linking~ or ~evaluating~, this is either the module's own [[DFSIndex]] or that of an "earlier" module in the same strongly connected component.
</td>
</tr>
<tr>
<td>
[[RequestedModules]]
</td>
<td>
List of String
</td>
<td>
A List of all the |ModuleSpecifier| strings used by the module represented by this record to request the importation of a module. The List is source code occurrence ordered.
</td>
</tr>
<tr>
<td>
<ins>[[CycleRoot]]</ins>
</td>
<td>
<ins>Cyclic Module Record | ~empty~</ins>
</td>
<td>
<ins>The first visited module of the cycle, the root DFS ancestor of the strongly connected component. For a module not in a cycle this would be the module itself.</ins>
</td>
</tr>
<tr>
<td>
<ins>[[Async]]</ins>
</td>
<td>
<ins>Boolean</ins>
</td>
<td>
<ins>Whether this module is individually asynchronous (for example, if it's a Source Text Module Record containing a top-level await). Having an asynchronous dependency does not make the module asynchronous. This field must not change after the module is parsed.</ins>
</td>
</tr>
<tr>
<td>
<ins>[[AsyncEvaluation]]</ins>
</td>
<td>
<ins>Boolean</ins>
</td>
<td>
<ins>Whether this module is either itself async or has an asynchronous dependency.</ins>
<ins>Note: The order in which this field is set is used to order queued executions, see <emu-xref href="#sec-async-module-execution-fulfilled"></emu-xref>.</ins>
</td>
</tr>
<tr>
<td>
<ins>[[TopLevelCapability]]</ins>
</td>
<td>
<ins>Promise Capability | ~empty~</ins>
</td>
<td>
<ins>
If Evaluate() was called on this module, this field contains the Promise capability for that entire evaluation.
It is used to settle the Promise object that is returned from the Evaluate() abstract method.
This field will be ~empty~ for any dependencies of that module, unless a top-level Evaluate() has been initiated for any of those dependencies.
</ins>
</td>
</tr>
<tr>
<td>
<ins>[[AsyncParentModules]]</ins>
</td>
<td>
<ins>List of Cyclic Module Records</ins>
</td>
<td>
<ins>
If this module or a dependency has [[Async]] *true*, and execution is in progress, this tracks the parent importers of this module for the top-level execution job.
These parent modules will not start executing before this module has successfully completed execution.
</ins>
</td>
</tr>
<tr>
<td>
<ins>[[PendingAsyncDependencies]]</ins>
</td>
<td>
<ins>Integer | ~empty~</ins>
</td>
<td>
<ins>
This tracks the number of async dependency modules remaining to execute for this module if it has any asynchronous dependencies.
A module with async dependencies will be executed when this field reaches 0 and there are no execution errors.
</ins>
</td>
</tr>
</tbody>
</table>
</emu-table>
<p>In addition to the methods defined in <emu-xref href="#table-37"></emu-xref> Cyclic Module Records have the additional methods listed in <emu-xref href="#table-cyclic-module-methods"></emu-xref></p>
<emu-table id="table-cyclic-module-methods" caption="Additional Abstract Methods of Cyclic Module Records">
<table>
<tbody>
<tr>
<th>
Method
</th>
<th>
Purpose
</th>
</tr>
<tr>
<td>
InitializeEnvironment()
</td>
<td>
Initialize the Environment Record of the module, including resolving all imported bindings, and create the module's execution context.
</td>
</tr>
<tr>
<td>
ExecuteModule( <ins>[ _promiseCapability_ ]</ins> )
</td>
<td>
Evaluate the module's code within its execution context.
<ins>If this module has *true* in [[Async]], then a Promise Capability is passed as an argument, and the method is expected to resolve or reject the given capability. In this case, the method must not throw an exception, but instead reject the Promise Capability if necessary.</ins>
</td>
</tr>
</tbody>
</table>
</emu-table>
<emu-clause id="sec-moduledeclarationlinking" oldids="sec-moduledeclarationinstantiation">
<h1>Link ( ) Concrete Method</h1>
<p>The Link concrete method of a Cyclic Module Record implements the corresponding Module Record abstract method.</p>
<p>On success, Link transitions this module's [[Status]] from ~unlinked~ to ~linked~. On failure, an exception is thrown and this module's [[Status]] remains ~unlinked~.</p>
<p>This abstract method performs the following steps (most of the work is done by the auxiliary function InnerModuleLinking):</p>
<emu-alg>
1. Let _module_ be this Cyclic Module Record.
1. Assert: _module_.[[Status]] is not ~linking~ or ~evaluating~.
1. Let _stack_ be a new empty List.
1. Let _result_ be InnerModuleLinking(_module_, _stack_, 0).
1. If _result_ is an abrupt completion, then
1. For each Cyclic Module Record _m_ in _stack_, do
1. Assert: _m_.[[Status]] is ~linking~.
1. Set _m_.[[Status]] to ~unlinked~.
1. Assert: _module_.[[Status]] is ~unlinked~.
1. Return _result_.
1. Assert: _module_.[[Status]] is ~linked~<ins>, ~evaluating-async~</ins> or ~evaluated~.
1. Assert: _stack_ is empty.
1. Return *undefined*.
</emu-alg>
<emu-clause id="sec-InnerModuleLinking" oldids="sec-innermoduleinstantiation" aoid="InnerModuleLinking">
<h1>InnerModuleLinking ( _module_, _stack_, _index_ )</h1>
<p>The abstract operation InnerModuleLinking takes arguments _module_ (a Cyclic Module Record), _stack_, and _index_. It is used by Link to perform the actual linking process for _module_, as well as recursively on all other modules in the dependency graph. The _stack_ and _index_ parameters, as well as a module's [[DFSIndex]] and [[DFSAncestorIndex]] fields, keep track of the depth-first search (DFS) traversal. In particular, [[DFSAncestorIndex]] is used to discover strongly connected components (SCCs), such that all modules in an SCC transition to ~linked~ together. It performs the following steps when called:</p>
<emu-alg>
1. If _module_ is not a Cyclic Module Record, then
1. Perform ? _module_.Link().
1. Return _index_.
1. If _module_.[[Status]] is ~linking~, ~linked~<ins>, ~evaluating-async~</ins> or ~evaluated~, then
1. Return _index_.
1. Assert: _module_.[[Status]] is ~unlinked~.
1. Set _module_.[[Status]] to ~linking~.
1. Set _module_.[[DFSIndex]] to _index_.
1. Set _module_.[[DFSAncestorIndex]] to _index_.
1. Set _index_ to _index_ + 1.
1. Append _module_ to _stack_.
1. For each String _required_ that is an element of _module_.[[RequestedModules]], do
1. Let _requiredModule_ be ? HostResolveImportedModule(_module_, _required_).
1. Set _index_ to ? InnerModuleLinking(_requiredModule_, _stack_, _index_).
1. If _requiredModule_ is a Cyclic Module Record, then
1. Assert: _requiredModule_.[[Status]] is either ~linking~, ~linked~<ins>, ~evaluating-async~</ins> or ~evaluated~.
1. Assert: _requiredModule_.[[Status]] is ~linking~ if and only if _requiredModule_ is in _stack_.
1. If _requiredModule_.[[Status]] is ~linking~, then
1. Set _module_.[[DFSAncestorIndex]] to min(_module_.[[DFSAncestorIndex]], _requiredModule_.[[DFSAncestorIndex]]).
1. Perform ? _module_.InitializeEnvironment().
1. Assert: _module_ occurs exactly once in _stack_.
1. Assert: _module_.[[DFSAncestorIndex]] is less than or equal to _module_.[[DFSIndex]].
1. If _module_.[[DFSAncestorIndex]] equals _module_.[[DFSIndex]], then
1. Let _done_ be *false*.
1. Repeat, while _done_ is *false*,
1. Let _requiredModule_ be the last element in _stack_.
1. Remove the last element of _stack_.
1. Assert: _requiredModule_ is a Cyclic Module Record.
1. Set _requiredModule_.[[Status]] to ~linked~.
1. If _requiredModule_ and _module_ are the same Module Record, set _done_ to *true*.
1. Return _index_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-moduleevaluation">
<h1>Evaluate ( ) Concrete Method</h1>
<p>The Evaluate concrete method of a Cyclic Module Record implements the corresponding Module Record abstract method.</p>
<p>Evaluate transitions this module's [[Status]] from ~linked~ to <ins>~evaluating-async~ or</ins>~evaluated~.</p>
<p><del>If execution results in an exception, that exception is recorded in the [[EvaluationError]] field and rethrown by future invocations of Evaluate.</del></p>
<p><ins>The Promise returned by Evaluate is allocated by the first invocation of Evaluate, and its capability is stored in the [[TopLevelCapability]] field. If execution results in an exception, the Promise is rejected. Future invocations of Evaluate return the same Promise.</ins></p>
<p>This abstract method performs the following steps (most of the work is done by the auxiliary function InnerModuleEvaluation):</p>
<emu-alg>
1. Assert: This call to Evaluate is not happening at the same time as another call to Evaluate within the surrounding agent.
1. Let _module_ be this Cyclic Module Record.
1. Assert: _module_.[[Status]] is ~linked~<ins>, ~evaluating-async~</ins> or ~evaluated~.
1. <ins>If _module_.[[Status]] is ~evaluating-async~ or ~evaluated~, set _module_ to _module_.[[CycleRoot]].</ins>
1. <ins>If _module_.[[TopLevelCapability]] is not ~empty~, then</ins>
1. <ins>Return _module_.[[TopLevelCapability]].[[Promise]].</ins>
1. Let _stack_ be a new empty List.
1. <ins>Let _capability_ be ! NewPromiseCapability(%Promise%).</ins>
1. <ins>Set _module_.[[TopLevelCapability]] to _capability_.</ins>
1. Let _result_ be InnerModuleEvaluation(_module_, _stack_, 0).
1. If _result_ is an abrupt completion, then
1. For each Cyclic Module Record _m_ in _stack_, do
1. Assert: _m_.[[Status]] is ~evaluating~.
1. Set _m_.[[Status]] to ~evaluated~.
1. Set _m_.[[EvaluationError]] to _result_.
1. Assert: _module_.[[Status]] is ~evaluated~ and _module_.[[EvaluationError]] is _result_.
1. <del>Return _result_.</del>
1. <ins>Perform ! Call(_capability_.[[Reject]], *undefined*, « _result_.[[Value]] »).</ins>
1. <ins>Otherwise,</ins>
1. Assert: _module_.[[Status]] is <ins>~evaluating-async~ or</ins>~evaluated~ and _module_.[[EvaluationError]] is ~empty~.
1. <ins>If _module_.[[AsyncEvaluation]] is *false*, then</ins>
1. <ins>Assert: _module_.[[Status]] is ~evaluated~.</ins>
1. <ins>Perform ! Call(_capability_.[[Resolve]], *undefined*, « *undefined* »).</ins>
1. Assert: _stack_ is empty.
1. Return <del>*undefined*</del><ins>_capability_.[[Promise]]</ins>.
</emu-alg>
<emu-clause id="sec-innermoduleevaluation" aoid="InnerModuleEvaluation">
<h1>InnerModuleEvaluation ( _module_, _stack_, _index_ )</h1>
<p>The abstract operation InnerModuleEvaluation takes arguments _module_ (a Source Text Module Record), _stack_, and _index_. It is used by Evaluate to perform the actual evaluation process for _module_, as well as recursively on all other modules in the dependency graph. The _stack_ and _index_ parameters, as well as _module_'s [[DFSIndex]] and [[DFSAncestorIndex]] fields, are used the same way as in InnerModuleLinking. It performs the following steps when called:</p>
<emu-alg>
1. If _module_ is not a Cyclic Module Record, then
1. Let _promise_ be ! _module_.Evaluate().
1. Assert: _promise_.[[PromiseState]] is not ~pending~.
1. If _promise_.[[PromiseState]] is ~rejected~, then
1. Return ThrowCompletion(_promise_.[[PromiseStateResult]]).
1. Return _index_.
1. If _module_.[[Status]] is ~evaluated~<ins> or ~evaluating-async~</ins>, then
1. If _module_.[[EvaluationError]] is ~empty~, return _index_.
1. Otherwise, return _module_.[[EvaluationError]].
1. If _module_.[[Status]] is ~evaluating~, return _index_.
1. Assert: _module_.[[Status]] is ~linked~.
1. Set _module_.[[Status]] to ~evaluating~.
1. Set _module_.[[DFSIndex]] to _index_.
1. Set _module_.[[DFSAncestorIndex]] to _index_.
1. <ins>Set _module_.[[PendingAsyncDependencies]] to 0.</ins>
1. Set _index_ to _index_ + 1.
1. Append _module_ to _stack_.
1. For each String _required_ that is an element of _module_.[[RequestedModules]], do
1. Let _requiredModule_ be ! HostResolveImportedModule(_module_, _required_).
1. NOTE: Link must be completed successfully prior to invoking this method, so every requested module is guaranteed to resolve successfully.
1. Set _index_ to ? InnerModuleEvaluation(_requiredModule_, _stack_, _index_).
1. If _requiredModule_ is a Cyclic Module Record, then
1. Assert: _requiredModule_.[[Status]] is either ~evaluating~<ins>, ~evaluating-async~</ins> or ~evaluated~.
1. Assert: _requiredModule_.[[Status]] is ~evaluating~ if and only if _requiredModule_ is in _stack_.
1. If _requiredModule_.[[Status]] is ~evaluating~, then
1. Set _module_.[[DFSAncestorIndex]] to min(_module_.[[DFSAncestorIndex]], _requiredModule_.[[DFSAncestorIndex]]).
1. <ins>Otherwise,</ins>
1. <ins>Set _requiredModule_ to _requiredModule_.[[CycleRoot]].</ins>
1. <ins>Assert: _requiredModule_.[[Status]] is ~evaluating-async~ or ~evaluated~.</ins>
1. <ins>If _requiredModule_.[[EvaluationError]] is not ~empty~, return _requiredModule_.[[EvaluationError]].</ins>
1. <ins>If _requiredModule_.[[AsyncEvaluation]] is *true*, then</ins>
1. <ins>Set _module_.[[PendingAsyncDependencies]] to _module_.[[PendingAsyncDependencies]] + 1.</ins>
1. <ins>Append _module_ to _requiredModule_.[[AsyncParentModules]].</ins>
1. <del>Perform ? _module_.ExecuteModule().</del>
1. <ins>If _module_.[[PendingAsyncDependencies]] > 0 or _module_.[[Async]] is *true*, then</ins>
1. <ins>Assert: _module_.[[AsyncEvaluation]] is *false*.</ins>
1. <ins>Set _module_.[[AsyncEvaluation]] to *true*.</ins>
1. <ins>NOTE: The order in which [[AsyncEvaluation]] transitions to *true* is significant. (See <emu-xref href="#sec-async-module-execution-fulfilled"></emu-xref>)</ins>
1. <ins>If _module_.[[PendingAsyncDependencies]] is 0, perform ! ExecuteAsyncModule(_module_).</ins>
1. <ins>Otherwise, perform ? _module_.ExecuteModule().</ins>
1. Assert: _module_ occurs exactly once in _stack_.
1. Assert: _module_.[[DFSAncestorIndex]] is less than or equal to _module_.[[DFSIndex]].
1. If _module_.[[DFSAncestorIndex]] equals _module_.[[DFSIndex]], then
1. Let _done_ be *false*.
1. Repeat, while _done_ is *false*,
1. Let _requiredModule_ be the last element in _stack_.
1. Remove the last element of _stack_.
1. Assert: _requiredModule_ is a Cyclic Module Record.
1. <ins>If _requiredModule_.[[AsyncEvaluation]] is *false*, set _requiredModule_.[[Status]] to ~evaluated~.</ins>
1. <ins>Otherwise, set _requiredModule_.[[Status]] to ~evaluating-async~.</ins>
1. If _requiredModule_ and _module_ are the same Module Record, set _done_ to *true*.
1. <ins>Set _requiredModule_.[[CycleRoot]] to _module_.</ins>
1. Return _index_.
</emu-alg>
<emu-note>
<p><ins>A module is ~evaluating~ while it is being traversed by InnerModuleEvaluation. A module is ~evaluated~ on execution completion or ~evaluating-async~ during execution if it is an asynchronous module.</ins></p>
</emu-note>
<emu-note>
<p><ins>Any modules depending on a module of an async cycle when that cycle is not ~evaluating~ will instead depend on the execution of the root of the cycle via [[CycleRoot]]. This ensures that the cycle state can be treated as a single strongly connected component through its root module state.</ins></p>
</emu-note>
</emu-clause>
<emu-clause id="sec-execute-async-module" aoid="ExecuteAsyncModule">
<h1><ins>ExecuteAsyncModule ( _module_ )</ins></h1>
<emu-alg>
1. Assert: _module_.[[Status]] is ~evaluating~ or ~evaluating-async~.
1. Assert: _module_.[[Async]] is *true*.
1. Let _capability_ be ! NewPromiseCapability(%Promise%).
1. Let _stepsFulfilled_ be the steps of a CallAsyncModuleFulfilled function as specified below.
1. Let _onFulfilled_ be CreateBuiltinFunction(_stepsFulfilled_, « [[Module]] »).
1. Set _onFulfilled_.[[Module]] to _module_.
1. Let _stepsRejected_ be the steps of a CallAsyncModuleRejected function as specified below.
1. Let _onRejected_ be CreateBuiltinFunction(_stepsRejected_, « [[Module]] »).
1. Set _onRejected_.[[Module]] to _module_.
1. Perform ! PerformPromiseThen(_capability_.[[Promise]], _onFulfilled_, _onRejected_).
1. Perform ! _module_.ExecuteModule(_capability_).
1. Return.
</emu-alg>
<p>A CallAsyncModuleFulfilled function is an anonymous built-in function with a [[Module]] internal slot. When a CallAsyncModuleFulfilled function is called that expects no arguments it performs the following steps:</p>
<emu-alg>
1. Let _f_ be the active function object.
1. Let _module_ be _f_.[[Module]].
1. Perform ! AsyncModuleExecutionFulfilled(_module_).
1. Return.
</emu-alg>
<p>A CallAsyncModuleRejected function is an anonymous built-in function with a [[Module]] internal slot. When a CallAsyncModuleRejected function is called with argument _error_ it performs the following steps:</p>
<emu-alg>
1. Let _f_ be the active function object.
1. Let _module_ be _f_.[[Module]].
1. Perform ! AsyncModuleExecutionRejected(_module_, _error_).
1. Return.
</emu-alg>
</emu-clause>
<emu-clause id="sec-gather-async-parent-completions" aoid="GatherAsyncParentCompletions">
<h1><ins>GatherAsyncParentCompletions ( _module_, _execList_ )</ins></h1>
<emu-alg>
1. For each Module _m_ of _module_.[[AsyncParentModules]], do
1. If _execList_ does not contain _m_ and _m_.[[CycleRoot]].[[EvaluationError]] is ~empty~, then
1. Assert: _m_.[[Status]] is ~evaluating-async~.
1. Assert: _m_.[[EvaluationError]] is ~empty~.
1. Assert: _m_.[[AsyncEvaluation]] is *true*.
1. Assert: _m_.[[PendingAsyncDependencies]] > 0.
1. Set _m_.[[PendingAsyncDependencies]] to _m_.[[PendingAsyncDependencies]] - 1.
1. If _m_.[[PendingAsyncDependencies]] is equal to 0, then
1. Append _m_ to _execList_.
1. If _m_.[[Async]] is *false*, perform ! GatherAsyncParentCompletions(_m_, _execList_).
1. Return *undefined*.
</emu-alg>
<emu-note>
<p>When an async execution for a root _module_ is fulfilled, this function determines the list of modules which are able to synchronously execute together on this completion, populating them in _execList_.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-async-module-execution-fulfilled" aoid="AsyncModuleExecutionFulfilled">
<h1><ins>AsyncModuleExecutionFulfilled ( _module_ )</ins></h1>
<emu-alg>
1. If _module_.[[Status]] is ~evaluated~,
1. Assert: _module_.[[EvaluationError]] is not ~empty~.
1. Return *undefined*.
1. Assert: _module_.[[Status]] is ~evaluating-async~.
1. Assert: _module_.[[AsyncEvaluation]] is *true*.
1. Assert: _module_.[[EvaluationError]] is ~empty~.
1. Set _module_.[[Status]] to ~evaluated~.
1. If _module_.[[TopLevelCapability]] is not ~empty~, then
1. Assert: _module_.[[CycleRoot]] is equal to _module_.
1. Perform ! Call(_module_.[[TopLevelCapability]].[[Resolve]], *undefined*, «*undefined*»).
1. Let _execList_ be a new empty List.
1. Perform ! GatherAsyncParentCompletions(_module_, _execList_).
1. Let _sortedExecList_ be a List of elements that are the elements of _execList_, in the order in which they had their [[AsyncEvaluation]] fields set to *true* in InnerModuleEvaluation.
1. Assert: All elements of _sortedExecList_ have their [[AsyncEvaluation]] field set to *true*, [[PendingAsyncDependencies]] field set to 0 and [[EvaluationError]] field set to *undefined*.
1. For each Module _m_ of _sortedExecList_, do
1. If _m_.[[Status]] is ~evaluated~, then
1. Assert: _m_.[[EvaluationError]] is not ~empty~.
1. Otherwise, if _m_.[[Async]] is *true*, then
1. Perform ! ExecuteAsyncModule(_m_).
1. Otherwise,
1. Let _result_ be _m_.ExecuteModule().
1. If _result_ is an abrupt completion,
1. Perform ! AsyncModuleExecutionRejected(_m_, _result_.[[Value]]).
1. Otherwise,
1. Set _m_.[[Status]] to ~evaluated~.
1. If _m_.[[TopLevelCapability]] is not ~empty~, then
1. Assert: _m_.[[CycleRoot]] is equal to _m_.
1. Perform ! Call(_m_.[[TopLevelCapability]].[[Resolve]], *undefined*, «*undefined*»).
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-async-module-execution-rejected" aoid="AsyncModuleExecutionRejected">
<h1><ins>AsyncModuleExecutionRejected ( _module_, _error_ )</ins></h1>
<emu-alg>
1. If _module_.[[Status]] is ~evaluated~,
1. Assert: _module_.[[EvaluationError]] is not ~empty~.
1. Return *undefined*.
1. Assert: _module_.[[Status]] is ~evaluating-async~.
1. Assert: _module_.[[AsyncEvaluation]] is *true*.
1. Assert: _module_.[[EvaluationError]] is ~empty~.
1. Set _module_.[[EvaluationError]] to ThrowCompletion(_error_).
1. Set _module_.[[Status]] to ~evaluated~.
1. For each Module _m_ of _module_.[[AsyncParentModules]], do
1. Perform ! AsyncModuleExecutionRejected(_m_, _error_).
1. If _module_.[[TopLevelCapability]] is not ~empty~, then
1. Assert: _module_.[[CycleRoot]] is equal to _module_.
1. Perform ! Call(_module_.[[TopLevelCapability]].[[Reject]], *undefined*, «_error_»).
1. Return *undefined*.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-example-cyclic-module-record-graphs">
<h1>Example Cyclic Module Record Graphs</h1>
<p>This non-normative section gives a series of examples of the linking and evaluation of a few common module graphs, with a specific focus on how errors can occur.</p>
<p>First consider the following simple module graph:</p>
<emu-figure id="figure-module-graph-simple" caption="A simple module graph">
<img alt="A module graph in which module A depends on module B, and module B depends on module C" width="121" height="211" src="img/module-graph-simple.svg">
</emu-figure>
<p>Let's first assume that there are no error conditions. When a host first calls _A_.Link(), this will complete successfully by assumption, and recursively link modules _B_ and _C_ as well, such that _A_.[[Status]] = _B_.[[Status]] = _C_.[[Status]] = ~linked~. This preparatory step can be performed at any time. Later, when the host is ready to incur any possible side effects of the modules, it can call _A_.Evaluate(), which will complete successfully<ins>, returning a Promise resolving to undefined</ins> (again by assumption), recursively having evaluated first _C_ and then _B_. Each module's [[Status]] at this point will be ~evaluated~.</p>
<p>Consider then cases involving linking errors. If InnerModuleLinking of _C_ succeeds but, thereafter, fails for _B_, for example because it imports something that _C_ does not provide, then the original _A_.Link() will fail, and both _A_ and _B_'s [[Status]] remain ~unlinked~. _C_'s [[Status]] has become ~linked~, though.</p>
<p>Finally, consider a case involving evaluation errors. If InnerModuleEvaluation of _C_ succeeds but, thereafter, fails for _B_, for example because _B_ contains code that throws an exception, then the original _A_.Evaluate() will fail<ins>, returning a rejected Promise</ins>. The resulting exception will be recorded in both _A_ and _B_'s [[EvaluationError]] fields, and their [[Status]] will become ~evaluated~. _C_ will also become ~evaluated~ but, in contrast to _A_ and _B_, will remain without an [[EvaluationError]], as it successfully completed evaluation. Storing the exception ensures that any time a host tries to reuse _A_ or _B_ by calling their Evaluate() method, it will encounter the same exception. (Hosts are not required to reuse Cyclic Module Records; similarly, hosts are not required to expose the exception objects thrown by these methods. However, the specification enables such uses.)</p>
<p>The difference here between linking and evaluation errors is due to how evaluation must be only performed once, as it can cause side effects; it is thus important to remember whether evaluation has already been performed, even if unsuccessfully. (In the error case, it makes sense to also remember the exception because otherwise subsequent Evaluate() calls would have to synthesize a new one.) Linking, on the other hand, is side-effect-free, and thus even if it fails, it can be retried at a later time with no issues.</p>
<p>Now consider a different type of error condition:</p>
<emu-figure id="figure-module-graph-missing" caption="A module graph with an unresolvable module">
<img alt="A module graph in which module A depends on a missing (unresolvable) module, represented by ???" width="121" height="121" src="img/module-graph-missing.svg">
</emu-figure>
<p>In this scenario, module _A_ declares a dependency on some other module, but no Module Record exists for that module, i.e. HostResolveImportedModule throws an exception when asked for it. This could occur for a variety of reasons, such as the corresponding resource not existing, or the resource existing but ParseModule throwing an exception when trying to parse the resulting source text. Hosts can choose to expose the cause of failure via the exception they throw from HostResolveImportedModule. In any case, this exception causes a linking failure, which as before results in _A_'s [[Status]] remaining ~unlinked~.</p>
<p><del>Lastly</del><ins>Now</ins>, consider a module graph with a cycle:</p>
<emu-figure id="figure-module-graph-cycle" caption="A cyclic module graph">
<img alt="A module graph in which module A depends on module B and C, but module B also depends on module A" width="181" height="121" src="img/module-graph-cycle.svg">
</emu-figure>
<p>Here we assume that the entry point is module _A_, so that the host proceeds by calling _A_.Link(), which performs InnerModuleLinking on _A_. This in turn calls InnerModuleLinking on _B_. Because of the cycle, this again triggers InnerModuleLinking on _A_, but at this point it is a no-op since _A_.[[Status]] is already ~linking~. _B_.[[Status]] itself remains ~linking~ when control gets back to _A_ and InnerModuleLinking is triggered on _C_. After this returns with _C_.[[Status]] being ~linked~ , both _A_ and _B_ transition from ~linking~ to ~linked~ together; this is by design, since they form a strongly connected component.</p>
<p>An analogous story occurs for the evaluation phase of a cyclic module graph, in the success case.</p>
<p>Now consider a case where _A_ has an linking error; for example, it tries to import a binding from _C_ that does not exist. In that case, the above steps still occur, including the early return from the second call to InnerModuleLinking on _A_. However, once we unwind back to the original InnerModuleLinking on _A_, it fails during InitializeEnvironment, namely right after _C_.ResolveExport(). The thrown *SyntaxError* exception propagates up to _A_.Link, which resets all modules that are currently on its _stack_ (these are always exactly the modules that are still ~linking~). Hence both _A_ and _B_ become ~unlinked~. Note that _C_ is left as ~linked~.</p>
<p>Finally, consider a case where _A_ has an evaluation error; for example, its source code throws an exception. In that case, the evaluation-time analog of the above steps still occurs, including the early return from the second call to InnerModuleEvaluation on _A_. However, once we unwind back to the original InnerModuleEvaluation on _A_, it fails by assumption. The exception thrown propagates up to _A_.Evaluate(), which records the error in all modules that are currently on its _stack_ (i.e., the modules that are still ~evaluating~)<ins> as well as via [[AsyncParentModules]], which form a chain for modules which contain or depend on top-level `await` through the whole dependency graph through the AsyncModuleExecutionRejected algorithm</ins>. Hence both _A_ and _B_ become ~evaluated~ and the exception is recorded in both _A_ and _B_'s [[EvaluationError]] fields, while _C_ is left as ~evaluated~ with no [[EvaluationError]].</p>
<p><ins>Lastly, consider a module graph with a cycle, where all modules complete asynchronously:</ins></p>
<ins><emu-figure id="figure-module-graph-cycle-async" caption="An asynchronous cyclic module graph">
<img alt="A module graph in which module A depends on module B and C, module B depends on module D, module C depends on module D and E, and module D depends on module A" width="241" height="211" src="img/module-graph-cycle-async.svg">
</emu-figure></ins>
<p><ins>Linking happens as before, and all modules end up with [[Status]] set to ~linked~.</ins></p>
<p><ins>
Calling _A_.Evaluate() triggers InnerModuleEvaluation on _A_, _B_, and _D_, which all transition to ~evaluating~.
Then InnerModuleEvaluation is called on _A_ again, which is a no-op because it is already ~evaluating~.
At this point, _D_.[[PendingAsyncDependencies]] is 0, so ExecuteAsyncModule(_D_) is called and triggers the execution promise for _D_.
We unwind back to the InnerModuleEvaluation on _B_, setting _B_.[[PendingAsyncDependencies]] to 1 and _B_.[[AsyncEvaluation]] to *true*.
We unwind back to the original InnerModuleEvaluation on _A_, setting _A_.[[PendingAsyncDependencies]] to 1.
In the next iteration of the loop over _A_'s dependencies, we call InnerModuleEvaluation on _C_ and thus on _D_ (again a no-op) and _E_.
As _E_ has no dependencies, ExecuteAsyncModule(_E_) is called, which triggers its execution promise.
Because _E_ is not part of a cycle, it is immediately removed from the stack and transitions to ~evaluated~.
We unwind once more to the original InnerModuleEvaluation on _A_, setting _C_.[[AsyncEvaluation]] to *true*.
Now we finish the loop over _A_'s dependencies, set _A_.[[AsyncEvaluation]] to *true*, and remove the entire strongly connected component from the stack, transitioning all of the modules to ~evaluating-async~ at once.
At this point, the fields of the modules are as given in <emu-xref href="#table-module-graph-cycle-async-fields-1"></emu-xref>.
</ins></p>
<emu-table id="table-module-graph-cycle-async-fields-1" caption="Module fields after the initial Evaluate() call">
<table>
<thead>
<tr>
<th>Module</th>
<th>[[DFSIndex]]</th>
<th>[[DFSAncestorIndex]]</th>
<th>[[Status]]</th>
<th>[[AsyncEvaluation]]</th>
<th>[[AsyncParentModules]]</th>
<th>[[PendingAsyncDependencies]]</th>
</tr>
</thead>
<tbody>
<tr>
<th>_A_</th>
<td>0</td>
<td>0</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« »</td>
<td>2 (_B_ and _C_)</td>
</tr>
<tr>
<th>_B_</th>
<td>1</td>
<td>0</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« _A_ »</td>
<td>1 (_D_)</td>
</tr>
<tr>
<th>_C_</th>
<td>2</td>
<td>0</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« _A_ »</td>
<td>2 (_D_ and _E_)</td>
</tr>
<tr>
<th>_D_</th>
<td>3</td>
<td>0</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« _B_, _C_ »</td>
<td>0</td>
</tr>
<tr>
<th>_E_</th>
<td>4</td>
<td>4</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« _C_ »</td>
<td>0</td>
</tr>
</tbody>
</table>
</emu-table>
<p><ins>
Let us assume that _E_ finishes executing first.
When that happens, AsyncModuleExecutionFulfilled is called, _E_.[[Status]] is set to ~evaluated~ and _C_.[[PendingAsyncDependencies]] is decremented to become 1.
The fields of the updated modules are as given in <emu-xref href="#table-module-graph-cycle-async-fields-2"></emu-xref>.
</ins></p>
<emu-table id="table-module-graph-cycle-async-fields-2" caption="Module fields after module _E_ finishes executing">
<table>
<thead>
<tr>
<th>Module</th>
<th>[[DFSIndex]]</th>
<th>[[DFSAncestorIndex]]</th>
<th>[[Status]]</th>
<th>[[AsyncEvaluation]]</th>
<th>[[AsyncParentModules]]</th>
<th>[[PendingAsyncDependencies]]</th>
</tr>
</thead>
<tbody>
<tr>
<th>_C_</th>
<td>2</td>
<td>0</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« _A_ »</td>
<td>1 (_D_)</td>
</tr>
<tr>
<th>_E_</th>
<td>4</td>
<td>4</td>
<td>~evaluated~</td>
<td>*true*</td>
<td>« _C_ »</td>
<td>0</td>
</tr>
</tbody>
</table>
</emu-table>
<p><ins>
_D_ is next to finish (as it was the only module that was still executing).
When that happens, AsyncModuleExecutionFulfilled is called again and _D_.[[Status]] is set to ~evaluated~.
Then _B_.[[PendingAsyncDependencies]] is decremented to become 0, ExecuteAsyncModule is called on _B_, and its execution is triggered.
_C_.[[PendingAsyncDependencies]] is also decremented to become 0, and _C_'s execution is triggered.
The fields of the updated modules are as given in <emu-xref href="#table-module-graph-cycle-async-fields-3"></emu-xref>.
</ins></p>
<emu-table id="table-module-graph-cycle-async-fields-3" caption="Module fields after module _D_ finishes executing">
<table>
<thead>
<tr>
<th>Module</th>
<th>[[DFSIndex]]</th>
<th>[[DFSAncestorIndex]]</th>
<th>[[Status]]</th>
<th>[[AsyncEvaluation]]</th>
<th>[[AsyncParentModules]]</th>
<th>[[PendingAsyncDependencies]]</th>
</tr>
</thead>
<tbody>
<tr>
<th>_B_</th>
<td>1</td>
<td>0</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« _A_ »</td>
<td>0</td>
</tr>
<tr>
<th>_C_</th>
<td>2</td>
<td>0</td>
<td>~evaluating-async~</td>
<td>*true*</td>
<td>« _A_ »</td>
<td>0</td>
</tr>
<tr>
<th>_D_</th>
<td>3</td>
<td>0</td>
<td>~evaluated~</td>
<td>*true*</td>
<td>« _B_, _C_ »</td>
<td>0</td>
</tr>
</tbody>
</table>
</emu-table>
<p><ins>
Let us assume that _C_ finishes executing next.
When that happens, AsyncModuleExecutionFulfilled is called again, _C_.[[Status]] is set to ~evaluated~ and _A_.[[PendingAsyncDependencies]] is decremented to become 1.
The fields of the updated modules are as given in <emu-xref href="#table-module-graph-cycle-async-fields-4"></emu-xref>.
</ins></p>
<emu-table id="table-module-graph-cycle-async-fields-4" caption="Module fields after module _C_ finishes executing">
<table>
<thead>
<tr>
<th>Module</th>
<th>[[DFSIndex]]</th>
<th>[[DFSAncestorIndex]]</th>
<th>[[Status]]</th>
<th>[[AsyncEvaluation]]</th>
<th>[[AsyncParentModules]]</th>
<th>[[PendingAsyncDependencies]]</th>
</tr>
</thead>
<tbody>
<tr>
<th>_A_</th>
<td>0</td>
<td>0</td>
<th>~evaluating-async~</th>
<td>*true*</td>
<td>« »</td>
<td>1 (_B_)</td>
</tr>
<tr>
<th>_C_</th>
<td>2</td>
<td>0</td>
<th>~evaluated~</th>
<td>*true*</td>
<td>« _A_ »</td>
<td>0</td>
</tr>
</tbody>
</table>
</emu-table>
<p><ins>
Then, _B_ finishes executing.
When that happens, AsyncModuleExecutionFulfilled is called again and _B_.[[Status]] is set to ~evaluated~.
_A_.[[PendingAsyncDependencies]] is decremented to become 0, so ExecuteAsyncModule is called and _A_'s execution is triggered.
The fields of the updated modules are as given in <emu-xref href="#table-module-graph-cycle-async-fields-5"></emu-xref>.
</ins></p>
<emu-table id="table-module-graph-cycle-async-fields-5" caption="Module fields after module _B_ finishes executing">
<table>