Skip to content

Commit

Permalink
Add worker modules
Browse files Browse the repository at this point in the history
Closes #550. The Worker and SharedWorker constructors now get an options
object, which can be specified as { type: "module", crossOrigin }, where
crossOrigin specifies the credentials mode used for the initial fetch
and for subsequent imports. The module fetching and execution machinery
is entirely reused from that for <script type="module">.

This commit does not include any modifications to importScripts; those
are in a subsequent commit.
  • Loading branch information
domenic committed Feb 2, 2016
1 parent efd6202 commit 85cb12c
Showing 1 changed file with 103 additions and 44 deletions.
147 changes: 103 additions & 44 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -84603,6 +84603,45 @@ interface <dfn>NavigatorOnLine</dfn> {
<li>Asynchronously complete this algorithm with <var>script</var>.</li>
</ol>

<p>To <dfn>fetch a classic worker script</dfn> given a <var>url</var>, a <var>referrer</var>, a
<var>settings object</var>, and a boolean <var>is shared</var>, run these steps. The algorithm
will asynchronously complete with either null (on failure) or a new <span>classic script</span>
(on success).</p>

<ol>
<li><p>Let <var>request</var> be a new <span data-x="concept-request">request</span> whose
<span data-x="concept-request-url">url</span> is <var>url</var>, <span
data-x="concept-request-client">client</span> is <var>settings object</var>, <span
data-x="concept-request-type">type</span> is "<code data-x="">script</code>", <span
data-x="concept-request-destination">destination</span> is "<code
data-x="">sharedworker</code>" if <var>is shared</var> is true and "<code
data-x="">worker</code>" otherwise, <span data-x="concept-request-referrer">referrer</span> is
<var>referrer</var>, <span data-x="concept-request-mode">mode</span> is "<code
data-x="">same-origin</code>", <span data-x="concept-request-credentials-mode">credentials
mode</span> is "<code data-x="">same-origin</code>", and whose <span>use-URL-credentials
flag</span> is set.</p></li>

<!--FETCH-->
<li><p><span data-x="concept-fetch">Fetch</span> <var>request</var>.</p></li>

<li><p>Return from this algorithm, and run the remaining steps as part of the fetch's
<span>process response</span> for the <span data-x="concept-response">response</span>
<var>response</var>.</p></li>

<li><p>If <var>response</var>'s <span data-x="concept-response-type">type</span> is "<code
data-x="">error</code>", or <var>response</var>'s <span
data-x="concept-response-status">status</span> is not an <span>ok status</span>, asynchronously
complete this algorithm with null, and abort these steps.</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>script</var> be the result of <span>creating a classic script</span> using
<var>source text</var> and <var>settings object</var>.</p></li>

<li>Asynchronously complete this algorithm with <var>script</var>.</li>
</ol>

<p>To <dfn>fetch a module script tree</dfn> given a <var>url</var>, a <var>credentials mode</var>,
and a <var>settings object</var>, run these steps. The algorithm will asynchronously complete
with either null (on failure) or a new <span>module script</span> (on success).</p>
Expand Down Expand Up @@ -93986,8 +94025,9 @@ interface <dfn>WorkerGlobalScope</dfn> : <span>EventTarget</span> {
<code>SharedWorker</code> object <var>worker</var>, <span>URL</span> <var>url</var>,
<span>URL</span> <var>referrer</var>, <code>MessagePort</code> <var>outside port</var>,
<span>list of relevant <code>Document</code> objects to add</span> <var>docs</var>, possible
<code>WorkerGlobalScope</code> <var>parent worker global scope</var>, and a boolean flag <var>is
shared</var>, it must run the following steps:</p>
<code>WorkerGlobalScope</code> <var>parent worker global scope</var>, a boolean flag <var>is
shared</var>, and a <code>WorkerOptions</code> dictionary <var>options</var>, it must run the
following steps:</p>

<ol>

Expand All @@ -94001,36 +94041,6 @@ interface <dfn>WorkerGlobalScope</dfn> : <span>EventTarget</span> {

</li>

<li><p>Let <var>request</var> be a new <span data-x="concept-request">request</span> whose
<span data-x="concept-request-url">url</span> is <var>url</var>, <span
data-x="concept-request-client">client</span> is <var>settings object</var>, <span
data-x="concept-request-type">type</span> is "<code data-x="">script</code>", <span
data-x="concept-request-destination">destination</span> is "<code
data-x="">sharedworker</code>" if <var>is shared</var> is true and "<code
data-x="">worker</code>" otherwise, <span data-x="concept-request-referrer">referrer</span> is
<var>referrer</var>, <span>synchronous flag</span> is set, <span
data-x="concept-request-mode">mode</span> is "<code data-x="">same-origin</code>", <span
data-x="concept-request-credentials-mode">credentials mode</span> is "<code
data-x="">same-origin</code>", and whose <span>use-URL-credentials flag</span> is set.</p></li>

<!--FETCH--><li><p>Let <var>response</var> be the result of <span
data-x="concept-fetch">fetching</span> <var>request</var>.</p></li>

<li>

<p>If <var>response</var>'s <span data-x="concept-response-status">status</span> is an
<span>ok status</span>, then let <var>source</var> be the result of running the <span>UTF-8
decode</span> algorithm on <var>response</var>'s <span
data-x="concept-response-body">body</span>.</p>

<p class="note">As with <code>script</code> elements, the MIME type of the script is ignored.
Unlike with <code>script</code> elements, there is no way to override the type. It's always
assumed to be JavaScript.</p>

</li>

<li><p>Otherwise, <span>queue a task</span> to <span>fire a simple event</span> named
<code data-x="event-error">error</code> at <var>worker</var>. Abort these steps.</p></li>

<li>
<p>Call the JavaScript <span
Expand All @@ -94053,6 +94063,45 @@ interface <dfn>WorkerGlobalScope</dfn> : <span>EventTarget</span> {
<li><p><span>Set up a worker environment settings object</span> with <var>realm execution
context</var>, and let <var>settings object</var> be the result.</p></li>

<li>
<p>Obtain <var>script</var> by switching on the value of <var>options</var>'s <code
data-x="">type</code> member:</p>

<dl class="switch">
<dt>"<code data-x="">classic</code>"</dt>
<dd><span>Fetch a classic worker script</span> given <var>url</var>, <var>referrer</var>,
<var>settings object</var>, and <var>is shared</var>.</dd>

<dt>"<code data-x="">module</code>"</dt>
<dd>
<ol>
<li>
<p>Let <var>credentials mode</var> be determined by switching on the value of the
<code data-x="">crossOrigin</code> member of <var>options</var>:</p>

<dl class="switch">
<dt>null</dt>
<dd>"<code data-x="">omit</code>"</dd>

<dt>"<code data-x="">anonymous</code>"</dt>
<dd>"<code data-x="">same-origin</code>"</dd>

<dt>"<code data-x="">use-credentials</code>"</dt>
<dd>"<code data-x="">include</code>"</dd>
</dl>
</li>

<li><p><span>Fetch a module script tree</span> given <var>url</var>, <var>credentials
mode</var>, and <var>settings object</var>.</p></li>
</ol>
</dl>

<p>If the algorithm asynchronously completes with null, <span>queue a task</span> to <span>fire
a simple event</span> named <code data-x="event-error">error</code> at <var>worker</var>, and
abort these steps. Otherwise, continue the rest of these steps after the algorithm's
asynchronous completion, with <var>script</var> being the asynchronous completion value.</p>
</li>

<li><p>Associate <var>worker</var> with <var>worker global scope</var>.</p></li>

<li><p><span>Create a new <code>MessagePort</code> object</span> whose <span
Expand Down Expand Up @@ -94115,11 +94164,10 @@ interface <dfn>WorkerGlobalScope</dfn> : <span>EventTarget</span> {

<!-- SCRIPT EXEC -->

<li><p>Let <var>script</var> be the result of <span>creating a classic script</span> using
<var>source</var> and <var>settings object</var>.</p></li>

<li>
<p><span data-x="run a classic script">Run the classic script</span> <var>script</var>.</p>
<p>Perform either the <span>run a classic script</span> or <span>run a module script</span>
algorithms on <var>script</var>, depending on whether it is a <span>classic script</span> or
<span>module script</span>.</p>

<p class="note">In addition to the usual possibilities of returning a value or failing due to
an exception, this could be prematurely aborted by the "<span>kill a worker</span>" or
Expand Down Expand Up @@ -94412,13 +94460,22 @@ interface <dfn>AbstractWorker</dfn> {

<h5>Dedicated workers and the <code>Worker</code> interface</h5>

<pre class="idl">[<span data-x="dom-Worker">Constructor</span>(DOMString scriptURL), Exposed=(Window,Worker)]
<pre class="idl">[<span data-x="dom-Worker">Constructor</span>(DOMString scriptURL, optional <span>WorkerOptions</span> options), Exposed=(Window,Worker)]
interface <dfn>Worker</dfn> : <span>EventTarget</span> {
void <span data-x="dom-Worker-terminate">terminate</span>();

void <span data-x="dom-Worker-postMessage">postMessage</span>(any message, optional sequence&lt;<span>Transferable</span>&gt; transfer);
attribute <span>EventHandler</span> <span data-x="handler-Worker-onmessage">onmessage</span>;
};

dictionary <dfn>WorkerOptions</dfn> {
<span>WorkerType</span> type = "classic";
<span>WorkerCrossOriginMode</span>? crossOrigin = null;
};

enum <dfn>WorkerType</dfn> { "classic", "module" };
enum <dfn>WorkerCrossOriginMode</dfn> { "anonymous", "use-credentials" };

<span>Worker</span> implements <span>AbstractWorker</span>;</pre>

<p>The <dfn><code data-x="dom-Worker-terminate">terminate()</code></dfn> method, when invoked, must
Expand Down Expand Up @@ -94459,8 +94516,9 @@ interface <dfn>Worker</dfn> : <span>EventTarget</span> {

<hr>

<p>When the <dfn><code data-x="dom-Worker">Worker(<var>scriptURL</var>)</code></dfn> constructor
is invoked, the user agent must run the following steps:</p>
<p>When the <dfn><code data-x="dom-Worker">Worker(<var>scriptURL</var>,
<var>options</var>)</code></dfn> constructor is invoked, the user agent must run the following
steps:</p>

<ol>

Expand Down Expand Up @@ -94503,15 +94561,15 @@ interface <dfn>Worker</dfn> : <span>EventTarget</span> {

<li><p><span>Run a worker</span> given <var>worker</var>, <var>worker URL</var>, the
<span>incumbent settings object</span>'s <span>creation URL</span>, <var>outside port</var>,
<var>docs</var>, <var>parent worker global scope</var>, and false.</p></li>
<var>docs</var>, <var>parent worker global scope</var>, false, and <var>options</var>.</p></li>

</ol>



<h5>Shared workers and the <code>SharedWorker</code> interface</h5>

<pre class="idl">[<span data-x="dom-SharedWorker">Constructor</span>(DOMString scriptURL, optional DOMString name = ""), Exposed=(Window,Worker)]
<pre class="idl">[<span data-x="dom-SharedWorker">Constructor</span>(DOMString scriptURL, optional DOMString name = "", optional <span>WorkerOptions</span> options), Exposed=(Window,Worker)]
interface <dfn>SharedWorker</dfn> : <span>EventTarget</span> {
readonly attribute <span>MessagePort</span> <span data-x="dom-SharedWorker-port">port</span>;
};
Expand All @@ -94521,8 +94579,9 @@ interface <dfn>SharedWorker</dfn> : <span>EventTarget</span> {
it was assigned by the object's constructor. It represents the <code>MessagePort</code> for
communicating with the shared worker.</p>

<p>When the <dfn><code data-x="dom-SharedWorker">SharedWorker(<var>scriptURL</var>, <var>name</var>)</code></dfn> constructor is invoked, the user agent must run the following
steps:</p>
<p>When the <dfn><code data-x="dom-SharedWorker">SharedWorker(<var>scriptURL</var>,
<var>name</var>, <var>options</var>)</code></dfn> constructor is invoked, the user agent must run
the following steps:</p>

<ol>

Expand Down Expand Up @@ -94661,7 +94720,7 @@ interface <dfn>SharedWorker</dfn> : <span>EventTarget</span> {

<li><p><span>Run a worker</span> given <var>worker</var>, <var>parsed scriptURL</var>, the
<span>incumbent settings object</span>'s <span>creation URL</span>, <var>outside port</var>,
<var>docs</var>, <var>parent worker global scope</var>, and true.</p></li>
<var>docs</var>, <var>parent worker global scope</var>, true, and <var>options</var>.</p></li>

</ol>

Expand Down

0 comments on commit 85cb12c

Please sign in to comment.