-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspec.emu
More file actions
892 lines (844 loc) · 46.5 KB
/
spec.emu
File metadata and controls
892 lines (844 loc) · 46.5 KB
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
<!doctype html>
<meta charset="utf8">
<link rel="stylesheet" href="./spec.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css">
<script src="./spec.js"></script>
<pre class="metadata">
title: Import Sync
stage: 2
contributors: Guy Bedford
</pre>
<emu-note type="editor">
<p style="margin-left:1em">NOTE: The diff markers are on top of <a href="https://tc39.es/proposal-defer-import-eval/">https://tc39.es/proposal-defer-import-eval/</a>.</p>
</emu-note>
<emu-clause id="sec-ecmascript-language-expressions" number="13">
<h1>ECMAScript Language: Expressions</h1>
<emu-clause id="sec-left-hand-side-expressions" number="3">
<h1>Left-Hand-Side Expressions</h1>
<h2>Syntax</h2>
<emu-grammar type="definition">
ImportCall[Yield, Await] :
`import` ImportCallArguments[?Yield, ?Await]
`import` `.` `defer` ImportCallArguments[?Yield, ?Await]
<ins>`import` `.` `sync` ImportCallArguments[?Yield, ?Await]</ins>
ImportCallArguments[Yield, Await] :
`(` AssignmentExpression[+In, ?Yield, ?Await] `,`? `)`
`(` AssignmentExpression[+In, ?Yield, ?Await] `,` AssignmentExpression[+In, ?Yield, ?Await] `,`? `)`
</emu-grammar>
<emu-clause id="sec-import-calls" number="10">
<h1>Import Calls</h1>
<emu-note type="editor">
The changes to this section are intended to mirror the changes proposed by the
<a href="https://tc39.es/proposal-source-phase-imports/#sec-import-calls">Source
Phase Imports</a> proposal, but adapted to the ~defer~ phase.
</emu-note>
<emu-clause id="sec-import-call-runtime-semantics-evaluation" type="sdo">
<h1>Runtime Semantics: Evaluation</h1>
<emu-grammar>ImportCall : `import` ImportCallArguments</emu-grammar>
<emu-alg>
1. Return ? EvaluateImportCall(|ImportCallArguments|, ~evaluation~).
</emu-alg>
<emu-grammar>ImportCall : `import` `.` `defer` ImportCallArguments</emu-grammar>
<emu-alg>
1. Return ? EvaluateImportCall(|ImportCallArguments|, ~defer~).
</emu-alg>
<emu-grammar><ins>ImportCall : `import` `.` `sync` ImportCallArguments</ins></emu-grammar>
<emu-alg>
1. <ins>Return ? ImportCallSync(|ImportCallArguments|).</ins>
</emu-alg>
</emu-clause>
<emu-clause id="sec-import-call-sync" type="abstract operation">
<h1>
<ins>ImportCallSync (
_arguments_: a Parse Node
): either a normal completion or an abrupt completion</ins>
</h1>
<dl class="header"></dl>
<emu-alg>
1. Let _referrer_ be GetActiveScriptOrModule().
1. If _referrer_ is *null*, set _referrer_ to the current Realm Record.
1. Let _specifier_ be ? EvaluateImportCallSpecifier of _arguments_.
1. Let _options_ be ? EvaluateImportCallOptions of _arguments_.
1. Let _specifierString_ be ? ToString(_specifier_).
1. Let _attributes_ be a new empty List.
1. If _options_ is not *undefined*, then
1. If _options_ is not an Object, then
1. Throw a *TypeError* exception.
1. Let _attributesObj_ be ? Get(_options_, *"with"*).
1. If _attributesObj_ is not *undefined*, then
1. If _attributesObj_ is not an Object, then
1. Throw a *TypeError* exception.
1. Let _entries_ be ? EnumerableOwnProperties(_attributesObj_, ~key+value~).
1. For each element _entry_ of _entries_, do
1. Let _key_ be ! Get(_entry_, *"0"*).
1. Let _value_ be ! Get(_entry_, *"1"*).
1. If _key_ is a String, then
1. If _value_ is not a String, then
1. Throw a *TypeError* exception.
1. Append the ImportAttribute Record { [[Key]]: _key_, [[Value]]: _value_ } to _attributes_.
1. If AllImportAttributesSupported(_attributes_) is *false*, then
1. Throw a *TypeError* exception.
1. Sort _attributes_ according to the lexicographic order of their [[Key]] field, treating the value of each such field as a sequence of UTF-16 code unit values. NOTE: This sorting is observable only in that hosts are prohibited from changing behaviour based on the order in which attributes are enumerated.
1. Let _moduleRequest_ be a new ModuleRequest Record { [[Specifier]]: _specifierString_, [[Attributes]]: _attributes_, [[Phase]]: ~evaluation~ }.
1. Let _moduleCompletion_ be NormalCompletion(~empty~).
1. Perform HostLoadImportedModule(_referrer_, _moduleRequest_, ~empty~, _moduleCompletion_).
1. NOTE: HostLoadImportedModule will update _moduleCompletion_ synchronously either updating its value to be a Module Record, or setting it to be a throw completion.
1. Let _module_ be ? _moduleCompletion_.
1. Perform ? _module_.LoadRequestedModulesSync().
1. Perform ? _module_.Link().
1. If ReadyForSyncExecution(_module_) is *false*, throw a *TypeError* exception.
1. Perform ? EvaluateModuleSync(_module_).
1. Return GetModuleNamespace(_module_, ~evaluation~).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-ecmascript-language-scripts-and-modules" number="16">
<h1>ECMAScript Language: Scripts and Modules</h1>
<emu-clause id="sec-modules" number="2">
<h1>Modules</h1>
<h2>Syntax</h2>
<emu-grammar type="definition">
Module :
ModuleBody?
ModuleBody :
ModuleItemList
ModuleItemList :
ModuleItem
ModuleItemList ModuleItem
ModuleItem :
ImportDeclaration
ExportDeclaration
StatementListItem[~Yield, +Await, ~Return]
ModuleExportName :
IdentifierName
StringLiteral
</emu-grammar>
<emu-clause id="sec-module-semantics">
<h1>Module Semantics</h1>
<emu-clause id="sec-abstract-module-records" number="5">
<h1>Abstract Module Records</h1>
<p>A <dfn variants="Module Records">Module Record</dfn> encapsulates structural information about the imports and exports of a single module. This information is used to link the imports and exports of sets of connected modules. A Module Record includes four fields that are only used when evaluating a module.</p>
<p>For specification purposes Module Record values are values of the Record specification type and can be thought of as existing in a simple object-oriented hierarchy where Module Record is an abstract class with both abstract and concrete subclasses. This specification defines the abstract subclass named Cyclic Module Record and its concrete subclass named Source Text Module Record. Other specifications and implementations may define additional Module Record subclasses corresponding to alternative module definition facilities that they defined.</p>
<p>Module Record defines the fields listed in <emu-xref href="#table-module-record-fields"></emu-xref>. All Module Definition subclasses include at least those fields. Module Record also defines the abstract method list in <emu-xref href="#table-abstract-methods-of-module-records"></emu-xref>. All Module definition subclasses must provide concrete implementations of these abstract methods.</p>
<emu-table id="table-module-record-fields" caption="Module Record Fields" oldids="table-36">
<table>
<thead>
<tr>
<th>
Field Name
</th>
<th>
Value Type
</th>
<th>
Meaning
</th>
</tr>
</thead>
<tr>
<td>
[[Realm]]
</td>
<td>
a Realm Record
</td>
<td>
The Realm within which this module was created.
</td>
</tr>
<tr>
<td>
[[Environment]]
</td>
<td>
a Module Environment Record or ~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>
an Object or ~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>
anything (default value is *undefined*)
</td>
<td>
Field reserved for use by host environments that need to associate additional information with a module.
</td>
</tr>
</table>
</emu-table>
<emu-table id="table-abstract-methods-of-module-records" caption="Abstract Methods of Module Records" oldids="table-37">
<table>
<thead>
<tr>
<th>
Method
</th>
<th>
Purpose
</th>
</tr>
</thead>
<tr>
<td>
LoadRequestedModules([_hostDefined_])
</td>
<td>
<p>Prepares the module for linking by recursively loading all its dependencies, and returns a promise.</p>
</td>
</tr>
<tr>
<td>
<ins>LoadRequestedModulesSync([_hostDefined_])</ins>
</td>
<td>
<p><ins>Prepares the module for linking by recursively loading all its dependencies synchronously.</ins></p>
</td>
</tr>
<tr>
<td>
GetExportedNames([_exportStarSet_])
</td>
<td>
<p>Return a list of all names that are either directly or indirectly exported from this module.</p>
<p>LoadRequestedModules must have completed successfully prior to invoking this method.</p>
</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" variants="ResolvedBinding Records">ResolvedBinding Record</dfn>, of the form { [[Module]]: Module Record, [[BindingName]]: String | ~namespace~ }. 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.</p>
<p>LoadRequestedModules must have completed successfully prior to invoking this method.</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>
<p>LoadRequestedModules must have completed successfully prior to invoking this method.</p>
</td>
</tr>
<tr>
<td>
Evaluate()
</td>
<td>
<p>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 if it has already been evaluated unsuccessfully. If the promise is rejected, hosts are expected to handle the promise rejection and rethrow the evaluation error.</p>
<p>Link must have completed successfully prior to invoking this method.</p>
</td>
</tr>
</table>
</emu-table>
<emu-clause id="sec-EvaluateModuleSync" type="abstract operation">
<h1>
EvaluateModuleSync (
_module_: a Module Record,
): either a normal completion containing ~unused~ or a throw completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It synchronously evaluates _module_, provided that the caller guarantees that _module_'s evaluation will return an already settled promise.</dd>
</dl>
<emu-alg>
1. Assert: _module_ is not a Cyclic Module Record.
1. Let _promise_ be _module_.Evaluate().
1. Assert: _promise_.[[PromiseState]] is either ~fulfilled~ or ~rejected~.
1. If _promise_.[[PromiseState]] is ~rejected~, then
1. If _promise_.[[PromiseIsHandled]] is *false*, perform HostPromiseRejectionTracker(_promise_, *"handle"*).
1. Set _promise_.[[PromiseIsHandled]] to *true*.
1. Return ThrowCompletion(_promise_.[[PromiseResult]]).
1. Return ~unused~.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-cyclic-module-records" number="6">
<h1>Cyclic Module Records</h1>
<p>A <dfn id="cyclic-module-record" variants="Cyclic Module Records">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-module-record-fields"></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>
<thead>
<tr>
<th>
Field Name
</th>
<th>
Value Type
</th>
<th>
Meaning
</th>
</tr>
</thead>
<tr>
<td>
[[Status]]
</td>
<td>
~new~, ~unlinked~, ~linking~, ~linked~, ~evaluating~, ~evaluating-async~, or ~evaluated~
</td>
<td>
Initially ~new~. Transitions to ~unlinked~, ~linking~, ~linked~, ~evaluating~, possibly ~evaluating-async~, ~evaluated~ (in that order) as the module progresses throughout its lifecycle. ~evaluating-async~ indicates this module is queued to execute on completion of its asynchronous dependencies or it is a module whose [[HasTLA]] field is *true* that has been executed and is pending top-level completion.
</td>
</tr>
<tr>
<td>
[[EvaluationError]]
</td>
<td>
a throw completion or ~empty~
</td>
<td>
A throw completion representing the exception that occurred during evaluation. *undefined* if no exception occurred or if [[Status]] is not ~evaluated~.
</td>
</tr>
<tr>
<td>
[[DFSAncestorIndex]]
</td>
<td>
an integer or ~empty~
</td>
<td>
Auxiliary field used during Link and Evaluate only. If [[Status]] is either ~linking~ or ~evaluating~, this is either the module's depth-first traversal index or that of an "earlier" module in the same strongly connected component.
</td>
</tr>
<tr>
<td>
[[RequestedModules]]
</td>
<td>
a List of ModuleRequest Records
</td>
<td>
A List of the ModuleRequest Records associated with the imports in this module. The List is in source text occurrence order of the imports.
</td>
</tr>
<tr>
<td>
[[LoadedModules]]
</td>
<td>
a List of LoadedModuleRequest Records
</td>
<td>
A map from the specifier strings used by the module represented by this record to request the importation of a module with the relative import attributes to the resolved Module Record. The list does not contain two different Records _r1_ and _r2_ such that ModuleRequestsEqual(_r1_, _r2_) is *true*.
</td>
</tr>
<tr>
<td>
[[CycleRoot]]
</td>
<td>
a Cyclic Module Record or ~empty~
</td>
<td>
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. Once Evaluate has completed, a module's [[DFSAncestorIndex]] is the depth-first traversal index of its [[CycleRoot]].
</td>
</tr>
<tr>
<td>
[[HasTLA]]
</td>
<td>
a Boolean
</td>
<td>
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 mean this field is *true*. This field must not change after the module is parsed.
</td>
</tr>
<tr>
<td>
[[AsyncEvaluationOrder]]
</td>
<td>
~unset~, an integer, or ~done~
</td>
<td>
This field is initially set to ~unset~, and remains ~unset~ for fully synchronous modules. For modules that are either themselves asynchronous or have an asynchronous dependency, it is set to an integer that determines the order in which execution of pending modules is queued by <emu-xref href="#sec-async-module-execution-fulfilled"></emu-xref>. Once the pending module is executed, the field is set to ~done~.
</td>
</tr>
<tr>
<td>
[[TopLevelCapability]]
</td>
<td>
a PromiseCapability Record or ~empty~
</td>
<td>
If this module is the [[CycleRoot]] of some cycle, and Evaluate() was called on some module in that cycle, this field contains the PromiseCapability Record 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 some of those dependencies.
</td>
</tr>
<tr>
<td>
[[AsyncParentModules]]
</td>
<td>
a List of Cyclic Module Records
</td>
<td>
If this module or a dependency has [[HasTLA]] *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.
</td>
</tr>
<tr>
<td>
[[PendingAsyncDependencies]]
</td>
<td>
an integer or ~empty~
</td>
<td>
If this module has any asynchronous dependencies, this tracks the number of asynchronous dependency modules remaining to execute for this module. A module with asynchronous dependencies will be executed when this field reaches 0 and there are no execution errors.
</td>
</tr>
</table>
</emu-table>
<p>In addition to the methods defined in <emu-xref href="#table-abstract-methods-of-module-records"></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>
<thead>
<tr>
<th>
Method
</th>
<th>
Purpose
</th>
</tr>
</thead>
<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( [ _promiseCapability_ ] )
</td>
<td>
Evaluate the module's code within its execution context. If this module has *true* in [[HasTLA]], then a PromiseCapability Record 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 PromiseCapability Record if necessary.
</td>
</tr>
</table>
</emu-table>
<p>A <dfn id="graphloadingstate-record" variants="GraphLoadingState Records">GraphLoadingState Record</dfn> is a Record that contains information about the loading process of a module graph. It's used to continue loading after a call to HostLoadImportedModule. Each GraphLoadingState Record has the fields defined in <emu-xref href="#table-graphloadingstate-record-fields"></emu-xref>:</p>
<emu-table id="table-graphloadingstate-record-fields" caption="GraphLoadingState Record Fields">
<table>
<thead>
<tr>
<th>
Field Name
</th>
<th>
Value Type
</th>
<th>
Meaning
</th>
</tr>
</thead>
<tr>
<td>
[[PromiseCapability]]
</td>
<td>
a PromiseCapability Record<ins> or ~syncImport~</ins>
</td>
<td>
The promise to resolve when the loading process finishes<ins>, or ~syncImport~ for a synchronous loading operation</ins>.
</td>
</tr>
<tr>
<td>
[[IsLoading]]
</td>
<td>
a Boolean
</td>
<td>
It is true if the loading process has not finished yet, neither successfully nor with an error.
</td>
</tr>
<tr>
<td>
[[PendingModulesCount]]
</td>
<td>
a non-negative integer
</td>
<td>
It tracks the number of pending HostLoadImportedModule calls.
</td>
</tr>
<tr>
<td>
[[Visited]]
</td>
<td>
a List of Cyclic Module Records
</td>
<td>
It is a list of the Cyclic Module Records that have been already loaded by the current loading process, to avoid infinite loops with circular dependencies.
</td>
</tr>
<tr>
<td>
[[HostDefined]]
</td>
<td>
anything (default value is ~empty~)
</td>
<td>
It contains host-defined data to pass from the LoadRequestedModules caller to HostLoadImportedModule.
</td>
</tr>
</table>
</emu-table>
<emu-clause id="sec-LoadRequestedModules" type="concrete method">
<h1>
LoadRequestedModules (
optional _hostDefined_: anything,
): a Promise
</h1>
<dl class="header">
<dt>for</dt>
<dd>a Cyclic Module Record _module_</dd>
<dt>description</dt>
<dd>It populates the [[LoadedModules]] of all the Module Records in the dependency graph of _module_ (most of the work is done by the auxiliary function InnerModuleLoading). It takes an optional _hostDefined_ parameter that is passed to the HostLoadImportedModule hook.</dd>
</dl>
<emu-alg>
1. If _hostDefined_ is not present, let _hostDefined_ be ~empty~.
1. Let _pc_ be ! NewPromiseCapability(%Promise%).
1. Let _state_ be the GraphLoadingState Record { [[IsLoading]]: *true*, [[PendingModulesCount]]: 1, [[Visited]]: « », [[PromiseCapability]]: _pc_, [[HostDefined]]: _hostDefined_ }.
1. Perform InnerModuleLoading(_state_, _module_).
1. Return _pc_.[[Promise]].
</emu-alg>
<emu-note>
The _hostDefined_ parameter can be used to pass additional information necessary to fetch the imported modules. It is used, for example, by HTML to set the correct fetch destination for <code><link rel="preload" as="..."></code> tags.
<code>import()</code> expressions never set the _hostDefined_ parameter.
</emu-note>
<emu-clause id="sec-InnerModuleLoadingSync" type="abstract operation">
<h1>
InnerModuleLoadingSync (
_state_: a GraphLoadingState Record,
_module_: a Module Record,
): ~unused~
</h1>
<dl class="header">
<dt>description</dt>
<dd>It is used by LoadRequestedModules to recursively perform the actual loading process for _module_'s dependency graph.</dd>
</dl>
<emu-alg>
1. TODO.
</emu-alg>
</emu-clause>
<emu-clause id="sec-InnerModuleLoading" type="abstract operation">
<h1>
InnerModuleLoading (
_state_: a GraphLoadingState Record,
_module_: a Module Record,
): ~unused~
</h1>
<dl class="header">
<dt>description</dt>
<dd>It is used by LoadRequestedModules to recursively perform the actual loading process for _module_'s dependency graph.</dd>
</dl>
<emu-alg>
1. Assert: _state_.[[IsLoading]] is *true*.
1. If _module_ is a Cyclic Module Record, _module_.[[Status]] is ~new~, and _state_.[[Visited]] does not contain _module_, then
1. Append _module_ to _state_.[[Visited]].
1. Let _requestedModulesCount_ be the number of elements in _module_.[[RequestedModules]].
1. Set _state_.[[PendingModulesCount]] to _state_.[[PendingModulesCount]] + _requestedModulesCount_.
1. For each ModuleRequest Record _request_ of _module_.[[RequestedModules]], do
1. If AllImportAttributesSupported(_request_.[[Attributes]]) is *false*, then
1. Let _error_ be ThrowCompletion(a newly created *SyntaxError* object).
1. Perform ContinueModuleLoading(_state_, _error_).
1. Else if _module_.[[LoadedModules]] contains a LoadedModuleRequest Record _record_ such that ModuleRequestsEqual(_record_, _request_) is *true*, then
1. Perform InnerModuleLoading(_state_, _record_.[[Module]]).
1. Else,
1. Perform HostLoadImportedModule(_module_, _request_, _state_.[[HostDefined]], _state_).
1. NOTE: HostLoadImportedModule will call FinishLoadingImportedModule, which re-enters the graph loading process through ContinueModuleLoading.
1. If _state_.[[IsLoading]] is *false*, return ~unused~.
1. Assert: _state_.[[PendingModulesCount]] ≥ 1.
1. Set _state_.[[PendingModulesCount]] to _state_.[[PendingModulesCount]] - 1.
1. If _state_.[[PendingModulesCount]] = 0, then
1. Set _state_.[[IsLoading]] to *false*.
1. For each Record _loaded_ of _state_.[[Visited]], do
1. If _loaded_.[[Status]] is ~new~, set _loaded_.[[Status]] to ~unlinked~.
1. Perform ! Call(_state_.[[PromiseCapability]].[[Resolve]], *undefined*, « *undefined* »).
1. Return ~unused~.
</emu-alg>
</emu-clause>
<emu-clause id="sec-ContinueModuleLoading" type="abstract operation">
<h1>
ContinueModuleLoading (
_state_: a GraphLoadingState Record,
_moduleCompletion_: either a normal completion containing a Module Record or a throw completion,
): ~unused~
</h1>
<dl class="header">
<dt>description</dt>
<dd>It is used to re-enter the loading process after a call to HostLoadImportedModule.</dd>
</dl>
<emu-alg>
1. If _state_.[[IsLoading]] is *false*, return ~unused~.
1. If _moduleCompletion_ is a normal completion, then
1. Perform InnerModuleLoading(_state_, _moduleCompletion_.[[Value]]).
1. Else,
1. Set _state_.[[IsLoading]] to *false*.
1. Perform ! Call(_state_.[[PromiseCapability]].[[Reject]], *undefined*, « _moduleCompletion_.[[Value]] »).
1. Return ~unused~.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-moduleevaluation" type="concrete method">
<h1>Evaluate ( ): a Promise</h1>
<dl class="header">
<dt>for</dt>
<dd>a Cyclic Module Record _module_</dd>
<dt>description</dt>
<dd>Evaluate transitions this module's [[Status]] from ~linked~ to either ~evaluating-async~ or ~evaluated~. The first time it is called on a module in a given strongly connected component, Evaluate creates and returns a Promise which resolves when the module has finished evaluating. This Promise is stored in the [[TopLevelCapability]] field of the [[CycleRoot]] for the component. Future invocations of Evaluate on any module in the component return the same Promise. (Most of the work is done by the auxiliary function InnerModuleEvaluation.)</dd>
</dl>
<emu-alg>
1. Assert: None of _module_ or any of its recursive dependencies have [[Status]] set to ~evaluating~, ~linking~, ~unlinked~, or ~new~.
1. Assert: _module_.[[Status]] is one of ~linked~, ~evaluating-async~, or ~evaluated~.
1. If _module_.[[Status]] is either ~evaluating-async~ or ~evaluated~, set _module_ to _module_.[[CycleRoot]].
1. If _module_.[[TopLevelCapability]] is not ~empty~, then
1. Return _module_.[[TopLevelCapability]].[[Promise]].
1. Let _stack_ be a new empty List.
1. Let _capability_ be ! NewPromiseCapability(%Promise%).
1. Set _module_.[[TopLevelCapability]] to _capability_.
1. Let _result_ be Completion(InnerModuleEvaluation(_module_, _stack_, 0)).
1. If _result_ is an abrupt completion, then
1. For each Cyclic Module Record _m_ of _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~.
1. Assert: _module_.[[EvaluationError]] is _result_.
1. Perform ! Call(_capability_.[[Reject]], *undefined*, « _result_.[[Value]] »).
1. Else,
1. Assert: _module_.[[Status]] is either ~evaluating-async~ or ~evaluated~.
1. Assert: _module_.[[EvaluationError]] is ~empty~.
1. If _module_.[[Status]] is ~evaluated~, then
1. NOTE: This implies that evaluation of _module_ completed synchronously.
1. Assert: _module_.[[AsyncEvaluationOrder]] is ~unset~.
1. Perform ! Call(_capability_.[[Resolve]], *undefined*, « *undefined* »).
1. Assert: _stack_ is empty.
1. Return _capability_.[[Promise]].
</emu-alg>
<emu-clause id="sec-innermoduleevaluation" type="abstract operation">
<h1>
InnerModuleEvaluation (
_module_: a Module Record,
_stack_: a List of Cyclic Module Records,
_index_: a non-negative integer,
): either a normal completion containing a non-negative integer or a throw completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>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 [[DFSAncestorIndex]] field, are used the same way as in InnerModuleLinking.</dd>
</dl>
<emu-alg>
1. If _module_ is not a Cyclic Module Record, then
1. Perform ? EvaluateModuleSync(_module_).
1. Return _index_.
1. If _module_.[[Status]] is either ~evaluating-async~ or ~evaluated~, 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. Let _moduleIndex_ be _index_.
1. Set _module_.[[DFSAncestorIndex]] to _index_.
1. Set _module_.[[PendingAsyncDependencies]] to 0.
1. Set _index_ to _index_ + 1.
1. Let _evaluationList_ be a new empty List.
1. For each ModuleRequest Record _required_ of _module_.[[RequestedModules]], do
1. Let _requiredModule_ be GetImportedModule(_module_, _required_.[[Specifier]]).
1. If _required_.[[Phase]] is ~defer~, then
1. Let _additionalModules_ be GatherAsynchronousTransitiveDependencies(_requiredModule_).
1. For each Module Record _additionalModule_ of _additionalModules_, do
1. If _evaluationList_ does not contain _additionalModule_, then
1. Append _additionalModule_ to _evaluationList_.
1. Else if _evaluationList_ does not contain _requiredModule_, then
1. Append _requiredModule_ to _evaluationList_.
1. Append _module_ to _stack_.
1. For each Module Record _requiredModule_ of _evaluationList_, do
1. Set _index_ to ? InnerModuleEvaluation(_requiredModule_, _stack_, _index_).
1. If _requiredModule_ is a Cyclic Module Record, then
1. Assert: _requiredModule_.[[Status]] is one of ~evaluating~, ~evaluating-async~, or ~evaluated~.
1. Assert: _requiredModule_.[[Status]] is ~evaluating~ if and only if _stack_ contains _requiredModule_.
1. If _requiredModule_.[[Status]] is ~evaluating~, then
1. Set _module_.[[DFSAncestorIndex]] to min(_module_.[[DFSAncestorIndex]], _requiredModule_.[[DFSAncestorIndex]]).
1. Else,
1. Set _requiredModule_ to _requiredModule_.[[CycleRoot]].
1. Assert: _requiredModule_.[[Status]] is either ~evaluating-async~ or ~evaluated~.
1. If _requiredModule_.[[EvaluationError]] is not ~empty~, return ? _requiredModule_.[[EvaluationError]].
1. If _requiredModule_.[[AsyncEvaluationOrder]] is an integer, then
1. Set _module_.[[PendingAsyncDependencies]] to _module_.[[PendingAsyncDependencies]] + 1.
1. Append _module_ to _requiredModule_.[[AsyncParentModules]].
1. If _module_.[[PendingAsyncDependencies]] > 0 or _module_.[[HasTLA]] is *true*, then
1. Assert: _module_.[[AsyncEvaluationOrder]] is ~unset~.
1. Set _module_.[[AsyncEvaluationOrder]] to IncrementModuleAsyncEvaluationCount().
1. If _module_.[[PendingAsyncDependencies]] = 0, perform ExecuteAsyncModule(_module_).
1. Else,
1. Perform ? <emu-meta effects="user-code">_module_.ExecuteModule()</emu-meta>.
1. Assert: _module_ occurs exactly once in _stack_.
1. Assert: _module_.[[DFSAncestorIndex]] ≤ _moduleIndex_.
1. If _module_.[[DFSAncestorIndex]] = _moduleIndex_, then
1. Let _done_ be *false*.
1. Repeat, while _done_ is *false*,
1. Let _requiredModule_ be the last element of _stack_.
1. Remove the last element of _stack_.
1. Assert: _requiredModule_ is a Cyclic Module Record.
1. Assert: _requiredModule_.[[AsyncEvaluationOrder]] is either an integer or ~unset~.
1. If _requiredModule_.[[AsyncEvaluationOrder]] is ~unset~, set _requiredModule_.[[Status]] to ~evaluated~.
1. Otherwise, set _requiredModule_.[[Status]] to ~evaluating-async~.
1. If _requiredModule_ and _module_ are the same Module Record, set _done_ to *true*.
1. Set _requiredModule_.[[CycleRoot]] to _module_.
1. Return _index_.
</emu-alg>
<emu-note>
<p>A module is ~evaluating~ while it is being traversed by InnerModuleEvaluation. A module is ~evaluated~ on execution completion or ~evaluating-async~ during execution if its [[HasTLA]] field is *true* or if it has asynchronous dependencies.</p>
</emu-note>
<emu-note>
<p>Any modules depending on a module of an asynchronous 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.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-GatherAsynchronousTransitiveDependencies" type="abstract operation">
<h1>
GatherAsynchronousTransitiveDependencies (
_module_: a Module Record,
optional _seen_: a List of Module Records
): a List of Module Records
</h1>
<dl class="header">
<dt>description</dt>
<dd>Collects the direct post-order list of asynchronous unexecuted transitive dependencies, stopping the depth-first search for a branch when an asynchronous dependency is found.</dd>
</dl>
<emu-alg>
1. If _seen_ is not present, set _seen_ to a new empty List.
1. Let _result_ be a new empty List.
1. If _seen_ contains _module_, return _result_.
1. Append _module_ to _seen_.
1. If _module_ is not a Cyclic Module Record, return _result_.
1. If _module_.[[Status]] is either ~evaluating~ or ~evaluated~, return _result_.
1. If _module_.[[HasTLA]] is *true*, then
1. Append _module_ to _result_.
1. Return _result_.
1. For each ModuleRequest Record _required_ of _module_.[[RequestedModules]], do
1. Let _requiredModule_ be GetImportedModule(_module_, _required_).
1. Let _additionalModules_ be GatherAsynchronousTransitiveDependencies(_requiredModule_, _seen_).
1. For each Module Record _m_ of _additionalModules_, do
1. If _result_ does not contain _m_, append _m_ to _result_.
1. Return _result_.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-HostLoadImportedModule" type="host-defined abstract operation" oldids="sec-hostresolveimportedmodule,sec-hostimportmoduledynamically">
<h1>
HostLoadImportedModule (
_referrer_: a Script Record, a Cyclic Module Record, or a Realm Record,
_moduleRequest_: a ModuleRequest Record,
_hostDefined_: anything,
<del>_payload_: a GraphLoadingState Record or a PromiseCapability Record,</del>
<ins>_payload_: a GraphLoadingState Record, a PromiseCapability Record, or a Completion Record,</ins>
): ~unused~
</h1>
<dl class="header">
<dt>description</dt>
<dd></dd>
</dl>
<emu-note id="note-HostLoadImportedModule-referrer-Realm-Record">
<p>An example of when _referrer_ can be a Realm Record is in a web browser host. There, if a user clicks on a control given by</p>
<pre><code class="html"><button type="button" onclick="import('./foo.mjs')">Click me</button></code></pre>
<p>there will be no active script or module at the time the <emu-xref href="#sec-import-calls">`import()`</emu-xref> expression runs. More generally, this can happen in any situation where the host pushes execution contexts with *null* ScriptOrModule components onto the execution context stack.</p>
</emu-note>
<p>An implementation of HostLoadImportedModule must conform to the following requirements:</p>
<ul>
<li>
The host environment must perform FinishLoadingImportedModule(_referrer_, _moduleRequest_, _payload_, _result_), where _result_ is either a normal completion containing the loaded Module Record or a throw completion, either synchronously or asynchronously.
</li>
<li>
<ins>If _payload_ is a CompletionRecord, or _payload_ is a GraphLoadingState and _payload_.[[PromiseCapability]] is ~syncImport~, then the host environment must call FinishLoadingImportedModule(_referrer_, _moduleRequest_, _payload_, _result_) synchronously.</ins>
</li>
<li>
<p>If this operation is called multiple times with two (_referrer_, _moduleRequest_) pairs such that:</p>
<ul>
<li>the first _referrer_ is the same as the second _referrer_;</li>
<li>ModuleRequestsEqual(the first _moduleRequest_, the second _moduleRequest_) is *true*;</li>
</ul>
<p>and it performs FinishLoadingImportedModule(_referrer_, _moduleRequest_, _payload_, _result_) where _result_ is a normal completion, then it must perform FinishLoadingImportedModule(_referrer_, _moduleRequest_, _payload_, _result_) with the same _result_ each time.</p>
</li>
<li>
<p>If _moduleRequest_.[[Attributes]] has an entry _entry_ such that _entry_.[[Key]] is *"type"* and _entry_.[[Value]] is *"json"*, when the host environment performs FinishLoadingImportedModule(_referrer_, _moduleRequest_, _payload_, _result_), _result_ must either be the Completion Record returned by an invocation of ParseJSONModule or a throw completion.</p>
</li>
<li>
The operation must treat _payload_ as an opaque value to be passed through to FinishLoadingImportedModule.
</li>
</ul>
<p>The actual process performed is host-defined, but typically consists of performing whatever I/O operations are necessary to load the appropriate Module Record. Multiple different (_referrer_, _moduleRequest_.[[Specifier]], _moduleRequest_.[[Attributes]]) triples may map to the same Module Record instance. The actual mapping semantics is host-defined but typically a normalization process is applied to _specifier_ as part of the mapping process. A typical normalization process would include actions such as expansion of relative and abbreviated path specifiers.</p>
<emu-note>
<p>The above text requires that hosts support JSON modules when imported with `type: "json"` (and HostLoadImportedModule completes normally), but it does not prohibit hosts from supporting JSON modules when imported without `type: "json"`.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-FinishLoadingImportedModule" type="abstract operation" oldids="sec-finishdynamicimport" number="11">
<h1>
FinishLoadingImportedModule (
_referrer_: a Script Record, a Cyclic Module Record, or a Realm Record,
_moduleRequest_: a ModuleRequest Record,
<del>_payload_: a GraphLoadingState Record or a PromiseCapability Record,</del>
<ins>_payload_: a GraphLoadingState Record, a PromiseCapability Record, or a Completion Record,</ins>
_result_: either a normal completion containing a Module Record or a throw completion,
): ~unused~
</h1>
<dl class="header">
<dt>description</dt>
<dd></dd>
</dl>
<emu-alg>
1. If _result_ is a normal completion, then
1. If _referrer_.[[LoadedModules]] contains a LoadedModuleRequest Record _record_ such that ModuleRequestsEqual(_record_, _moduleRequest_) is *true*, then
1. Assert: _record_.[[Module]] and _result_.[[Value]] are the same Module Record.
1. Else,
1. Append the LoadedModuleRequest Record { [[Specifier]]: _moduleRequest_.[[Specifier]], [[Attributes]]: _moduleRequest_.[[Attributes]], [[Module]]: _result_.[[Value]] } to _referrer_.[[LoadedModules]].
1. If _payload_ is a GraphLoadingState Record, then
1. Perform ContinueModuleLoading(_payload_, _result_).
1. <ins>Else if _payload_ is a Completion Record, then</ins>
1. Assert: _result_ is a normal completion or a throw completion.
1. If _result_ is a throw completion, set _payload_.[[Type]] to ~throw~.
1. Set _payload_.[[Value]] to _result_.[[Value]].
1. Else,
1. Perform ContinueDynamicImport(_payload_, _result_, _moduleRequest_.[[Phase]]).
1. Return ~unused~.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>