Skip to content

Commit 6440faa

Browse files
littledantakikawa
authored andcommitted
[WIP] WebAssembly JavaScript Module integration
For concreteness, this patch specifies how the WebAssembly JavaScript module integration proposal [1] could work in HTML. It is not yet ready to merge, as the proposal is still in a relatively early state. Note that this change depends on the ability for modules to block in the evaluation phase, to permit WebAssembly module instantiation to yield, as is necessary on some platforms where compilation work is performed during the first instantiation. Such an ability to yield is provided by the JavaScript top-level await proposal [2] and associated HTML integration patch whatwg#4352. [1] https://github.com/webassembly/esm-integration [2] https://github.com/tc39/proposal-top-level-await
1 parent 465a6b6 commit 6440faa

File tree

1 file changed

+154
-29
lines changed

1 file changed

+154
-29
lines changed

source

Lines changed: 154 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
24932493
<li><dfn data-x="body safely extract" data-x-href="https://fetch.spec.whatwg.org/#bodyinit-safely-extract">safely extracting a body</dfn></li>
24942494
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#process-response-end-of-body">processResponseConsumeBody</dfn></li>
24952495
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#fetch-processresponseendofbody">processResponseEndOfBody</dfn></li>
2496+
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#concept-body-consume-body">consume body</dfn></li>
24962497
<li>
24972498
<dfn data-x="concept-response"
24982499
data-x-href="https://fetch.spec.whatwg.org/#concept-response">response</dfn> and its
@@ -4186,6 +4187,19 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
41864187
<ul class="brief">
41874188
<li><dfn data-x-href="https://wicg.github.io/uuid/#dfn-generate-a-random-uuid">generating a random UUID</dfn></li>
41884189
</ul>
4190+
</dd>
4191+
4192+
<dt>WebAssembly JavaScript Module Integration</dt>
4193+
4194+
<dd>
4195+
<p>The following terms are defined in <cite>WebAssembly JavaScript Module Integration</cite>: <ref spec=WASMESM></p>
4196+
4197+
<ul class="brief">
4198+
<li><dfn data-x-href="https://webassembly.github.io/esm-integration/js-api/index.html#webassembly-module-record">WebAssembly Module Record</dfn></li>
4199+
<li><dfn data-x-href="https://webassembly.github.io/esm-integration/js-api/index.html#parse-a-webassembly-module">parse a WebAssembly module</dfn></li>
4200+
</ul>
4201+
</dd>
4202+
41894203
</dl>
41904204

41914205
<hr>
@@ -91620,6 +91634,9 @@ document.querySelector("button").addEventListener("click", bound);
9162091634
<li><p>a <span>Synthetic Module Record</span>, for <span data-x="CSS module script">CSS module
9162191635
scripts</span> and <span data-x="JSON module script">JSON module scripts</span></p></li>
9162291636

91637+
<li><p>a <span>WebAssembly Module Record</span>, for <span data-x="WebAssembly module
91638+
script">WebAssembly module scripts</span>;</p></li>
91639+
9162391640
<li><p>null, representing a parsing failure.</p></li>
9162491641
</ul>
9162591642
</dd>
@@ -91691,6 +91708,10 @@ document.querySelector("button").addEventListener("click", bound);
9169191708
its <span data-x="concept-script-record">record</span> is a <span>Source Text Module
9169291709
Record</span>.</p></li>
9169391710

91711+
<li><p>A <span>module script</span> is a <dfn data-export="">WebAssembly module script</dfn> if
91712+
its <span data-x="concept-script-record">record</span> is a <span>WebAssembly Module
91713+
Record</span>.</p></li>
91714+
9169491715
<li>
9169591716
<p>A <span>module script</span> is a <dfn data-export="">CSS module script</dfn> if its <span
9169691717
data-x="concept-script-record">record</span> is a <span>Synthetic Module Record</span>, and it
@@ -92705,6 +92726,22 @@ document.querySelector("button").addEventListener("click", bound);
9270592726
<span>creating a JSON module script</span> given <var>source text</var> and <var>module map
9270692727
settings object</var>.</p></li>
9270792728

92729+
<li><p>If the <span>MIME type essence</span> of <var>MIME type</var> is
92730+
"<code>application/wasm</code>" and <var>module type</var> is "<code
92731+
data-x="">javascript</code>", then:</p>
92732+
<ol>
92733+
<li><p>Let <var>bufferPromise</var> be the result of running <span>consume body</span> on
92734+
<var>response</var> with <i>ArrayBuffer</i>.</p></li>
92735+
92736+
<li><p><span data-x="creating a WebAssembly module script">Create a WebAssembly module
92737+
script</span> given <var>bufferPromise</var>, <var>module map settings object</var>,
92738+
<var>response</var>'s <span data-x="concept-response-url">url</span>, and <var>options</var>.
92739+
Wait until the algorithm asynchronously completes with <var>result</var>.</p></li>
92740+
92741+
<li><p>Set <var>module script</var> to <var>result</var>.</p></li>
92742+
</ol>
92743+
</li>
92744+
9270892745
<li>
9270992746
<p><span data-x="map set">Set</span> <var>moduleMap</var>[(<var>url</var>,
9271092747
<var>moduleType</var>)] to <var>module script</var>, and asynchronously complete this algorithm
@@ -92890,36 +92927,12 @@ document.querySelector("button").addEventListener("click", bound);
9289092927
we only asked for "<code data-x="">type</code>" assertions in
9289192928
<span>HostGetSupportedImportAssertions</span>.</p></li>
9289292929

92893-
<li id="validate-requested-module-specifiers">
92894-
<p><span data-x="list iterate">For each</span> <span>ModuleRequest record</span>
92895-
<var>requested</var> of <var>result</var>.[[RequestedModules]]:</p>
92896-
92897-
<ol>
92898-
<li><p>Let <var>url</var> be the result of <span data-x="resolve a module specifier">resolving
92899-
a module specifier</span> given <var>script</var>'s <span data-x="concept-script-base-url">base
92900-
URL</span> and <var>requested</var>.[[Specifier]].</p></li>
92901-
92902-
<li><p>Let <var>moduleType</var> be the result of running the <span>module type from module
92903-
request</span> steps given <var>requested</var>.</p></li>
92930+
<li><p>Run the <span>validate requested module specifiers</span> steps given <var>result</var>
92931+
and <var>script</var>.</p>
9290492932

92905-
<li>
92906-
<p>If <var>url</var> is failure, or if the result of running the <span>module type
92907-
allowed</span> steps given <var>moduleType</var> and <var>settings</var> is false, then:</p>
92908-
<ol>
92909-
<li><p>Let <var>error</var> be a new <code>TypeError</code> exception.</p></li>
92910-
92911-
<li><p>Set <var>script</var>'s <span data-x="concept-script-parse-error">parse error</span>
92912-
to <var>error</var>.</p></li>
92913-
92914-
<li><p>Return <var>script</var>.</p></li>
92915-
</ol>
92916-
</li>
92917-
</ol>
92918-
92919-
<p class="note">This step is essentially validating all of the requested module specifiers and
92920-
type assertions. We treat a module with unresolvable module specifiers or unsupported type
92921-
assertions the same as one that cannot be parsed; in both cases, a syntactic issue makes it
92922-
impossible to ever contemplate linking the module later.</p>
92933+
<p>If these steps throw an exception, set <var>script</var>'s <span
92934+
data-x="concept-script-parse-error">parse error</span> to that exception, and return
92935+
<var>script</var>.</p>
9292392936
</li>
9292492937

9292592938
<li><p>Set <var>script</var>'s <span data-x="concept-script-record">record</span> to
@@ -92996,6 +93009,109 @@ document.querySelector("button").addEventListener("click", bound);
9299693009
<li><p>Return <var>script</var>.</p></li>
9299793010
</ol>
9299893011

93012+
<p>To <dfn data-x="creating a WebAssembly module script">create a WebAssembly module script</dfn>,
93013+
given a promise that will resolve with an <code data-x="idl-ArrayBuffer">ArrayBuffer</code>
93014+
<var>bufferPromise</var>, an <span>environment settings object</span> <var>settings</var>, a
93015+
<span>URL</span> <var>baseURL</var>, and some <span>script fetch options</span>
93016+
<var>options</var>, run these steps. The algorithm will asynchronously complete with a new
93017+
<span>WebAssembly module script</span>.</p>
93018+
93019+
<ol>
93020+
<li><p>If <span data-x="concept-environment-noscript">scripting is disabled</span> for
93021+
<var>settings</var>, then set <var>bufferPromise</var> to a promise resolved with an empty
93022+
<code data-x="idl-ArrayBuffer">ArrayBuffer</code>.</p></li>
93023+
<!-- REVIEW NOTE: this will cause a parse error, because it doesn't have the magic bytes. -->
93024+
93025+
<li><p>Let <var>script</var> be a new <span>WebAssembly module script</span> that this algorithm
93026+
will subsequently initialize.</p></li>
93027+
93028+
<li><p>Set <var>script</var>'s <span>settings object</span> to <var>settings</var>.</p></li>
93029+
93030+
<li><p>Set <var>script</var>'s <span data-x="concept-script-base-url">base URL</span> to
93031+
<var>baseURL</var>.</p></li>
93032+
93033+
<li><p>Set <var>script</var>'s <span data-x="concept-script-script-fetch-options">fetch
93034+
options</span> to <var>options</var>.</p></li>
93035+
93036+
<li><p>Set <var>script</var>'s <span data-x="concept-script-parse-error">parse error</span> and
93037+
<span data-x="concept-script-error-to-rethrow">error to rethrow</span> to null.</p></li>
93038+
93039+
<li>
93040+
<p>Upon fulfillment of <var>bufferPromise</var> with <var>buffer</var>:</p>
93041+
93042+
<ol>
93043+
<li>
93044+
<p>Let <var>module</var> be the result of <span>parse a WebAssembly module</span> given
93045+
<var>buffer</var>, <var>settings</var>'s <span data-x="environment settings object's
93046+
Realm">Realm</span>, and <var>script</var>.</p>
93047+
93048+
<p class="note">Passing <var>script</var> as the last parameter here ensures
93049+
<var>result</var>.[[HostDefined]] will be <var>script</var>.</p>
93050+
93051+
<p>If this algorithm throws an exception, set <var>script</var>'s <span
93052+
data-x="concept-script-parse-error">parse error</span> to that exception, and asynchronously
93053+
complete this algorithm with <var>script</var>.</p>
93054+
</li>
93055+
93056+
<li><p>Run the <span>validate requested module specifiers</span> steps given <var>module</var>
93057+
and <var>script</var>.</p>
93058+
93059+
</li>
93060+
93061+
<li><p>Set <var>script</var>'s <span data-x="concept-script-record">record</span> to
93062+
<var>module</var>.</p></li>
93063+
93064+
<li><p>Asynchronously complete this algorithm with <var>script</var>.</p></li>
93065+
</ol>
93066+
</li>
93067+
93068+
<li>
93069+
<p>Upon rejection of <var>bufferPromise</var> with <var>reason</var>:</p>
93070+
<ol>
93071+
<li><p>Set <var>script</var>'s <span data-x="concept-script-parse-error">parse error</span> to
93072+
<var>reason</var>.</p></li>
93073+
93074+
<li><p>Asynchronously complete this algorithm with <var>script</var>.</p></li>
93075+
</ol>
93076+
</li>
93077+
</ol>
93078+
93079+
<p>The <dfn id="validate-requested-module-specifiers">validate requested module specifiers</dfn>
93080+
steps, given a module record <var>record</var> and a script <var>script</var>, are as
93081+
follows:</p>
93082+
93083+
<ol>
93084+
<li>
93085+
<p><span data-x="list iterate">For each</span> <span>ModuleRequest record</span>
93086+
<var>requested</var> of <var>record</var>.[[RequestedModules]]:</p>
93087+
93088+
<ol>
93089+
<li><p>Let <var>url</var> be the result of <span data-x="resolve a module specifier">resolving
93090+
a module specifier</span> given <var>script</var>'s <span data-x="concept-script-base-url">base
93091+
URL</span> and <var>requested</var>.[[Specifier]].</p></li>
93092+
93093+
<li><p>Let <var>moduleType</var> be the result of running the <span>module type from module
93094+
request</span> steps given <var>requested</var>.</p></li>
93095+
93096+
<li>
93097+
<p>If <var>url</var> is failure, or if the result of running the <span>module type
93098+
allowed</span> steps given <var>moduleType</var> and <var>settings</var> is false, then:</p>
93099+
93100+
<ol>
93101+
<li><p>Let <var>error</var> be a new <code>TypeError</code> exception.</p></li>
93102+
93103+
<li><p>Throw <var>error</var>.</p></li>
93104+
</ol>
93105+
</li>
93106+
</ol>
93107+
</li>
93108+
</ol>
93109+
93110+
<p class="note">These steps are essentially validating all of the requested module specifiers and
93111+
type assertions. We treat a module with unresolvable module specifiers or unsupported type
93112+
assertions the same as one that cannot be parsed; in both cases, a syntactic issue makes it
93113+
impossible to ever contemplate linking the module later.</p>
93114+
9299993115
<p>The <dfn>module type from module request</dfn> steps, given a <span>ModuleRequest
9300093116
Record</span> <var>moduleRequest</var>, are as follows:</p>
9300193117

@@ -126668,6 +126784,9 @@ INSERT INTERFACES HERE
126668126784
<dt><dfn><code>application/xml</code></dfn></dt>
126669126785
<dd>XML <ref spec=XML> <ref spec=RFC7303></dd>
126670126786

126787+
<dt><dfn><code>application/wasm</code></dfn></dt>
126788+
<dd>WebAssembly <ref spec=WASM></dd>
126789+
126671126790
<dt><dfn><code>image/gif</code></dfn></dt>
126672126791
<dd>GIF images <ref spec=GIF></dd>
126673126792

@@ -127314,6 +127433,12 @@ INSERT INTERFACES HERE
127314127433
<dt id="refsWASMJS">[WASMJS]</dt>
127315127434
<dd>(Non-normative) <cite><a href="https://webassembly.github.io/spec/js-api/">WebAssembly JavaScript Interface</a></cite>, D. Ehrenberg. W3C.</dd>
127316127435

127436+
<dt id="refsWASM">[WASM]</dt>
127437+
<dd><cite><a href="https://webassembly.github.io/spec/core/bikeshed/index.html">WebAssembly Core Specification</a></cite>, A. Rossberg. W3C.</dd>
127438+
127439+
<dt id="refsWASMESM">[WASMESM]</dt>
127440+
<dd><cite><a href="https://webassembly.github.io/esm-integration/js-api/index.html">WebAssembly JavaScript Module Integration</a></cite>, L. Clark, D. Ehrenberg. W3C.</dd>
127441+
127317127442
<dt id="refsWCAG">[WCAG]</dt>
127318127443
<dd>(Non-normative) <cite><a href="https://w3c.github.io/wcag/guidelines/">Web Content Accessibility Guidelines (WCAG)</a></cite>, A. Kirkpatrick, J. O Connor, A. Campbell, M. Cooper. W3C.</dd>
127319127444

0 commit comments

Comments
 (0)