From bdae4df8d3bf8d2ec1b199b5b6a08ef371146f26 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 | 188 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 148 insertions(+), 40 deletions(-) diff --git a/source b/source index 697f6616357..646795ff551 100644 --- a/source +++ b/source @@ -2751,6 +2751,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute JavaScript MIME type essence match
  • JSON MIME type
  • XML MIME type
  • +
  • WebAssembly MIME type
  • @@ -2783,6 +2784,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
  • the RequestCredentials enumeration
  • the RequestDestination enumeration
  • the fetch() method
  • +
  • consume body
  • response and its @@ -4180,6 +4182,17 @@ 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
    • +
    +
    +
    @@ -86768,7 +86781,7 @@ interface ApplicationCache : EventTarget {
    Definitions
    -

    A script is one of two possible A script is one of three possible structs. All scripts have:

    @@ -86781,8 +86794,10 @@ interface ApplicationCache : EventTarget {

    Either a Script Record, for classic scripts; a Source Text Module Record, for module scripts; or null. In the former two cases, it represents a parsed script; - null represents a failure parsing.

    + script">module scripts; a WebAssembly Module Record for WebAssembly module scripts; or null. In the + former two cases, it represents a parsed script; in the third case, a parsed WebAssembly + module; null represents a failure parsing.

    A parse error
    @@ -86834,6 +86849,10 @@ interface ApplicationCache : EventTarget { data-x="concept-script">script. It has no additional items.

    +

    A WebAssembly module script is another type of script. It has no additional items.

    +

    The active script is determined by the following algorithm:

      @@ -87472,25 +87491,48 @@ interface ApplicationCache : EventTarget {
    1. response's status is not an ok status

    2. + + -
    3. -

      The result of extracting a MIME type from - response's header list is not a - JavaScript MIME type

      +
    4. +

      Let type be the result of extracting a + MIME type from response's header + list.

      -

      For historical reasons, fetching a - classic script does not include MIME type checking. In contrast, module scripts will - fail to load if they are not of a correct MIME type.

      -
    5. - +

      For historical reasons, fetching a + classic script does not include MIME type checking. In contrast, module scripts' + interpretation is driven by their MIME type, and they will fail to load if they are not of + a supported MIME type.

      -
    6. Let source text be the result of UTF-8 - decoding response's body.

    7. +
    8. Let module script be null.

    9. -
    10. Let module script be the result of creating a module script given - source text, module map settings object, response's url, and options.

    11. +
    12. +

      If type is a JavaScript MIME type, then:

      + +
        +
      1. Let source text be the result of UTF-8 + decoding response's body.

      2. + +
      3. Set module script to the result of creating a module script given + source text, module map settings object, response's url, and options.

      4. +
      +
    13. + +
    14. +

      If type is a WebAssembly MIME type, then:

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

      2. + +
      3. Set module script to the result of creating a WebAssembly module + script given buffer, module map settings object, + response's url, and + options.

      4. +
      +
    15. Set moduleMap[url] to module @@ -87737,6 +87779,44 @@ interface ApplicationCache : EventTarget {

    16. Return script.

    +

    To validate requested module specifiers + of a module record record for a script script:

    + +
      +
    1. +

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

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

      2. + +
      3. +

        If url is failure, then:

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

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

        4. + +
        5. Return.

        6. +
        +
      4. +
      + +
    2. + +
    3. Set script's record to + result.

    4. +
    + +

    This algorithm is essentially validating all of the requested module specifiers. We + treat a module with unresolvable module specifiers the same as one that cannot be parsed; in + both cases, a syntactic issue makes it impossible to ever contemplate instantiating the module + later.

    +

    To create a module script, given a JavaScript string source, an environment settings object settings, a URL baseURL, and some script fetch @@ -87781,37 +87861,56 @@ interface ApplicationCache : EventTarget {

  • -
  • -

    For each string requested of - result.[[RequestedModules]]:

    +
  • Validate requested module specifiers of result with + script.

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

    2. +
    3. Return script.

    4. +
    -
  • -

    If url is failure, then:

    +

    To create a WebAssembly module script, + given an ArrayBuffer buffer, an + environment settings object settings, a URL baseURL, + and some script fetch options options:

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

    2. +
        +
      1. If scripting is disabled for + settings's responsible browsing context, then set source to + the empty string.

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

      4. +
      5. Let script be a new WebAssembly module script that this algorithm + will subsequently initialize.

      6. -
      7. Return script.

      8. -
      - -
    +
  • Set script's settings object to settings.

  • + +
  • Set script's base URL to + baseURL.

  • + +
  • Set script's fetch + options to options.

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

  • -

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

    +
  • +

    Let module be the result of parse a WebAssembly module given the + ArrayBuffer 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, then catch it and perform the following steps:

    + +
      +
    1. Set script's parse error to + the exception.

    2. + +
    3. Return script.

    4. +
  • -
  • Set script's record to - result.

  • +
  • Validate requested module specifiers of module with + script.

  • Return script.

  • @@ -121512,6 +121611,9 @@ INSERT INTERFACES HERE
    application/xml
    XML
    +
    application/wasm
    +
    WebAssembly
    +
    image/gif
    GIF images
    @@ -122108,6 +122210,12 @@ INSERT INTERFACES HERE
    [UTR36]
    (Non-normative) UTR #36: Unicode Security Considerations, M. Davis, M. Suignard. Unicode Consortium.
    +
    [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) 2.0, B. Caldwell, M. Cooper, L. Reid, G. Vanderheiden. W3C.