Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESM Phase Imports import() & new Worker() support for WA.Module #106

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions document/js-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ In addition, the interface object for {{Module}} must have as its \[[Prototype]]
1. If |module| is [=error=], throw a {{CompileError}} exception.
1. Set **this**.\[[Module]] to |module|.
1. Set **this**.\[[Bytes]] to |stableBytes|.
1. Set **this**.\[[ModuleRecord]] to null.

Note: Some implementations enforce a size limitation on |bytes|. Use of this API is discouraged, in favor of asynchronous APIs.
</div>
Expand Down Expand Up @@ -1381,21 +1382,18 @@ Note: While this specification is not yet merged into the main Wasm specificatio
<dfn export>WebAssembly Module Record</dfn>s are a subclass of [=Cyclic Module Record=] which contain WebAssembly code.

<div algorithm>
To <dfn export>parse a WebAssembly module</dfn> given a <a>byte sequence</a> |bytes|, a Realm |realm| and object |hostDefined|, perform the following steps.
To <dfn export>create a WebAssembly Module Record</dfn> given a WebAssembly module object |moduleObject|, a Realm |realm| and object |hostDefined|, perform the following steps.

1. Let |stableBytes| be a [=get a copy of the buffer source|copy of the bytes held by the buffer=] |bytes|.
1. [=Compile a WebAssembly module|Compile the WebAssembly module=] |stableBytes| and store the result as |module|.
1. If |module| is [=error=], throw a {{CompileError}} exception.
1. [=Construct a WebAssembly module object=] from |module| and |bytes|, and let |module| be the result.
1. Assert: |moduleObject|.\[[ModuleRecord]] is null.
1. Let |requestedModules| be a set.
1. For each (|moduleName|, <var ignore>name</var>, <var ignore>type</var>) in [=module_imports=](|module|.\[[Module]]),
1. For each (|moduleName|, <var ignore>name</var>, <var ignore>type</var>) in [=module_imports=](|moduleObject|.\[[Module]]),
1. [=set/Append=] |moduleName| to |requestedModules|.
1. Let |moduleRecord| be {
<!-- Abstract Module Records -->
\[[Realm]]: |realm|,
\[[Environment]]: ~empty~,
\[[Namespace]]: ~empty~,
\[[ModuleSource]]: |module|,
\[[ModuleSource]]: |moduleObject|,
\[[HostDefined]]: |hostDefined|,
<!-- Cyclic Module Records -->
\[[Status]]: "new",
Expand All @@ -1411,10 +1409,10 @@ To <dfn export>parse a WebAssembly module</dfn> given a <a>byte sequence</a> |by
\[[AsyncParentModules]]: &laquo; &raquo;,
\[[PendingAsyncDependencies]]: ~empty~,
}.
1. Set |module|.\[[ModuleRecord]] to |moduleRecord|.
1. Set |moduleObject|.\[[ModuleRecord]] to |moduleRecord|.
1. Return |moduleRecord|.

Note: From HTML, it's not observable when [=parse a WebAssembly module=] begins, so any work perfomed in compilation may be performed off-thread.
Note: From HTML, it's not observable when [=create a WebAssembly Module Record=] begins, so any work perfomed in compilation may be performed off-thread.
</div>

<div algorithm>
Expand Down Expand Up @@ -1447,6 +1445,17 @@ WebAssembly Module Records have the following methods:

</div>

<div algorithm=ModuleSourcesEqual>

<h3 id="module-sources-equal">ModuleSourcesEqual ( |otherRecord| ) Concrete Method</h3>
1. If |otherRecord| is not a WebAssemly Module Record, return false.
1. Let |record| be this WebAssembly Module Record.
1. Let |module| be |record|.\[[ModuleSource]].
1. Let |otherModule| be |otherRecord|.\[[ModuleSource]].
1. Return true if |module|.\[[Bytes]] is equal to |otherModule|.\[[Bytes]], and false otherwise.

</div>

<div algorithm=GetModuleSourceKind>

<h3 id="get-module-source-kind">GetModuleSourceKind ( ) Concrete Method</h3>
Expand Down
45 changes: 42 additions & 3 deletions document/web-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ urlPrefix: https://webassembly.github.io/spec/js-api/; spec: WASMJS
text: asynchronously compile a webassembly module; url: #asynchronously-compile-a-webassembly-module
text: instantiate a promise of a module; url: #instantiate-a-promise-of-a-module
text: Exported Function; url: #exported-function
url:https://html.spec.whatwg.org/#cors-same-origin;text:CORS-same-origin;type:dfn;spec:HTML
urlPrefix: https://html.spec.whatwg.org/; spec: HTML; type: dfn
text: rooted source; url: #concept-script-rooted-source
text: default script fetch options; url: #default-script-fetch-options
text: current settings object; url: #current-settings-object
text: CORS-same-origin; url: #cors-same-origin
text: create a WebAssembly Module Script; url: #creating-a-webassembly-module-script
url:https://fetch.spec.whatwg.org/#concept-body-consume-body;text:consume body;type:dfn;spec:FETCH
</pre>

Expand Down Expand Up @@ -148,15 +153,49 @@ The [=serialization steps=], given |value|, |serialized|, and |forStorage|, are:

1. Set |serialized|.\[[AgentCluster]] to the [=current Realm=]'s corresponding [=agent cluster=].

1. Let |rooted| be false.

1. If |value|.\[[ModuleRecord]] is not null and |value|.\[[ModuleRecord]].\[[HostDefined]] is not empty:

1. Let |moduleScript| be |value|.\[[ModuleRecord]].\[[HostDefined]].

1. Set |rooted| to |moduleScript|'s [=rooted source=] boolean.

1. If |rooted| is true then,

1. Set |serialized|.\[[Rooted]] to true.

1. Set |serialized|.\[[BaseURL]] to |moduleScript|'s [=base URL=].

1. If |rooted| is false:

1. Set |serialized|.\[[Rooted]] to false.

1. Set |serialized|.\[[BaseURL]] to null.

The [=deserialization steps=], given |serialized|, |value|, and |targetRealm| are:

1. If |targetRealm|'s corresponding [=agent cluster=] is not |serialized|.\[[AgentCluster]], then throw a "<a exception>DataCloneError</a>" {{DOMException}}.

1. Let |bytes| be the [=sub-deserialization=] of |serialized|.\[[Bytes]].

1. Set |value|.\[[Bytes]] to |bytes|.

1. [=Compile a WebAssembly module=] from |bytes| and set |value|.\[[Module]] to the result.

1. If |targetRealm|'s corresponding [=agent cluster=] is not |serialized|.\[[AgentCluster]], then throw a "<a exception>DataCloneError</a>" {{DOMException}}.
1. Assert: there was no compile error as this same module was previously compiled successfully.

1. [=Compile a WebAssembly module=] from |bytes| and set |value|.\[[Module]] to the result.
1. Let |rooted| be the [=sub-deserialization=] of |serialized|.\[[Rooted]].

1. If |rooted| is true:

1. Let |baseURL| be the [=sub-deserialization=] of |serialized|.\[[BaseURL]].

1. Let |settings| be the [=current settings object=].

1. Let |fetchOptions| be the [=default script fetch options=].

1. [=Create a WebAssembly module script=] given |value|.\[[Module]], |settings|, |baseURL|, |fetchOptions|, and true.

Engines should attempt to share/reuse internal compiled code when performing
a structured serialization, although in corner cases like CPU upgrade or browser
Expand Down