Skip to content

Commit d76829f

Browse files
littledandomenic
authored andcommitted
[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 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 whatwg#4315
1 parent 38c50c4 commit d76829f

File tree

1 file changed

+73
-16
lines changed

1 file changed

+73
-16
lines changed

source

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
27782778
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#requestcredentials"><code>RequestCredentials</code></dfn> enumeration</li>
27792779
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#requestdestination"><code>RequestDestination</code></dfn> enumeration</li>
27802780
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#dom-global-fetch"><code>fetch()</code></dfn> method</li>
2781+
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#concept-body-consume-body">consume body</dfn></li>
27812782
<li>
27822783
<dfn data-x="concept-response"
27832784
data-x-href="https://fetch.spec.whatwg.org/#concept-response">response</dfn> and its
@@ -3116,6 +3117,15 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
31163117

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

31213131

@@ -86655,7 +86665,7 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {
8665586665

8665686666
<h5>Definitions</h5>
8665786667

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

8666186671
<dl>
@@ -86668,7 +86678,9 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {
8666886678

8666986679
<dd><p>Either a <span>Script Record</span>, for <span data-x="classic script">classic
8667086680
scripts</span>; a <span>Source Text Module Record</span>, for <span data-x="module
86671-
script">module scripts</span>; or null. In the former two cases, it represents a parsed script;
86681+
script">module scripts</span>; a <span>Synthetic Module Record</span> for <span
86682+
data-x="JSON module script">JSON module scripts</span>; or null. In the
86683+
former two cases, it represents a parsed script; in the third case, a parsed JSON document;
8667286684
null represents a failure parsing.</p></dd>
8667386685

8667486686
<dt>A <dfn data-dfn-for="script" data-export="" data-x="concept-script-parse-error">parse
@@ -86721,6 +86733,10 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {
8672186733
data-x="concept-script">script</span>. It has no additional <span data-x="struct
8672286734
item">items</span>.</p>
8672386735

86736+
<p>A <dfn data-export="">JSON module script</dfn> is another type of <span
86737+
data-x="concept-script">script</span>. It has no additional <span data-x="struct
86738+
item">items</span>.</p>
86739+
8672486740
<p>The <dfn>active script</dfn> is determined by the following algorithm:</p>
8672586741

8672686742
<ol>
@@ -87359,25 +87375,63 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {
8735987375

8736087376
<li><p><var>response</var>'s <span data-x="concept-response-status">status</span> is not an
8736187377
<span>ok status</span></p></li>
87378+
</ul>
87379+
</li>
8736287380

87363-
<li>
87364-
<p>The result of <span data-x="extract a MIME type">extracting a MIME type</span> from
87365-
<var>response</var>'s <span data-x="concept-response-header-list">header list</span> is not a
87366-
<span>JavaScript MIME type</span></p>
87381+
<li>
87382+
<p>Let <var>type</var> be the result of <span data-x="extract a MIME type">extracting a
87383+
MIME type</span> from <var>response</var>'s <span data-x="concept-response-header-list">header
87384+
list</span>.</p>
8736787385

87368-
<p class="note">For historical reasons, <span data-x="fetch a classic script">fetching a
87369-
classic script</span> does not include MIME type checking. In contrast, module scripts will
87370-
fail to load if they are not of a correct MIME type.</p>
87371-
</li>
87372-
</ul>
87386+
<p class="note">For historical reasons, <span data-x="fetch a classic script">fetching a
87387+
classic script</span> does not include MIME type checking. In contrast, module scripts'
87388+
interpretation is driven by their MIME type, and they will fail to load if they are not of
87389+
a supported MIME type.</p>
8737387390
</li>
8737487391

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

87378-
<li><p>Let <var>module script</var> be the result of <span>creating a module script</span> given
87379-
<var>source text</var>, <var>module map settings object</var>, <var>response</var>'s <span
87380-
data-x="concept-response-url">url</span>, and <var>options</var>.</p></li>
87394+
<li>
87395+
<p>If <var>type</var> is a <span>JavaScript MIME type</span>, then:</p>
87396+
87397+
<ol>
87398+
<li><p>Let <var>source text</var> be the result of <span data-x="UTF-8 decode">UTF-8
87399+
decoding</span> <var>response</var>'s <span data-x="concept-response-body">body</span>.</p></li>
87400+
87401+
<li><p>Set <var>module script</var> to the result of <span>creating a module script</span> given
87402+
<var>source text</var>, <var>module map settings object</var>, <var>response</var>'s <span
87403+
data-x="concept-response-url">url</span>, and <var>options</var>.</p></li>
87404+
</ol>
87405+
</li>
87406+
87407+
<li>
87408+
<p>If <var>type</var> is a <span>JSON MIME type</span>, then:</p>
87409+
87410+
<ol>
87411+
<li><p>Set <var>module script</var> to a new <span>JSON module script</span> that this algorithm
87412+
will subsequently initialize.</p></li>
87413+
87414+
<li>
87415+
<p>Let <var>json</var> be the result of running <span>consume body</span> on
87416+
<var>response</var> with <i>JSON</i>.</p>
87417+
87418+
<p>If this throws an exception, catch it, and set <var>module script</var>'s
87419+
<span data-x="concept-script-parse-error">parse error</span> to that exception.</p>
87420+
</li>
87421+
87422+
<li>
87423+
<p>If no exception was thrown, set <var>module script</var>'s <span
87424+
data-x="concept-script-record">record</span> to <span>CreateSyntheticModule</span>
87425+
(&laquo; "default" &raquo;, the following steps, <var>module map settings object</var>'s
87426+
<span data-x="environment settings object's Realm">Realm</span>, <var>json</var>) with the
87427+
following steps given <var>module</var> as an argument:</p>
87428+
<ol>
87429+
<li>1. <span>SetSyntheticModuleExport</span>(<var>module</var>, "default",
87430+
<var>module</var>.[[HostDefined]]).</li>
87431+
</ol>
87432+
</li>
87433+
</ol>
87434+
</li>
8738187435

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

122011+
<dt id="refsJSSTDLIB">[JSSTDLIB]</dt>
122012+
<dd><cite><a href="https://github.com/tc39/proposal-javascript-standard-library/">JavaScript Standard Library</a></cite>. Ecma International.</dd>
122013+
121957122014
<dt id="refsJSON">[JSON]</dt>
121958122015
<dd><cite><a href="https://tools.ietf.org/html/rfc7159">The JavaScript Object Notation (JSON) Data Interchange Format</a></cite>, T. Bray. IETF.</dd>
121959122016

0 commit comments

Comments
 (0)