Skip to content

Commit

Permalink
fixes #77: importMetaHook
Browse files Browse the repository at this point in the history
  • Loading branch information
caridy committed Aug 22, 2022
1 parent e3d74c8 commit b7899d1
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 47 deletions.
89 changes: 76 additions & 13 deletions 0-module-and-module-source.emu
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ location: https://tc39.es/proposal-compartments/
</emu-table>
</emu-clause>

<emu-clause id="sec-meta-properties">
<h1>Meta Properties</h1>

<emu-clause id="sec-meta-properties-runtime-semantics-evaluation" type="sdo">
<h1>Runtime Semantics: Evaluation</h1>

<emu-grammar>ImportMeta : `import` `.` `meta`</emu-grammar>
<emu-alg>
1. Let _module_ be GetActiveScriptOrModule().
1. Assert: _module_ is a Source Text Module Record.
1. Let _importMeta_ be _module_.[[ImportMeta]].
1. If _importMeta_ is ~empty~, then
1. Set _importMeta_ to OrdinaryObjectCreate(*null*).
1. <ins>If _module_'s [[ModuleInstance]] internal slot value is *undefined*, then</ins>
1. Let _importMetaValues_ be HostGetImportMetaProperties(_module_).
1. For each Record { [[Key]], [[Value]] } _p_ of _importMetaValues_, do
1. Perform ! CreateDataPropertyOrThrow(_importMeta_, _p_.[[Key]], _p_.[[Value]]).
1. Perform HostFinalizeImportMeta(_importMeta_, _module_).
1. Set _module_.[[ImportMeta]] to _importMeta_.
1. Return _importMeta_.
1. <ins>Else,</ins>
1. <ins>Let _importMetaHook_ be _module_.[[ModuleInstance]].[[ImportMetaHook]].</ins>
1. <ins>Perform ? Call(_importMetaHook_, *undefined*, &laquo; _importMeta_ &raquo;).</ins>
1. <ins>Set _module_.[[ImportMeta]] to _importMeta_.</ins>
1. <ins>Return _importMeta_.</ins>
1. Else,
1. Assert: Type(_importMeta_) is Object.
1. Return _importMeta_.
</emu-alg>
</emu-clause>
</emu-clause>

<emu-clause id="sec-modulesource-records">
<h1>Module Source Records</h1>

Expand Down Expand Up @@ -537,27 +569,48 @@ location: https://tc39.es/proposal-compartments/
</ul>

<emu-clause id="sec-module">
<h1>Module ( _moduleSource_, _referral_, _importHook_ [, _importMeta_ ] )</h1>
<h1>Module ( _moduleSource_, _handler_ [, _importMeta_ ] )</h1>
<p>When the `Module` function is called with the arguments _moduleSource_, _referral_, _importHook_ and _importMeta_, the following steps are taken:</p>
<emu-alg>
1. If NewTarget is *undefined*, throw a *TypeError* exception.
1. If Type(_moduleSource_) is not Object, throw a *TypeError* exception.
1. If IsCallable(_importHook_) is *false*, throw a *TypeError* exception.
1. If _importMeta_ is *undefined*, then
1. Set _importMeta_ to *null*.
1. Else, if Type(_importMeta_) is not Object, then
1. Throw a *TypeError* exception.
_importHook_
1. Perform ? RequireInternalSlot(_moduleSource_, [[ModuleSource]]).
1. Let _O_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%Module.prototype%"*, « [[Module]], [[ModuleSourceInstance]], [[ImportHook]] »).
1. Let _moduleRecord_ to ! CreateModuleRecord(_moduleSource_.[[ModuleSource]]).
1. Set _moduleRecord_.[[ModuleInstance]] to _O_.
1. Set _moduleRecord_.[[ImportMeta]] to _importMeta_.
1. Let _importHookClosure_ be a new Abstract Closure with parameters (_specifier_) that captures _importHook_ and _referral_ and performs the following steps when called
1. Assert: Type(_specifier_) is String.
1. return ? Call(_importHook_, *undefined*, « _specifier_, _referral_ »).
1. Set _O_.[[Module]] to _moduleRecord_.
1. If Type(_handler_) is Object, then
1. If _handler_ is *null, throw a TypeError exception.
1. Let _importHook_ be ? GetMethod(handler, "importHook").
1. If _importHook_ is *undefined*, then
1. Let _importHookClosure_ be a new Abstract Closure with parameters (_specifier_) that captures _moduleRecord_ and performs the following steps when called
1. Assert: Type(_specifier_) is String.
1. NOTE: Default import hook.
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Perform HostImportModuleDynamically(_moduleRecord_, _specifier_, _promiseCapability_).
1. Return _promiseCapability_.[[Promise]].
1. Else,
1. Let _importHookClosure_ be a new Abstract Closure with parameters (_specifier_) that captures _importHook_ and _handler_ and performs the following steps when called
1. Assert: Type(_specifier_) is String.
1. return ? Call(_importHook_, _handler_, « _specifier_ »).
1. Let _importMetaHook_ be ? GetMethod(handler, "importMetaHook").
1. If _importMetaHook_ is *undefined*, then
1. Let _importHookClosure_ be a new Abstract Closure with parameters (_importMeta_) that captures _moduleRecord_ and performs the following steps when called
1. NOTE: Default import.meta hook.
1. Let _importMetaValues_ be HostGetImportMetaProperties(_moduleRecord_).
1. For each Record { [[Key]], [[Value]] } _p_ of _importMetaValues_, do
1. Perform ! CreateDataPropertyOrThrow(_importMeta_, _p_.[[Key]], _p_.[[Value]]).
1. Perform HostFinalizeImportMeta(_importMeta_, _moduleRecord_).
1. Set _moduleRecord_.[[ImportMeta]] to _importMeta_.
1. Return _importMeta_.
1. Else,
1. Let _importHookClosure_ be a new Abstract Closure with parameters (_importMeta_) that captures _importMetaHook_ and _handler_ and performs the following steps when called
1. return ? Call(_importMetaHook_, _handler_, &laquo; _importMeta_ &raquo;).
1. Let _O_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%Module.prototype%"*, « [[Module]], [[ModuleSourceInstance]], [[HooksThisValue]], [[ImportHook]], [[ImportMetaHook]] »).
1. Set _O_.[[ModuleSourceInstance]] to _moduleSource_.
1. Set _O_.[[ImportHook]] be CreateBuiltinFunction(_importHookClosure_, 1, *""*, &laquo; &raquo;).
1. Set _O_.[[ImportMetaHook]] be CreateBuiltinFunction(_importMetaHookClosure_, 0, *""*, &laquo; &raquo;).
1. Set _O_.[[HooksThisValue]] be _handler_.
1. Set _moduleRecord_.[[ModuleInstance]] to _O_.
1. Set _O_.[[Module]] to _moduleRecord_.
1. Return _O_.
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -632,11 +685,21 @@ location: https://tc39.es/proposal-compartments/
<td>ModuleSource Instance or *null*</td>
<td>The ModuleSource Instance associated to Module Instance. *null* if this Module Instance has not a source associated to it.</td>
</tr>
<tr>
<td>[[HooksThisValue]]</td>
<td>an ECMAScript language value</td>
<td>This is the *this* value used for invocation of the hook functions.</td>
</tr>
<tr>
<td>[[ImportHook]]</td>
<td>a callable Object</td>
<td>The callable Object associated to the [[Module]] record to resolve module dependencies.</td>
</tr>
<tr>
<td>[[ImportMetaHook]]</td>
<td>a callable Object</td>
<td>The callable Object associated to the [[Module]] record to provide import.meta value.</td>
</tr>
</tbody>
</table>
</emu-table>
Expand Down
Loading

0 comments on commit b7899d1

Please sign in to comment.