Skip to content

Commit 94c6f97

Browse files
littledandomenic
authored andcommitted
Normative: Add dynamic import() (#1482)
This PR includes minor changes to rebase, which are out for review in tc39/proposal-dynamic-import#72 Co-authored-by: Domenic Denicola <[email protected]>
1 parent 2829edc commit 94c6f97

File tree

1 file changed

+107
-5
lines changed

1 file changed

+107
-5
lines changed

spec.html

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12267,6 +12267,7 @@ <h2>Syntax</h2>
1226712267
CallExpression[Yield, Await] :
1226812268
CoverCallExpressionAndAsyncArrowHead[?Yield, ?Await] #callcover
1226912269
SuperCall[?Yield, ?Await]
12270+
ImportCall[?Yield, ?Await]
1227012271
CallExpression[?Yield, ?Await] Arguments[?Yield, ?Await]
1227112272
CallExpression[?Yield, ?Await] `[` Expression[+In, ?Yield, ?Await] `]`
1227212273
CallExpression[?Yield, ?Await] `.` IdentifierName
@@ -12275,6 +12276,9 @@ <h2>Syntax</h2>
1227512276
SuperCall[Yield, Await] :
1227612277
`super` Arguments[?Yield, ?Await]
1227712278

12279+
ImportCall[Yield, Await] :
12280+
`import` `(` AssignmentExpression[+In, ?Yield, ?Await] `)`
12281+
1227812282
Arguments[Yield, Await] :
1227912283
`(` `)`
1228012284
`(` ArgumentList[?Yield, ?Await] `)`
@@ -12431,6 +12435,7 @@ <h1>Static Semantics: AssignmentTargetType</h1>
1243112435
CallExpression :
1243212436
CoverCallExpressionAndAsyncArrowHead
1243312437
SuperCall
12438+
ImportCall
1243412439
CallExpression Arguments
1243512440
CallExpression TemplateLiteral
1243612441

@@ -12725,6 +12730,26 @@ <h1>Runtime Semantics: ArgumentListEvaluation</h1>
1272512730
</emu-clause>
1272612731
</emu-clause>
1272712732

12733+
<emu-clause id="sec-import-calls">
12734+
<h1>Import Calls</h1>
12735+
12736+
<emu-clause id="sec-import-call-runtime-semantics-evaluation">
12737+
<h1>Runtime Semantics: Evaluation</h1>
12738+
12739+
<emu-grammar>ImportCall : `import` `(` AssignmentExpression `)`</emu-grammar>
12740+
<emu-alg>
12741+
1. Let _referencingScriptOrModule_ be ! GetActiveScriptOrModule().
12742+
1. Let _argRef_ be the result of evaluating |AssignmentExpression|.
12743+
1. Let _specifier_ be ? GetValue(_argRef_).
12744+
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
12745+
1. Let _specifierString_ be ToString(_specifier_).
12746+
1. IfAbruptRejectPromise(_specifierString_, _promiseCapability_).
12747+
1. Perform ! HostImportModuleDynamically(_referencingScriptOrModule_, _specifierString_, _promiseCapability_).
12748+
1. Return _promiseCapability_.[[Promise]].
12749+
</emu-alg>
12750+
</emu-clause>
12751+
</emu-clause>
12752+
1272812753
<emu-clause id="sec-tagged-templates">
1272912754
<h1>Tagged Templates</h1>
1273012755
<emu-note>
@@ -22594,21 +22619,93 @@ <h1>ExecuteModule ( ) Concrete Method</h1>
2259422619
</emu-clause>
2259522620

2259622621
<emu-clause id="sec-hostresolveimportedmodule" aoid="HostResolveImportedModule">
22597-
<h1>Runtime Semantics: HostResolveImportedModule ( _referencingModule_, _specifier_ )</h1>
22598-
<p>HostResolveImportedModule is an implementation-defined abstract operation that provides the concrete Module Record subclass instance that corresponds to the |ModuleSpecifier| String, _specifier_, occurring within the context of the module represented by the Module Record _referencingModule_.</p>
22622+
<h1>Runtime Semantics: HostResolveImportedModule ( _referencingScriptOrModule_, _specifier_ )</h1>
22623+
<p>HostResolveImportedModule is an implementation-defined abstract operation that provides the concrete Module Record subclass instance that corresponds 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 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.</p>
22624+
22625+
<emu-note>
22626+
<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>
22627+
22628+
<pre><code class="html">&lt;button type="button" onclick="import('./foo.mjs')"&rt;Click me&lt;/button&rt;</code></pre>
22629+
22630+
<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>
22631+
</emu-note>
22632+
2259922633
<p>The implementation of HostResolveImportedModule must conform to the following requirements:</p>
2260022634
<ul>
2260122635
<li>
2260222636
The normal return value must be an instance of a concrete subclass of Module Record.
2260322637
</li>
2260422638
<li>
22605-
If a Module Record corresponding to the pair _referencingModule_, _specifier_ does not exist or cannot be created, an exception must be thrown.
22639+
If a Module Record corresponding to the pair _referencingScriptOrModule_, _specifier_ does not exist or cannot be created, an exception must be thrown.
2260622640
</li>
2260722641
<li>
22608-
This operation must be idempotent if it completes normally. Each time it is called with a specific _referencingModule_, _specifier_ pair as arguments it must return the same Module Record instance.
22642+
This operation must be idempotent if it completes normally. Each time it is called with a specific _referencingScriptOrModule_, _specifier_ pair as arguments it must return the same Module Record instance.
2260922643
</li>
2261022644
</ul>
22611-
<p>Multiple different _referencingModule_, _specifier_ 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>
22645+
<p>Multiple different _referencingScriptOrModule_, _specifier_ 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>
22646+
</emu-clause>
22647+
22648+
<emu-clause id="sec-hostimportmoduledynamically" aoid="HostImportModuleDynamically">
22649+
<h1>Runtime Semantics: HostImportModuleDynamically ( _referencingScriptOrModule_, _specifier_, _promiseCapability_ )</h1>
22650+
22651+
<p>HostImportModuleDynamically is an implementation-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>
22652+
22653+
<p>The implementation of HostImportModuleDynamically must conform to the following requirements:</p>
22654+
22655+
<ul>
22656+
<li>
22657+
The abstract operation must always complete normally with *undefined*. Success or failure must instead be signaled as discussed below.
22658+
</li>
22659+
<li>
22660+
The host environment must conform to one of the two following sets of requirements:
22661+
<dl>
22662+
<dt>Success path</dt>
22663+
22664+
<dd>
22665+
<ul>
22666+
<li>At some future time, the host environment must perform FinishDynamicImport(_referencingScriptOrModule_, _specifier_, _promiseCapability_, NormalCompletion(*undefined*)).</li>
22667+
22668+
<li>Any subsequent call to HostResolveImportedModule after FinishDynamicImport has completed, given the arguments _referencingScriptOrModule_ and _specifier_, must complete normally.</li>
22669+
22670+
<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>
22671+
</ul>
22672+
</dd>
22673+
22674+
<dt>Failure path</dt>
22675+
22676+
<dd>
22677+
<ul>
22678+
<li>At some future time, the host environment must perform FinishDynamicImport(_referencingScriptOrModule_, _specifier_, _promiseCapability_, an abrupt completion), with the abrupt completion representing the cause of failure.</li>
22679+
</ul>
22680+
</dd>
22681+
</dl>
22682+
</li>
22683+
<li>
22684+
Every call to HostImportModuleDynamically with the same _referencingScriptOrModule_ and _specifier_ arguments must conform to the <em>same</em> set of requirements above as previous calls do. That is, if the host environment takes the success path once for a given _referencingScriptOrModule_, _specifier_ pair, it must always do so, and the same for the failure path.
22685+
</li>
22686+
<li>
22687+
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.
22688+
</li>
22689+
</ul>
22690+
22691+
<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>
22692+
</emu-clause>
22693+
22694+
<emu-clause id="sec-finishdynamicimport" aoid="FinishDynamicImport">
22695+
<h1>Runtime Semantics: FinishDynamicImport ( _referencingScriptOrModule_, _specifier_, _promiseCapability_, _completion_ )</h1>
22696+
22697+
<p>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.</p>
22698+
22699+
<emu-alg>
22700+
1. If _completion_ is an abrupt completion, then perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _completion_.[[Value]] »).
22701+
1. Else,
22702+
1. Assert: _completion_ is a normal completion and _completion_.[[Value]] is *undefined*.
22703+
1. Let _moduleRecord_ be ! HostResolveImportedModule(_referencingScriptOrModule_, _specifier_).
22704+
1. Assert: Evaluate has already been invoked on _moduleRecord_ and successfully completed.
22705+
1. Let _namespace_ be GetModuleNamespace(_moduleRecord_).
22706+
1. If _namespace_ is an abrupt completion, perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _namespace_.[[Value]] »).
22707+
1. Else, perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _namespace_.[[Value]] »).
22708+
</emu-alg>
2261222709
</emu-clause>
2261322710

2261422711
<emu-clause id="sec-getmodulenamespace" aoid="GetModuleNamespace">
@@ -23343,6 +23440,10 @@ <h1>Forbidden Extensions</h1>
2334323440
<li>
2334423441
When parsing for the |Module| goal symbol, the lexical grammar extensions defined in <emu-xref href="#sec-html-like-comments"></emu-xref> must not be supported.
2334523442
</li>
23443+
<li>
23444+
|ImportCall| must not be extended.
23445+
<!-- This is so that in the future we can potentially add new arguments or support ArgumentList. -->
23446+
</li>
2334623447
</ul>
2334723448
</emu-clause>
2334823449
</emu-clause>
@@ -39758,6 +39859,7 @@ <h1>Expressions</h1>
3975839859
<p>&nbsp;</p>
3975939860

3976039861
<emu-prodref name=SuperCall></emu-prodref>
39862+
<emu-prodref name=ImportCall></emu-prodref>
3976139863
<emu-prodref name=Arguments></emu-prodref>
3976239864
<emu-prodref name=ArgumentList></emu-prodref>
3976339865
<emu-prodref name=LeftHandSideExpression></emu-prodref>

0 commit comments

Comments
 (0)