From eb39f80a2bd51ae1939254d5bc554c895b2cb3c0 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Sat, 16 Feb 2019 17:50:15 +0100 Subject: [PATCH] [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 #4352. [1] https://github.com/webassembly/esm-integration [2] https://github.com/tc39/proposal-top-level-await --- source | 183 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 154 insertions(+), 29 deletions(-) diff --git a/source b/source index 53a123e70a7..398ad1ffafd 100644 --- a/source +++ b/source @@ -2493,6 +2493,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
  • safely extracting a body
  • processResponseConsumeBody
  • processResponseEndOfBody
  • +
  • consume body
  • response and its @@ -4186,6 +4187,19 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute + + +
    WebAssembly JavaScript Module Integration
    + +
    +

    The following terms are defined in WebAssembly JavaScript Module Integration:

    + +
      +
    • WebAssembly Module Record
    • +
    • parse a WebAssembly module
    • +
    +
    +
    @@ -91620,6 +91634,9 @@ document.querySelector("button").addEventListener("click", bound);
  • a Synthetic Module Record, for CSS module scripts and JSON module scripts

  • +
  • a WebAssembly Module Record, for WebAssembly module scripts;

  • +
  • null, representing a parsing failure.

  • @@ -91691,6 +91708,10 @@ document.querySelector("button").addEventListener("click", bound); its record is a Source Text Module Record.

    +
  • A module script is a WebAssembly module script if + its record is a WebAssembly Module + Record.

  • +
  • A module script is a CSS module script if its record is a Synthetic Module Record, and it @@ -92705,6 +92726,22 @@ document.querySelector("button").addEventListener("click", bound); creating a JSON module script given source text and module map settings object.

  • +
  • If the MIME type essence of MIME type is + "application/wasm" and module type is "javascript", then:

    +
      +
    1. Let bufferPromise be the result of running consume body on + response with ArrayBuffer.

    2. + +
    3. Create a WebAssembly module + script given bufferPromise, module map settings object, + response's url, and options. + Wait until the algorithm asynchronously completes with result.

    4. + +
    5. Set module script to result.

    6. +
    +
  • +
  • Set moduleMap[(url, moduleType)] to module script, and asynchronously complete this algorithm @@ -92890,36 +92927,12 @@ document.querySelector("button").addEventListener("click", bound); we only asked for "type" assertions in HostGetSupportedImportAssertions.

  • -
  • -

    For each ModuleRequest record - requested of result.[[RequestedModules]]:

    - -
      -
    1. Let url be the result of resolving - a module specifier given script's base - URL and requested.[[Specifier]].

    2. - -
    3. Let moduleType be the result of running the module type from module - request steps given requested.

    4. +
    5. Run the validate requested module specifiers steps given result + and script.

      -
    6. -

      If url is failure, or if the result of running the module type - allowed steps given moduleType and settings is false, then:

      -
        -
      1. Let error be a new TypeError exception.

      2. - -
      3. Set script's parse error - to error.

      4. - -
      5. Return script.

      6. -
      -
    7. -
    - -

    This step is essentially validating all of the requested module specifiers and - type assertions. We treat a module with unresolvable module specifiers or unsupported type - assertions the same as one that cannot be parsed; in both cases, a syntactic issue makes it - impossible to ever contemplate linking the module later.

    +

    If these steps throw an exception, set script's parse error to that exception, and return + script.

  • Set script's record to @@ -92996,6 +93009,109 @@ document.querySelector("button").addEventListener("click", bound);

  • Return script.

  • +

    To create a WebAssembly module script, + given a promise that will resolve with an ArrayBuffer + bufferPromise, an environment settings object settings, a + URL baseURL, and some script fetch options + options, run these steps. The algorithm will asynchronously complete with a new + WebAssembly module script.

    + +
      +
    1. If scripting is disabled for + settings, then set bufferPromise to a promise resolved with an empty + ArrayBuffer.

    2. + + +
    3. Let script be a new WebAssembly module script that this algorithm + will subsequently initialize.

    4. + +
    5. Set script's settings object to settings.

    6. + +
    7. Set script's base URL to + baseURL.

    8. + +
    9. Set script's fetch + options to options.

    10. + +
    11. Set script's parse error and + error to rethrow to null.

    12. + +
    13. +

      Upon fulfillment of bufferPromise with buffer:

      + +
        +
      1. +

        Let module be the result of parse a WebAssembly module given + buffer, settings's Realm, and script.

        + +

        Passing script as the last parameter here ensures + result.[[HostDefined]] will be script.

        + +

        If this algorithm throws an exception, set script's parse error to that exception, and asynchronously + complete this algorithm with script.

        +
      2. + +
      3. Run the validate requested module specifiers steps given module + and script.

        + +
      4. + +
      5. Set script's record to + module.

      6. + +
      7. Asynchronously complete this algorithm with script.

      8. +
      +
    14. + +
    15. +

      Upon rejection of bufferPromise with reason:

      +
        +
      1. Set script's parse error to + reason.

      2. + +
      3. Asynchronously complete this algorithm with script.

      4. +
      +
    16. +
    + +

    The validate requested module specifiers + steps, given a module record record and a script script, are as + follows:

    + +
      +
    1. +

      For each ModuleRequest record + requested of record.[[RequestedModules]]:

      + +
        +
      1. Let url be the result of resolving + a module specifier given script's base + URL and requested.[[Specifier]].

      2. + +
      3. Let moduleType be the result of running the module type from module + request steps given requested.

      4. + +
      5. +

        If url is failure, or if the result of running the module type + allowed steps given moduleType and settings is false, then:

        + +
          +
        1. Let error be a new TypeError exception.

        2. + +
        3. Throw error.

        4. +
        +
      6. +
      +
    2. +
    + +

    These steps are essentially validating all of the requested module specifiers and + type assertions. We treat a module with unresolvable module specifiers or unsupported type + assertions the same as one that cannot be parsed; in both cases, a syntactic issue makes it + impossible to ever contemplate linking the module later.

    +

    The module type from module request steps, given a ModuleRequest Record moduleRequest, are as follows:

    @@ -126668,6 +126784,9 @@ INSERT INTERFACES HERE
    application/xml
    XML
    +
    application/wasm
    +
    WebAssembly
    +
    image/gif
    GIF images
    @@ -127314,6 +127433,12 @@ INSERT INTERFACES HERE
    [WASMJS]
    (Non-normative) WebAssembly JavaScript Interface, D. Ehrenberg. W3C.
    +
    [WASM]
    +
    WebAssembly Core Specification, A. Rossberg. W3C.
    + +
    [WASMESM]
    +
    WebAssembly JavaScript Module Integration, L. Clark, D. Ehrenberg. W3C.
    +
    [WCAG]
    (Non-normative) Web Content Accessibility Guidelines (WCAG), A. Kirkpatrick, J. O Connor, A. Campbell, M. Cooper. W3C.