Skip to content

Commit 8e6ee1f

Browse files
committed
feat: phase imports support
1 parent 3e82df4 commit 8e6ee1f

File tree

1 file changed

+80
-53
lines changed

1 file changed

+80
-53
lines changed

document/js-api/index.bs

Lines changed: 80 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ urlPrefix: https://tc39.github.io/ecma262/; spec: ECMASCRIPT
110110
text: ToBigInt64; url: sec-tobigint64
111111
text: Module Namespace exotic object; url: sec-module-namespace-exotic-objects
112112
text: ResolvedBinding Record; url: resolvedbinding-record
113+
text: ModuleRequest Record; url: modulerequest-record
114+
text: ImportAttribute Record; url: importattribute-record
113115
type: abstract-op
114116
text: CreateDataPropertyOrThrow; url: sec-createdatapropertyorthrow
115117
text: CreateMethodProperty; url: sec-createmethodproperty
@@ -2316,11 +2318,12 @@ To <dfn export>parse a WebAssembly module</dfn> given a <a>byte sequence</a> |by
23162318
1. For each (|moduleName|, |name|, |type|) in [=module_imports=](|module|.\[[Module]]),
23172319
1. If |moduleName| starts with the prefix "wasm-js:",
23182320
1. Throw a {{LinkError}} exception.
2319-
1. If |name| starts with the prefix "wasm:" or "wasm-js:",
2321+
1. If |name| starts with the prefix "wasm:" or "wasm-js:", and |name| is not equal to "wasm-js:phase/source" or "wasm-js:phase/source",
23202322
1. Throw a {{LinkError}} exception.
23212323
1. Note: The following step only applies when integrating with the JS String Builtins proposal.
23222324
1. If [=Find a builtin=] with (|moduleName|, |name|, |type|) and builtins |module|.\[[BuiltinSets]] is not null, then [=iteration/continue=].
2323-
1. [=set/Append=] |moduleName| to |requestedModules|.
2325+
1. Let (|moduleRequest|, <var ignore>importName</var>) be the [=module request=] for |moduleName| and |name|.
2326+
1. [=set/Append=] |moduleRequest| to |requestedModules|.
23242327
1. For each (|name|, <var ignore>type</var>) in [=module_exports=](|module|.\[[Module]])
23252328
1. If |name| starts with the prefix "wasm:" or "wasm-js:",
23262329
1. Throw a {{LinkError}} exception.
@@ -2364,6 +2367,18 @@ The <dfn>export name list</dfn> of a WebAssembly Module Record |record| is defin
23642367

23652368
</div>
23662369

2370+
<div algorithm>
2371+
The <dfn>module request</dfn> for a string |moduleName| and a string |name| is defined by the following algorithm:
2372+
2373+
1. Let |attributes| be an empty [=set=].
2374+
1. Let |phase| be ~evaluation~.
2375+
1. If |name| is equal to "wasm-js:phase/source", set |phase| to ~source~.
2376+
1. If |name| is equal to "wasm-js:phase/defer", set |phase| to ~defer~.
2377+
1. Let |moduleRequest| be a new [=ModuleRequest Record=] { \[[Specifier]]: |name|, \[[Attributes]]: |attributes|, \[[Phase]]: _phase_ }.
2378+
1. Return (|moduleRequest|, |name|).
2379+
2380+
</div>
2381+
23672382
WebAssembly Module Records have the following methods:
23682383

23692384
<div algorithm=GetExportedNames>
@@ -2411,57 +2426,69 @@ WebAssembly Module Records have the following methods:
24112426
1. [=list/iterate|For each=] (|importedModuleName|, |name|, |importtype|) in [=module_imports=](|module|),
24122427
1. Note: The following step only applies when integrating with the JS String Builtins proposal.
24132428
1. If [=Find a builtin=] with (|importedModuleName|, |name|) and builtins |module|.\[[BuiltinSets]] is not null, then [=iteration/continue=].
2414-
1. Let |importedModule| be [$GetImportedModule$](|record|, |importedModuleName|).
2415-
1. Let |resolution| be |importedModule|.ResolveExport(|name|).
2416-
1. Assert: |resolution| is a [=ResolvedBinding Record=], as validated during environment initialization.
2417-
1. Let |resolvedModule| be |resolution|.\\[[Module]].
2418-
1. Let |resolvedName| be |resolution|.\[[BindingName]].
2419-
1. If |resolvedModule| is a WebAssembly Module Record,
2420-
1. If |resolvedModule|.\[[Instance]] is ~empty~, throw a {LinkError} exception.
2421-
1. Assert: |resolvedModule|.\[[Instance]] is a WebAssembly {{Instance}} object.
2422-
1. Assert: |resolvedModule|.\[[ModuleSource]] is a WebAssembly {{Module}} object.
2423-
1. Let |module| be |resolvedModule|.\[[ModuleSource]].\[[Module]].
2424-
1. Let |externval| be [=instance_export=](|resolvedModule|.\[[Instance]], |resolvedName|).
2425-
1. Assert: |externval| is not [=error=].
2426-
1. Assert: [=module_exports=](|module|) contains an element (|resolvedName|, <var ignore>type</var>).
2427-
1. Let |externtype| be the value of |type| for the element (|resolvedName|, |type|) in [=module_exports=](|module|).
2428-
1. If |importtype| is not an [=extern subtype=] of |externtype|, throw a {{LinkError}} exception.
2429-
1. [=list/Append=] |externval| to |imports|.
2430-
1. Otherwise,
2431-
1. Let |env| be |resolvedModule|.\[[Environment]].
2432-
1. Let |v| be [=?=] |env|.GetBindingValue(|resolvedName|, true).
2433-
1. If |importtype| is of the form [=func=] |functype|,
2434-
1. If [$IsCallable$](|v|) is false, throw a {{LinkError}} exception.
2435-
1. If |v| has a \[[FunctionAddress]] internal slot, and therefore is an [=Exported Function=],
2436-
1. Let |funcaddr| be the value of |v|'s \[[FunctionAddress]] internal slot.
2437-
1. Otherwise,
2438-
1. [=Create a host function=] from |v| and |functype|, and let |funcaddr| be the result.
2439-
1. Let <var ignore>index</var> be the number of external functions in |imports|, defining the [=index of the host function=] |funcaddr|.
2440-
1. Let |externfunc| be the [=external value=] [=external value|func=] |funcaddr|.
2441-
1. [=list/Append=] |externfunc| to |imports|.
2442-
1. If |importtype| is of the form [=global=] |mut| |valtype|,
2443-
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
2444-
1. If |v| [=implements=] {{Global}},
2445-
1. Let |globaladdr| be |v|.\[[Global]].
2446-
1. Let |targetmut| <var ignore>valuetype</var> be [=global_type=](|store|, |globaladdr|).
2447-
1. If |mut| is [=const=] and |targetmut| is [=var=], throw a {{LinkError}} exception.
2448-
1. Otherwise,
2449-
1. If |valtype| is [=v128=], throw a {{LinkError}} exception.
2450-
1. If |mut| is [=var=], throw a {{LinkError}} exception.
2451-
1. Let |value| be [=?=] [=ToWebAssemblyValue=](|v|, |valtype|).
2452-
1. Let (|store|, |globaladdr|) be [=global_alloc=](|store|, |mut| |valtype|, |value|).
2453-
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
2454-
1. Let |externglobal| be [=external value|global=] |globaladdr|.
2455-
1. [=list/Append=] |externglobal| to |imports|.
2456-
1. If |importtype| is of the form [=mem=] <var ignore>memtype</var>,
2457-
1. If |v| does not [=implement=] {{Memory}}, throw a {{LinkError}} exception.
2458-
1. Let |externmem| be the [=external value=] [=external value|mem=] |v|.\[[Memory]].
2459-
1. [=list/Append=] |externmem| to |imports|.
2460-
1. If |importtype| is of the form [=table=] <var ignore>tabletype</var>,
2461-
1. If |v| does not [=implement=] {{Table}}, throw a {{LinkError}} exception.
2462-
1. Let |tableaddr| be |v|.\[[Table]].
2463-
1. Let |externtable| be the [=external value=] [=external value|table=] |tableaddr|.
2464-
1. [=list/Append=] |externtable| to |imports|.
2429+
1. Let (|moduleRequest|, |importName|) be the [=module request=] for |moduleName| and |name|.
2430+
1. Let |importedModule| be [$GetImportedModule$](|record|, |moduleRequest|).
2431+
1. If |moduleRequest|.\[[Phase]] is ~defer~ or ~source~,
2432+
1. If |moduleRequest|.\[[Phase]] is ~defer~, let |phase| be GetModuleNamespace(|importedModule|, ~defer~).
2433+
1. Else, let |phase| be _importedModule_.[[ModuleSource]] throwing a *SyntextError* exception if ~empty~.
2434+
1. If |importtype| is not of the form [=global=] [=const=] |valtype|, throw a {{LinkError}} exception.
2435+
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
2436+
1. Let |value| be [=?=] [=ToWebAssemblyValue=](|v|, |valtype|).
2437+
1. Let (|store|, |globaladdr|) be [=global_alloc=](|store|, |mut| |valtype|, |value|).
2438+
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
2439+
1. Let |externglobal| be [=external value|global=] |globaladdr|.
2440+
1. [=list/Append=] |externglobal| to |imports|.
2441+
1. Else,
2442+
1. Let |resolution| be |importedModule|.ResolveExport(|importName|).
2443+
1. Assert: |resolution| is a [=ResolvedBinding Record=], as validated during environment initialization.
2444+
1. Let |resolvedModule| be |resolution|.\\[[Module]].
2445+
1. Let |resolvedName| be |resolution|.\[[BindingName]].
2446+
1. If |resolvedModule| is a WebAssembly Module Record,
2447+
1. If |resolvedModule|.\[[Instance]] is ~empty~, throw a {LinkError} exception.
2448+
1. Assert: |resolvedModule|.\[[Instance]] is a WebAssembly {{Instance}} object.
2449+
1. Assert: |resolvedModule|.\[[ModuleSource]] is a WebAssembly {{Module}} object.
2450+
1. Let |module| be |resolvedModule|.\[[ModuleSource]].\[[Module]].
2451+
1. Let |externval| be [=instance_export=](|resolvedModule|.\[[Instance]], |resolvedName|).
2452+
1. Assert: |externval| is not [=error=].
2453+
1. Assert: [=module_exports=](|module|) contains an element (|resolvedName|, <var ignore>type</var>).
2454+
1. Let |externtype| be the value of |type| for the element (|resolvedName|, |type|) in [=module_exports=](|module|).
2455+
1. If |importtype| is not an [=extern subtype=] of |externtype|, throw a {{LinkError}} exception.
2456+
1. [=list/Append=] |externval| to |imports|.
2457+
1. Otherwise,
2458+
1. Let |env| be |resolvedModule|.\[[Environment]].
2459+
1. Let |v| be [=?=] |env|.GetBindingValue(|resolvedName|, true).
2460+
1. If |importtype| is of the form [=func=] |functype|,
2461+
1. If [$IsCallable$](|v|) is false, throw a {{LinkError}} exception.
2462+
1. If |v| has a \[[FunctionAddress]] internal slot, and therefore is an [=Exported Function=],
2463+
1. Let |funcaddr| be the value of |v|'s \[[FunctionAddress]] internal slot.
2464+
1. Otherwise,
2465+
1. [=Create a host function=] from |v| and |functype|, and let |funcaddr| be the result.
2466+
1. Let <var ignore>index</var> be the number of external functions in |imports|, defining the [=index of the host function=] |funcaddr|.
2467+
1. Let |externfunc| be the [=external value=] [=external value|func=] |funcaddr|.
2468+
1. [=list/Append=] |externfunc| to |imports|.
2469+
1. If |importtype| is of the form [=global=] |mut| |valtype|,
2470+
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
2471+
1. If |v| [=implements=] {{Global}},
2472+
1. Let |globaladdr| be |v|.\[[Global]].
2473+
1. Let |targetmut| <var ignore>valuetype</var> be [=global_type=](|store|, |globaladdr|).
2474+
1. If |mut| is [=const=] and |targetmut| is [=var=], throw a {{LinkError}} exception.
2475+
1. Otherwise,
2476+
1. If |valtype| is [=v128=], throw a {{LinkError}} exception.
2477+
1. If |mut| is [=var=], throw a {{LinkError}} exception.
2478+
1. Let |value| be [=?=] [=ToWebAssemblyValue=](|v|, |valtype|).
2479+
1. Let (|store|, |globaladdr|) be [=global_alloc=](|store|, |mut| |valtype|, |value|).
2480+
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
2481+
1. Let |externglobal| be [=external value|global=] |globaladdr|.
2482+
1. [=list/Append=] |externglobal| to |imports|.
2483+
1. If |importtype| is of the form [=mem=] <var ignore>memtype</var>,
2484+
1. If |v| does not [=implement=] {{Memory}}, throw a {{LinkError}} exception.
2485+
1. Let |externmem| be the [=external value=] [=external value|mem=] |v|.\[[Memory]].
2486+
1. [=list/Append=] |externmem| to |imports|.
2487+
1. If |importtype| is of the form [=table=] <var ignore>tabletype</var>,
2488+
1. If |v| does not [=implement=] {{Table}}, throw a {{LinkError}} exception.
2489+
1. Let |tableaddr| be |v|.\[[Table]].
2490+
1. Let |externtable| be the [=external value=] [=external value|table=] |tableaddr|.
2491+
1. [=list/Append=] |externtable| to |imports|.
24652492
1. [=Instantiate the core of a WebAssembly module=] |module| with |imports|, and let |instance| be the result.
24662493
1. Set |record|.\[[Instance]] to |instance|.
24672494
1. [=list/iterate|For each=] (|name|, |externtype|) of [=module_exports=](|module|),

0 commit comments

Comments
 (0)