Skip to content

Commit

Permalink
[WIP] JSON module support
Browse files Browse the repository at this point in the history
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
tc39/proposal-built-in-modules#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
  • Loading branch information
littledan committed Mar 2, 2019
1 parent 552ef78 commit 3d3e219
Showing 1 changed file with 73 additions and 16 deletions.
89 changes: 73 additions & 16 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -2783,6 +2783,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#requestcredentials"><code>RequestCredentials</code></dfn> enumeration</li>
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#requestdestination"><code>RequestDestination</code></dfn> enumeration</li>
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#dom-global-fetch"><code>fetch()</code></dfn> method</li>
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#concept-body-consume-body">consume body</dfn></li>
<li>
<dfn data-x="concept-response"
data-x-href="https://fetch.spec.whatwg.org/#concept-response">response</dfn> and its
Expand Down Expand Up @@ -3122,6 +3123,15 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute

<p>User agents that support JavaScript must also implement the <cite>BigInt</cite> proposal. <ref
spec=JSBIGINT></p>

<p>User agents that support JavaScript must also implement the <cite>JavaScript standard library</cite> proposal.
The following terms are defined there, and used in this specification: <ref spec=JSSTDLIB></p>
<ul class="brief">
<li>The <dfn data-x-href="https://proposal-javascript-standard-library-domenic.now.sh/#sec-createsyntheticmodule">CreateSyntheticModule</dfn> abstract operation</li>
<li>The <dfn data-x-href="https://proposal-javascript-standard-library-domenic.now.sh/#sec-setsyntheticmoduleexport">SetSyntheticModuleExport</dfn> abstract operation</li>
<li><dfn data-x-href="https://proposal-javascript-standard-library-domenic.now.sh/#sec-synthetic-module-records">Synthetic Module Record</dfn></li>
<!-- TODO(littledan): Create a proper reference before landing this patch -->
</ul>
</dd>


Expand Down Expand Up @@ -86768,7 +86778,7 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {

<h5>Definitions</h5>

<p>A <dfn data-x="concept-script" data-export="">script</dfn> is one of two possible <span
<p>A <dfn data-x="concept-script" data-export="">script</dfn> is one of three possible <span
data-x="struct">structs</span>. All scripts have:</p>

<dl>
Expand All @@ -86781,7 +86791,9 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {

<dd><p>Either a <span>Script Record</span>, for <span data-x="classic script">classic
scripts</span>; a <span>Source Text Module Record</span>, for <span data-x="module
script">module scripts</span>; or null. In the former two cases, it represents a parsed script;
script">module scripts</span>; a <span>Synthetic Module Record</span> for <span
data-x="JSON module script">JSON module scripts</span>; 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.</p></dd>

<dt>A <dfn data-dfn-for="script" data-export="" data-x="concept-script-parse-error">parse
Expand Down Expand Up @@ -86834,6 +86846,10 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {
data-x="concept-script">script</span>. It has no additional <span data-x="struct
item">items</span>.</p>

<p>A <dfn data-export="">JSON module script</dfn> is another type of <span
data-x="concept-script">script</span>. It has no additional <span data-x="struct
item">items</span>.</p>

<p>The <dfn>active script</dfn> is determined by the following algorithm:</p>

<ol>
Expand Down Expand Up @@ -87472,25 +87488,63 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {

<li><p><var>response</var>'s <span data-x="concept-response-status">status</span> is not an
<span>ok status</span></p></li>
</ul>
</li>

<li>
<p>The result of <span data-x="extract a MIME type">extracting a MIME type</span> from
<var>response</var>'s <span data-x="concept-response-header-list">header list</span> is not a
<span>JavaScript MIME type</span></p>
<li>
<p>Let <var>type</var> be the result of <span data-x="extract a MIME type">extracting a
MIME type</span> from <var>response</var>'s <span data-x="concept-response-header-list">header
list</span>.</p>

<p class="note">For historical reasons, <span data-x="fetch a classic script">fetching a
classic script</span> does not include MIME type checking. In contrast, module scripts will
fail to load if they are not of a correct MIME type.</p>
</li>
</ul>
<p class="note">For historical reasons, <span data-x="fetch a classic script">fetching a
classic script</span> 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.</p>
</li>

<li><p>Let <var>source text</var> be the result of <span data-x="UTF-8 decode">UTF-8
decoding</span> <var>response</var>'s <span data-x="concept-response-body">body</span>.</p></li>
<li><p>Let <var>module script</var> be null.</p></li>

<li><p>Let <var>module script</var> be the result of <span>creating a module script</span> given
<var>source text</var>, <var>module map settings object</var>, <var>response</var>'s <span
data-x="concept-response-url">url</span>, and <var>options</var>.</p></li>
<li>
<p>If <var>type</var> is a <span>JavaScript MIME type</span>, then:</p>

<ol>
<li><p>Let <var>source text</var> be the result of <span data-x="UTF-8 decode">UTF-8
decoding</span> <var>response</var>'s <span data-x="concept-response-body">body</span>.</p></li>

<li><p>Set <var>module script</var> to the result of <span>creating a module script</span> given
<var>source text</var>, <var>module map settings object</var>, <var>response</var>'s <span
data-x="concept-response-url">url</span>, and <var>options</var>.</p></li>
</ol>
</li>

<li>
<p>If <var>type</var> is a <span>JSON MIME type</span>, then:</p>

<ol>
<li><p>Set <var>module script</var> to a new <span>JSON module script</span> that this algorithm
will subsequently initialize.</p></li>

<li>
<p>Let <var>json</var> be the result of running <span>consume body</span> on
<var>response</var> with <i>JSON</i>.</p>

<p>If this throws an exception, catch it, and set <var>module script</var>'s
<span data-x="concept-script-parse-error">parse error</span> to that exception.</p>
</li>

<li>
<p>If no exception was thrown, set <var>module script</var>'s <span
data-x="concept-script-record">record</span> to <span>CreateSyntheticModule</span>
(&laquo; "default" &raquo;, the following steps, <var>module map settings object</var>'s
<span data-x="environment settings object's Realm">Realm</span>, <var>json</var>) with the
following steps given <var>module</var> as an argument:</p>
<ol>
<li>1. <span>SetSyntheticModuleExport</span>(<var>module</var>, "default",
<var>module</var>.[[HostDefined]]).</li>
</ol>
</li>
</ol>
</li>

<li>
<p><span data-x="map set">Set</span> <var>moduleMap</var>[<var>url</var>] to <var>module
Expand Down Expand Up @@ -121881,6 +121935,9 @@ INSERT INTERFACES HERE
<dt id="refsJSINTL">[JSINTL]</dt>
<dd><cite><a href="https://tc39.github.io/ecma402/">ECMAScript Internationalization API Specification</a></cite>. Ecma International.</dd>

<dt id="refsJSSTDLIB">[JSSTDLIB]</dt>
<dd><cite><a href="https://github.com/tc39/proposal-javascript-standard-library/">JavaScript Standard Library</a></cite>. Ecma International.</dd>

<dt id="refsJSON">[JSON]</dt>
<dd><cite><a href="https://tools.ietf.org/html/rfc7159">The JavaScript Object Notation (JSON) Data Interchange Format</a></cite>, T. Bray. IETF.</dd>

Expand Down

0 comments on commit 3d3e219

Please sign in to comment.