/
spec.html
670 lines (591 loc) · 40 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
<!doctype html>
<meta charset="utf8">
<script src="ecmarkup.js"></script>
<link rel="stylesheet" href="ecmarkup.css">
<title>import conditions</title>
<pre class=metadata>
title: Import conditions
status: proposal
stage: 2
location: https://github.com/tc39/proposal-import-conditions
copyright: false
contributors: Sven Sauleau, Myles Borins, Daniel Ehrenberg, Daniel Clark
</pre>
<emu-intro id="intro">
<h1>import conditions</h1>
<p>See <a href="https://github.com/tc39/proposal-import-conditions/blob/master/README.md">the explainer</a> for information.</p>
</emu-intro>
<emu-clause id="sec-syntax">
<h1>Syntax</h1>
<emu-grammar>
ImportDeclaration :
`import` ImportClause FromClause `;`
`import` ModuleSpecifier `;`
<ins>`import` ImportClause FromClause IfClause `;`</ins>
<ins>`import` ModuleSpecifier IfClause `;`</ins>
ExportDeclaration :
`export` ExportFromClause FromClause `;`
<ins>`export` ExportFromClause FromClause IfClause `;`</ins>
`export` NamedExports `;`
`export` VariableStatement[~Yield, ~Await]
`export` Declaration[~Yield, ~Await]
`export` `default` HoistableDeclaration[~Yield, ~Await, +Default]
`export` `default` ClassDeclaration[~Yield, ~Await, +Default]
`export` `default` [lookahead <! {`function`, `async` [no |LineTerminator| here] `function`, `class`}] AssignmentExpression[+In, ~Yield, ~Await] `;`
IfClause :
<ins>`if` `{` ConditionEntries `,`? `}`</ins>
ConditionEntries :
<ins>IdentifierName `:` StringLiteral</ins>
<ins>IdentifierName `:` StringLiteral `,` ConditionEntries</ins>
ImportCall[Yield, Await] :
`import` `(` AssignmentExpression[+In, ?Yield, ?Await] <ins>`,`?</ins> `)`
<ins>`import` `(` AssignmentExpression[+In, ?Yield, ?Await] `,` AssignmentExpression[+In, ?Yield, ?Await] `,`? `)`</ins>
</emu-grammar>
</emu-clause>
<emu-clause id="sec-semantics">
<h1>Semantics</h1>
<emu-note type="editor"><p>Many productions operating on grammar are the same whether or not an |IfClause|/second |ImportCall| parameter is included; the new parameter is ignored. In this section, only the semantically significant changes are included, and the PR to merge into the main specification would fill in the straightforward details.</p></emu-note>
<emu-clause id="sec-import-calls">
<h1>Import Calls</h1>
<emu-clause id="sec-import-call-runtime-semantics-evaluation">
<h1>Runtime Semantics: Evaluation</h1>
<emu-grammar>ImportCall : `import` `(` AssignmentExpression `,`? `)`</emu-grammar>
<emu-alg>
1. Let _referencingScriptOrModule_ be ! GetActiveScriptOrModule().
1. Let _argRef_ be the result of evaluating |AssignmentExpression|.
1. Let _specifier_ be ? GetValue(_argRef_).
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Let _specifierString_ be ToString(_specifier_).
1. IfAbruptRejectPromise(_specifierString_, _promiseCapability_).
1. <ins>Let _moduleRequest_ be a new ModuleRequest Record { [[Specifier]]: _specifierString_, [[Conditions]]: an empty List }.</ins>
1. Perform ! HostImportModuleDynamically(_referencingScriptOrModule_, <del>_specifierString_,</del> <ins>_moduleRequest_,</ins> _promiseCapability_).
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
<ins><emu-grammar>ImportCall : `import` `(` AssignmentExpression `,` AssignmentExpression `,`? `)`</emu-grammar></ins>
<emu-alg>
1. Let _referencingScriptOrModule_ be ! GetActiveScriptOrModule().
1. Let _argRef_ be the result of evaluating <ins>the first</ins> |AssignmentExpression|.
1. Let _specifier_ be ? GetValue(_argRef_).
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Let _specifierString_ be ToString(_specifier_).
1. IfAbruptRejectPromise(_specifierString_, _promiseCapability_).
1. <ins>Let _argRef_ be the result of evaluating the second |AssignmentExpression|.</ins>
1. <ins>Let _arg_ be ? GetValue(_argRef_).</ins>
1. <ins>If _arg_ is *undefined*, let _conditions_ be an empty List.</ins>
1. <ins>Otherwise,</ins>
1. <ins>Let _conditionsObj_ be ? Get(_arg_, *"if"*).</ins>
1. <ins>Let _conditions_ be a new empty List.</ins>
1. <ins>Let _keys_ be EnumerableOwnPropertyNames(_conditionsObj_, ~key~).</ins>
1. <ins>IfAbruptRejectPromise(_keys_, _promiseCapability_).</ins>
1. <ins>For each String _key_ of _keys_,</ins>
1. <ins>Let _value_ be Get(_conditionsObj_, _key_).</ins>
1. <ins>IfAbruptRejectPromise(_value_, _promiseCapability_).</ins>
1. <ins>Append { [[Key]]: _key_, [[Value]], _value_ } to _conditions_.</ins>
1. <ins>Sort _conditions_ by the code point order of the [[Key]] of each entry. NOTE: This sorting is observable only in that hosts are prohibited from distinguishing among conditions by the order they occur in.</ens>
1. <ins>Let _moduleRequest_ be a new ModuleRequest Record { [[Specifier]]: _specifierString_, [[Conditions]]: _conditions_ }.</ins>
1. Perform ! HostImportModuleDynamically(_referencingScriptOrModule_, <del>_specifierString_,</del> <ins>_moduleRequest_,</ins> _promiseCapability_).
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-hostresolveimportedmodule" aoid="HostResolveImportedModule">
<h1>Runtime Semantics: HostResolveImportedModule ( _referencingScriptOrModule_, <del>_specifier_</del> <ins>_moduleRequest_</ins> )</h1>
<p>HostResolveImportedModule is an implementation-defined abstract operation that provides the concrete Module Record subclass instance that corresponds to <del>the |ModuleSpecifier| String, _specifier_,</del><ins>the ModuleRequest Record _moduleRequest_</del> occurring within the context of the script or module represented by the Script Record or Module Record _referencingScriptOrModule_. _referencingScriptOrModule_ may also be *null*, if the resolution is being performed in the context of an <emu-xref href="#sec-import-calls">`import()`</emu-xref> expression, and there is no active script or module at that time.</ins></p>
<emu-note>
<p>An example of when _referencingScriptOrModule_ can be *null* 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>The implementation of HostResolveImportedModule must conform to the following requirements:</p>
<ul>
<li>
The normal return value must be an instance of a concrete subclass of Module Record.
</li>
<li>
If a Module Record corresponding to the pair _referencingScriptOrModule_, <del>_specifier_</del>, <ins>_moduleRequest_</ins> does not exist or cannot be created, an exception must be thrown.
</li>
<li>
Each time this operation is called with a specific _referencingScriptOrModule_, <del>_specifier_,</del> <ins>_moduleRequest_.[[Specifier]]</ins> pair as arguments it must return the same Module Record instance if it completes normally.
</li>
<li>
<ins>_moduleRequest_.[[Conditions]] must not influence the interpretation of the module or the module specifier; instead, it may be used to determine whether the algorithm completes normally or with an abrupt completion.</ins>
</li>
<li>
<ins>
If _conditions_ has an entry _entry_ such that _entry_.[[Key]] is *"type"*, let _type_ be _entry_.[[Value]]. The following requirements apply:
<ul>
<li><ins>If _type_ is *"json"*, then this algorithm must either invoke ParseJSONModule and return the resulting Module Record, or throw an exception.</ins></li>
</ul>
</ins>
</li>
</ul>
<p>Multiple different _referencingScriptOrModule_, <del>_specifier_</del> <ins>_moduleRequest_.[[Specifier]]</ins> pairs may map to the same Module Record instance. The actual mapping semantic is implementation-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 alphabetic case folding and expansion of relative and abbreviated path specifiers.</p>
<emu-note type=editor>
<p>The above text implies that, if a module is imported multiple times with different _moduleRequest_.[[Conditions]] values, then there can be just one possible "successful" value (possibly as a result of multiple different conditions), but that it can also fail with an exception thrown; this exception from one import does not rule out success with a different condition list.</p>
<p>Conditions do not affect the contents of the module or be part of the cache key. Future follow-up proposals may relax this restriction with "evaluator attributes" that would change the contents of the module. There are three possible ways to handle multiple imports of the same module with "evaluator attributes":</p>
<ul>
<li><strong>Race</strong> and use the attribute that was requested by the first import. This seems broken--the second usage is ignored.</li>
<li><strong>Reject</strong> the module graph and don't load if attributes differ. This seems bad for composition--using two unrelated packages together could break, if they load the same module with disagreeing attributes.</li>
<li><strong>Clone</strong> and make two copies of the module, for the different ways it's transformed. In this case, the attributes would have to be part of the cache key. These semantics would run counter to the intuition that there is just one copy of a module.</li>
</ul>
<p>It's possible that one of these three options may make sense for a module load, on a case-by-case basis by attribute, but it's worth careful thought before making this choice.</p>
</emu-note>
<emu-note type=editor>
<p>The above text implies that hosts *must* support JSON modules imported with `type: "json"` (if it completes normally), but it doesn't prohibit hosts from supporting JSON modules imported with no type specified. Some environments (for example, web browsers) are expected to require `if { type: "json" }`, and environments which want to restrict themselves to a compatible subset would do so as well.</p>
</emu-note>
<emu-note type=editor>
<p>All of the import statements in the module graph that address the same JSON module will evaluate to the same mutable object.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-hostimportmoduledynamically" aoid="HostImportModuleDynamically">
<h1>Runtime Semantics: HostImportModuleDynamically ( _referencingScriptOrModule_, <del>_specifier_,<del> <ins>_moduleRequest_,</ins> _promiseCapability_ )</h1>
<p>HostImportModuleDynamically is an implementation-defined abstract operation that performs any necessary setup work in order to make available the module corresponding to the <del>|ModuleSpecifier| String, _specifier_,</del><ins>ModuleRequest Record _moduleRequest_</ins> 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_, <del>_specifier_,</del> <ins>_moduleRequest_</ins>, _promiseCapability_, NormalCompletion(*undefined*)).</li>
<li>Any subsequent call to HostResolveImportedModule after FinishDynamicImport has completed, given the arguments _referencingScriptOrModule_, and <del>_specifier_</del> <ins>_moduleRequest_</ins> must complete normally.</li>
<li>The completion value of any subsequent call to HostResolveImportedModule after FinishDynamicImport has completed, given the arguments _referencingScriptOrModule_, and <del>_specifier_,</del> <ins>_moduleRequest_</ins> 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_, <del>_specifier_,</del> <ins>_moduleRequest_,</ins> _promiseCapability_, an abrupt completion), with the abrupt completion representing the cause of failure.</li>
</ul>
</dd>
</dl>
</li>
<li>
If the host environment takes the success path once for a given _referencingScriptOrModule_, <del>_specifier_,</del> <ins>_moduleRequest_</ins> 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 implementation-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>Runtime Semantics: FinishDynamicImport ( _referencingScriptOrModule_, <del>_specifier_,</del> <ins>_moduleRequest_,</ins> _promiseCapability_, _completion_ )</h1>
<p>The abstract operation FinishDynamicImport takes arguments _referencingScriptOrModule_, <del>_specifier_,</del> <ins>_moduleRequest_ (a ModuleRequest Record),</ins> _promiseCapability_, and _completion_. 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 _completion_. It is performed by host environments as part of HostImportModuleDynamically. It performs the following steps when called:</p>
<emu-alg>
1. If _completion_ is an abrupt completion, then perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _completion_.[[Value]] »).
1. Else,
1. Assert: _completion_ is a normal completion and _completion_.[[Value]] is *undefined*.
1. Let _moduleRecord_ be ! HostResolveImportedModule(_referencingScriptOrModule_, <del>_specifier_,</del> <ins>_moduleRequest_</ins>).
1. Assert: Evaluate has already been invoked on _moduleRecord_ and successfully completed.
1. Let _namespace_ be GetModuleNamespace(_moduleRecord_).
1. If _namespace_ is an abrupt completion, perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _namespace_.[[Value]] »).
1. Else, perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _namespace_.[[Value]] »).
</emu-alg>
</emu-clause>
<emu-clause id="sec-synthetic-module-records">
<h1>Synthetic Module Records</h1>
<emu-note type="editor">This Synthetic Module Records specification text comes from the <a href="github.com/tc39/proposal-javascript-standard-library/">JavaScript Standard Library proposal</a> and was written by Domenic Denicola. A version of this text exists <a href="https://heycam.github.io/webidl/#synthetic-module-records">in WebIDL</a>. A version of the text is copied here to clarify that the concept of Synthetic Module Records can proceed in standardization independently of the built-in modules effort.</emu-note>
<p>A <dfn>Synthetic Module Record</dfn> is used to represent information about a module that is defined by specifications. Its exports are derived from a pair of lists, of string keys and of ECMAScript values. The set of exported names is static, and determined at creation time (as an argument to CreateSyntheticModule), while the set of exported values can be changed over time using SetSyntheticModuleExport. It has no imports or dependencies.</p>
<emu-note>A Synthetic Module Record could be used for defining a variety of module types: for example, built-in modules, or JSON modules, or CSS modules.</emu-note>
<p>In addition to the fields defined in <emu-xref href="#table-36"></emu-xref>, Synthetic Module Records have the additional fields listed in <emu-xref href="#table-synthetic-module-record-fields"></emu-xref>. Each of these fields is initially set in CreateSyntheticModule.</p>
<emu-table id="table-synthetic-module-record-fields" caption="Additional Fields of Synthetic Module Records">
<table>
<thead>
<th>Field Name
<th>Value Type
<th>Meaning
</thead>
<tbody>
<tr>
<td>[[ExportNames]]
<td>List of String
<td>A List of all names that are exported.
</tr>
<tr>
<td>[[EvaluationSteps]]
<td>An abstract operation
<td>An abstract operation that will be performed upon evaluation of the module, taking the Synthetic Module Record as its sole argument. These will usually set up the exported values, by using SetSyntheticModuleExport. They must not modify [[ExportNames]]. They may return an abrupt completion.
</tr>
</tbody>
</table>
</emu-table>
<emu-clause id="sec-createsyntheticmodule" aoid="CreateSyntheticModule">
<h1>CreateSyntheticModule ( _exportNames_, _evaluationSteps_, _realm_, _hostDefined_ )</h1>
<p>The abstract operation CreateSyntheticModule creates a Synthetic Module Record based upon the given exported names and evaluation steps. It performs the following steps:</p>
<emu-alg>
1. Return Synthetic Module Record { [[Realm]]: _realm_, [[Environment]]: *undefined*, [[Namespace]]: *undefined*, [[HostDefined]]: _hostDefined_, [[ExportNames]]: _exportNames_, [[EvaluationSteps]]: _evaluationSteps_ }.
</emu-alg>
<emu-note type="editor">It seems we could set up the environment either here or in Instantiate(). I've chosen to do so in Instantiate() for symmetry with Source Text Module Records, but I don't think there's any actual requirement in that regard.</emu-note>
</emu-clause>
<emu-clause id="sec-setsyntheticmoduleexport" aoid="SetSyntheticModuleExport">
<h1>SetSyntheticModuleExport ( _module_, _exportName_, _exportValue_ )</h1>
<p>The abstract operation SetSyntheticModuleExport can be used to set or change the exported value for a pre-established export of a Synthetic Module Record. It performs the following steps:</p>
<emu-alg>
1. Let _envRec_ be _module_.[[Environment]]'s EnvironmentRecord.
1. Perform _envRec_.SetMutableBinding(_exportName_, _exportValue_, *true*).
</emu-alg>
</emu-clause>
<emu-clause id="sec-smr-concrete-methods">
<h1>Concrete Methods</h1>
<p>The following are the concrete methods for Synthetic Module Record that implement the corresponding Module Record abstract methods.</p>
<emu-note type="editor">I find having this wrapping sub-clause cleaner and suggest we do the same for Source Text Module Records in the main spec.</emu-note>
<emu-clause id="sec-smr-getexportednames">
<h1>GetExportedNames( _exportStarSet_ )</h1>
<p>The GetExportedNames concrete method of a Synthetic Module Record implements the corresponding Module Record abstract method.</p>
<p>It performs the following steps:</p>
<emu-alg>
1. Let _module_ be this Synthetic Module Record.
1. Return _module_.[[ExportNames]].
</emu-alg>
</emu-clause>
<emu-clause id="sec-smr-resolveexport">
<h1>ResolveExport( _exportName_, _resolveSet_ )</h1>
<p>The ResolveExport concrete method of a Synthetic Module Record implements the corresponding Module Record abstract method.</p>
<p>It performs the following steps:</p>
<emu-alg>
1. Let _module_ be this Synthetic Module Record.
1. If _module_.[[ExportNames]] does not contain _exportName_, return null.
1. Return ResolvedBinding Record { [[Module]]: _module_, [[BindingName]]: _exportName_ }.
</emu-alg>
</emu-clause>
<emu-clause id="sec-smr-instantiate">
<h1>Instantiate ( )</h1>
<p>The Instantiate concrete method of a Synthetic Module Record implements the corresponding Module Record abstract method.</p>
<p>It performs the following steps:</p>
<emu-alg>
1. Let _module_ be this Synthetic Module Record.
1. Let _realm_ be _module_.[[Realm]].
1. Assert: _realm_ is not *undefined*.
1. Let _env_ be NewModuleEnvironment(_realm_.[[GlobalEnv]]).
1. Set _module_.[[Environment]] to _env_.
1. Let _envRec_ be _env_'s EnvironmentRecord.
1. For each _exportName_ in _module_.[[ExportNames]],
1. Perform ! _envRec_.CreateMutableBinding(_exportName_, *false*).
1. Perform ! _envRec_.InitializeBinding(_exportName_, *undefined*).
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-smr-Evaluate">
<h1>Evaluate ( )</h1>
<p>The Evaluate concrete method of a Synthetic Module Record implements the corresponding Module Record abstract method.</p>
<p>It performs the following steps:</p>
<emu-alg>
1. Let _module_ be this Synthetic Module Record.
1. Let _moduleCxt_ be a new ECMAScript code execution context.
1. Set the Function of _moduleCxt_ to *null*.
1. Assert: _module_.[[Realm]] is not *undefined*.
1. Set the Realm of _moduleCxt_ to _module_.[[Realm]].
1. Set the ScriptOrModule of _moduleCxt_ to _module_.
1. Set the VariableEnvironment of _moduleCxt_ to _module_.[[Environment]].
1. Set the LexicalEnvironment of _moduleCxt_ to _module_.[[Environment]].
1. Suspend the currently running execution context.
1. Push _moduleCxt_ on to the execution context stack; _moduleCxt_ is now the running execution context.
1. Let _result_ be the result of performing ? _module_.[[EvaluationSteps]](_module_).
1. Suspend _moduleCxt_ and remove it from the execution context stack.
1. Resume the context that is now on the top of the execution context stack as the running execution context.
1. Return Completion(_result_).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-create-default-export-synthetic-module" aoid="CreateDefaultExportSyntheticModule">
<h1>CreateDefaultExportSyntheticModule ( _defaultExport_ )</h1>
<p>The CreateDefaultExportSyntheticModule abstract operation creates a Synthetic Module Record whose default export is _defaultExport_. It performs the following steps:</p>
<emu-alg>
1. Return CreateSyntheticModule(« *"default"* », the following steps, the current Realm, _defaultExport_) with the following steps given _module_ as an argument:</p>
1. SetSyntheticModuleExport(_module_, *"default"*, _module_.[[HostDefined]]).</li>
</emu-alg>
<!-- TODO: Could we apply the new spec closure concept here rather than painstakingly passing _defaultExport_ through [[HostDefined]]? -->
</emu-clause>
<emu-clause id="sec-parse-json-module" aoid="ParseJSONModule">
<h1>ParseJSONModule ( _source_ )</h1>
<p>The abstract operation ParseJSONModule takes a single argument _source_ which is a String representing the contents of a module.</p>
<emu-alg>
1. Let _json_ be ? Call(%JSONParse%, *undefined*, « _source_ »). <!-- TODO: Refactor JSONParse into an abstract algorithm -->
1. Return CreateDefaultExportSyntheticModule(_json_).
</emu-alg>
</emu-clause>
<emu-clause id="sec-if-clause-early-errors">
<h1>Static Semantics: Early Errors</h1>
<emu-grammar>IfClause : `if` `{` ConditionEntries `,`? `}`</emu-grammar>
<ul>
<li>It is a Syntax Error if IfClauseToConditions of |IfClause| has two entries _a_ and _b_ such that _a_.[[Key]] is _b_.[[Key]].</li>
</ul>
</emu-clause>
<emu-clause id="sec-if-clause-to-object" aoid="IfClauseToConditions">
<h1>Runtime Semantics: IfClauseToConditions</h1>
<emu-grammar>IfClause : `if` `{` ConditionEntries `,`? `}`</emu-grammar>
<emu-alg>
1. Let _conditions_ be IfEntriesToConditions of |ConditionEntries|.
1. Sort _conditions_ by the code point order of the [[Key]] of each entry. NOTE: This sorting is observable only in that hosts are prohibited from distinguishing among conditions by the order they occur in.
1. Return _conditions_.
</emu-alg>
<emu-grammar> ConditionEntries : IdentifierName `:` StringLiteral </emu-grammar>
<emu-alg>
1. Let _entry_ be a Record { [[Key]]: StringValue of |IdentifierName|, [[Value]]: StringValue of |StringLiteral| }.
1. Return a new List containing the single element, _entry_.
</emu-alg>
<emu-grammar> ConditionEntries : IdentifierName `:` StringLiteral `,` ConditionEntries </emu-grammar>
<emu-alg>
1. Let _entry_ be a Record { [[Key]]: StringValue of |IdentifierName|, [[Value]]: StringValue of |StringLiteral| }.
1. Let _rest_ be IfEntriesToConditions of |ConditionEntries|.
1. Return a new List containing _entry_ followed by the entries of _rest_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-modulerequest-record">
<h1>ModuleRequest Records</h1>
<p>A <dfn>ModuleRequest Record</dfn> represents the request to import a module with given import conditions. It consists of the following fields:</p>
<emu-table id="table-cyclic-module-fields" caption="ModuleRequest Record fields">
<table>
<tbody>
<tr>
<th>
Field Name
</th>
<th>
Value Type
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
[[Specifier]]
</td>
<td>
String
</td>
<td>
The module specifier
</td>
</tr>
<tr>
<td>
[[Conditions]]
</td>
<td>
a List of Records { [[Key]]: a String, [[Value]]: a String }
</td>
<td>
The import conditions
</td>
</tr>
</tbody>
</table>
</emu-table>
<emu-note type=editor>In general, this proposal replaces places where module specifiers are passed around with ModuleRequest Records. For example, several syntax-directed operations, such as ModuleRequests produce Lists of ModuleRequest Records rather than Lists of Strings which are interpreted as module specifiers. Some algorithms like ImportEntries and ImportEntriesForModule pass around ModuleRequest Records rather than Strings, in a way which doesn't require any particular textual change. Additionally, record fields in Cyclic Module Records and Source Text Module Records which contained Lists of Strings are replaced by Lists of ModuleRequest Records, as indicated above.</emu-note>
</emu-clause>
<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~ | ~evaluated~
</td>
<td>
Initially ~unlinked~. Transitions to ~linking~, ~linked~, ~evaluating~, ~evaluated~ (in that order) as the module progresses throughout its lifecycle.
</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 | *undefined*
</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 ongoing depth-first traversal of the dependency graph.
</td>
</tr>
<tr>
<td>
[[DFSAncestorIndex]]
</td>
<td>
Integer | *undefined*
</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 <del>String</del><ins>ModuleRequest Record</ins>
</td>
<td>
A List of all the |ModuleSpecifier| strings <ins>and import conditions</ins> used by the module represented by this record to request the importation of a module. The List is source code occurrence ordered.
</td>
</tr>
</tbody>
</table>
</emu-table>
<p>An <dfn id="importentry-record">ImportEntry Record</dfn> is a Record that digests information about a single declarative import. Each ImportEntry Record has the fields defined in <emu-xref href="#table-39"></emu-xref>:</p>
<emu-table id="table-39" caption="ImportEntry Record Fields">
<table>
<tbody>
<tr>
<th>
Field Name
</th>
<th>
Value Type
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
[[ModuleRequest]]
</td>
<td>
<del>String</del>
<ins>ModuleRequest Record</ins>
</td>
<td>
<del>String value of the |ModuleSpecifier| of the |ImportDeclaration|.</del>
<ins>ModuleRequest Record representing the |ModuleSpecifier| and import conditions of the |ImportDeclaration|.</ins>
</td>
</tr>
<tr>
<td>
[[ImportName]]
</td>
<td>
String
</td>
<td>
The name under which the desired binding is exported by the module identified by [[ModuleRequest]]. The value *"\*"* indicates that the import request is for the target module's namespace object.
</td>
</tr>
<tr>
<td>
[[LocalName]]
</td>
<td>
String
</td>
<td>
The name that is used to locally access the imported value from within the importing module.
</td>
</tr>
</tbody>
</table>
</emu-table>
<emu-clause id="sec-static-semantics-modulerequests">
<h1>Static Semantics: ModuleRequests</h1>
<emu-see-also-para op="ModuleRequests"></emu-see-also-para>
<emu-grammar>ImportDeclaration : `import` ImportClause FromClause `;`</emu-grammar>
<emu-alg>
1. <del>Return ModuleRequests of |FromClause|.</del>
1. <ins>Let _specifier_ be StringValue of the |StringLiteral| contained in |FromClause|.</ins>
1. <ins>Return a ModuleRequest Record { [[Specifer]]: _specifier_, [[Conditions]]: an empty List }.</ins>
</emu-alg>
<emu-grammar>ImportDeclaration : `import` ImportClause FromClause IfClause `;`</emu-grammar>
<emu-alg>
1. <ins>Let _specifier_ be StringValue of the |StringLiteral| contained in |FromClause|.</ins>
1. <ins>Let _conditions_ be IfClauseToConditions of |IfClause|.</ins>
1. <ins>Return a ModuleRequest Record { [[Specifer]]: _specifier_, [[Conditions]]: _conditions_ }.</ins>
</emu-alg>
<emu-grammar>Module : [empty]</emu-grammar>
<emu-alg>
1. Return a new empty List.
</emu-alg>
<emu-grammar>ModuleItemList : ModuleItem</emu-grammar>
<emu-alg>
1. Return ModuleRequests of |ModuleItem|.
</emu-alg>
<emu-grammar>ModuleItemList : ModuleItemList ModuleItem</emu-grammar>
<emu-alg>
1. Let _moduleNames_ be ModuleRequests of |ModuleItemList|.
1. Let _additionalNames_ be ModuleRequests of |ModuleItem|.
1. Append to _moduleNames_ each element of _additionalNames_ <del>that is not already an element of _moduleNames_</del>.
1. Return _moduleNames_.
</emu-alg>
<emu-note type=editor>Deletion of duplicates is an unnecessary "spec optimization" that would be more complicated to explain in terms of examining import condition records, and can be simply removed.</emu-note>
<emu-grammar>ModuleItem : StatementListItem</emu-grammar>
<emu-alg>
1. Return a new empty List.
</emu-alg>
<emu-grammar>
ExportDeclaration : `export` ExportFromClause FromClause `;`
</emu-grammar>
<emu-alg>
1. <del>Return ModuleRequests of |FromClause|.</del>
1. <ins>Let _specifier_ be StringValue of the |StringLiteral| contained in |FromClause|.</ins>
1. <ins>Return a ModuleRequest Record { [[Specifer]]: _specifier_, [[Conditions]]: an empty List }.</ins>
</emu-alg>
<emu-grammar>
ExportDeclaration : `export` ExportFromClause FromClause IfClause `;`
</emu-grammar>
<emu-alg>
1. <ins>Let _specifier_ be StringValue of the |StringLiteral| contained in |FromClause|.</ins>
1. <ins>Let _conditions_ be IfClauseToObject of |IfClause|.</ins>
1. <ins>Return a ModuleRequest Record { [[Specifer]]: _specifier_, [[Conditions]]: _conditions_ }.</ins>
</emu-alg>
<emu-grammar>
ExportDeclaration :
`export` NamedExports `;`
`export` VariableStatement
`export` Declaration
`export` `default` HoistableDeclaration
`export` `default` ClassDeclaration
`export` `default` AssignmentExpression `;`
</emu-grammar>
<emu-alg>
1. Return a new empty List.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-annex id="sec-host-integration">
<h1>Sample host integration: The Web embedding</h1>
<p>The import conditions proposal is intended to give key information about how modules are interpreted to hosts. For the Web embedding and environments which aim to be similar to it, the string is interpreted as the "module type". This is not the primary way the module type is determined (which, on the Web, would be the MIME type, and in other environments may be the file extension), but rather a secondary check which is required to pass for the module graph to load.</p>
<p>In the Web embedding, the following changes would be made to the HTML specification for import conditions:</p>
<ul>
<li>The <a href="https://html.spec.whatwg.org/#module-script">module script</a> would have an additional item, which would be the module type, as a string (e.g., *"json"*), or *undefined* for a JavaScript module.</li>
<li>HostResolveImportedModule and HostImportModuleDynamically would take a ModuleRequest Record parameter in place of a specifier string, which would be passed down through several abstract operations to reach the <a href="https://html.spec.whatwg.org/#fetch-a-single-module-script">fetch a single module script</a> algorithm. Somewhere near the entrypoint, if the ModuleRequest Record's [[Conditions]] field has an entry _entry_ such that _entry_.[[Key]] is *"type"*, then let _type_ be _entry_.[[Value]]; otherwise let _type_ be *undefined*. If the type is invalid, then an exception is thrown and module loading fails. Otherwise, this will equal the module type, if the module can be successfully fetched with a matching MIME type.</li>
<li>In the <a href="https://html.spec.whatwg.org/#fetch-the-descendants-of-a-module-script">fetch the descendents of a module script</a> algorithm, when iterating over [[RequestedModules]], the elements are ModuleRequest Records rather than just specifier strings; these Records is passed on to the internal module script graph fetching procedure (which sends it to "fetch a single module script". Other usage sites of [[RequestedModules]] ignore the condition.</li>
<li>"Fetch a single module script" would check the condition in two places:
<ul>
<li>If the module is found in the module map, then _type_ is checked against he module script's type field. If they differ, then an exception is thrown and module loading fails.</li>
<li>When a new module is fetched, before writing it into the module map, the MIME type is checked to ensure that it matches _type_. (Note that the interpretation of the module is still driven by the MIME type, but once the MIME type is established, this is checked against the _type_.) If they differ, then an exception is thrown and module loading fails. The _type_ is written into the module script as the type.</li>
</ul>
</li>
</ul>
<p>Note that the module map remains keyed by the absolute URL; the _type_ is not part of the module map key, and initially, no other import conditions are supported, so they are also not present.</p>
</emu-annex>