From d76829fe0f162b66ee87c4dc3c9ee147384371d6 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Sat, 16 Feb 2019 17:50:15 +0100 Subject: [PATCH] [WIP] JSON module support This patch provides JSON modules as a single default export, with parse errors checked before instantiating the module graph. Note, editorially, it's unclear whether JSON modules should be considered a type of "module script", with a settings object, fetch options, base URL, etc or not. This patch considers them "module scripts", but leaves those record fields unset (as they are unused). This patch is based on https://github.com/tc39/proposal-javascript-standard-library/pull/44 which hasn't landed yet, so the references are a bit awkward, and this patch should not land until that one does. Closes #4315 --- source | 89 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/source b/source index 413dea01e24..c87427977b2 100644 --- a/source +++ b/source @@ -2778,6 +2778,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 @@ -3116,6 +3117,15 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute

    User agents that support JavaScript must also implement the BigInt proposal.

    + +

    User agents that support JavaScript must also implement the JavaScript standard library proposal. + The following terms are defined there, and used in this specification:

    + @@ -86655,7 +86665,7 @@ interface ApplicationCache : EventTarget {
    Definitions
    -

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

    @@ -86668,7 +86678,9 @@ 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; + script">module scripts; a Synthetic Module Record for JSON module scripts; or null. In the + former two cases, it represents a parsed script; in the third case, a parsed JSON document; null represents a failure parsing.

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

    +

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

    +

    The active script is determined by the following algorithm:

      @@ -87359,25 +87375,63 @@ 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 JSON MIME type, then:

      + +
        +
      1. Set module script to a new JSON module script that this algorithm + will subsequently initialize.

      2. + +
      3. +

        Let json be the result of running consume body on + response with JSON.

        + +

        If this throws an exception, catch it, and set module script's + parse error to that exception.

        +
      4. + +
      5. +

        If no exception was thrown, set module script's record to CreateSyntheticModule + (« "default" », the following steps, module map settings object's + Realm, json) with the + following steps given module as an argument:

        +
          +
        1. 1. SetSyntheticModuleExport(module, "default", + module.[[HostDefined]]).
        2. +
        +
      6. +
      +
    15. Set moduleMap[url] to module @@ -121954,6 +122008,9 @@ INSERT INTERFACES HERE

      [JSINTL]
      ECMAScript Internationalization API Specification. Ecma International.
      +
      [JSSTDLIB]
      +
      JavaScript Standard Library. Ecma International.
      +
      [JSON]
      The JavaScript Object Notation (JSON) Data Interchange Format, T. Bray. IETF.