Skip to content

Commit 04803c8

Browse files
authored
[worklets] First pass of fixing import for worklets. (w3c#251)
[worklets] Make import method of worklets use a shared cache. This ensures that they'll all have the same code regardless of when they are created. Also fixes threading issues in spec.
1 parent 8c47ced commit 04803c8

File tree

1 file changed

+70
-29
lines changed

1 file changed

+70
-29
lines changed

worklets/Overview.bs

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ Editor: Ian Kilpatrick, ikilpatrick@chromium.org
1212

1313
<pre class="anchors">
1414
urlPrefix: http://heycam.github.io/webidl/#dfn-; type: dfn;
15+
text: DOMException
16+
text: SyntaxError
1517
text: inherit
18+
urlPrefix: https://fetch.spec.whatwg.org/; type: dfn;
19+
urlPrefix: #concept-;
20+
text: fetch
1621
urlPrefix: https://html.spec.whatwg.org/multipage/browsers.html; type: dfn;
1722
text: effective script origin
1823
url: #origin-2; text: origin
@@ -27,7 +32,6 @@ urlPrefix: https://html.spec.whatwg.org/multipage/webappapis.html; type: dfn;
2732
text: code entry-point
2833
text: creation url
2934
text: document environment
30-
text: entry settings object
3135
text: environment settings object
3236
text: event loop
3337
text: fetch a module script tree
@@ -36,7 +40,9 @@ urlPrefix: https://html.spec.whatwg.org/multipage/webappapis.html; type: dfn;
3640
text: incumbent settings object
3741
text: microtask queue
3842
text: module script
43+
text: perform the request
3944
text: realm execution context
45+
text: relevant settings object
4046
text: responsible browsing context
4147
text: responsible document
4248
text: responsible event loop
@@ -174,16 +180,19 @@ When a user agent is to <dfn>create a WorkletGlobalScope</dfn>, for a given |wor
174180

175181
4. Associate the |settingsObject| with |workletGlobalScope|.
176182

177-
5. For each |resolvedModuleURL| in the given |worklet|'s <a>worklet's resolved module URLs</a>,
183+
5. For each |entry| in the given |worklet|'s <a>module responses map</a> (in insertion order),
178184
run the following substeps:
179-
1. Let |script| be the result of <a>fetch a module script tree</a> given
185+
186+
1. Let |resolvedModuleURL| be |entry|'s key.
187+
188+
2. Let |script| be the result of <a>fetch a module script tree</a> given
180189
|resolvedModuleURL|, "anonymous" for the <a>CORS setting attribute</a>, and
181190
|settingsObject|.
182191

183192
Note: Worklets follow <a>web workers</a> here in not allowing "use-credientials" for
184193
fetching resources.
185194

186-
2. <a>Run a module script</a> given |script|.
195+
3. <a>Run a module script</a> given |script|.
187196

188197
### Script settings for worklets ### {#script-settings-for-worklets}
189198

@@ -247,7 +256,7 @@ The {{Worklet}} object provides the capability to import module scripts into its
247256

248257
<pre class='idl'>
249258
interface Worklet {
250-
[NewObject] Promise&lt;void> import(DOMString moduleURL);
259+
[NewObject] Promise&lt;void> import(USVString moduleURL);
251260
};
252261
</pre>
253262

@@ -259,52 +268,84 @@ Note: As an example the <a>worklet global scope type</a> might be a {{PaintWorkl
259268
A {{Worklet}} has a list of the <dfn export>worklet's WorkletGlobalScopes</dfn>. Initially this list
260269
is empty; it is populated when the user agent chooses to create its {{WorkletGlobalScope}}.
261270

262-
A {{Worklet}} has a list of the <dfn>worklet's resolved module URLs</dfn>. Initially this list is
263-
empty; it is populated when module scripts resolved.
271+
A {{Worklet}} has a <dfn>module responses map</dfn>. This is a ordered map of module URLs to values
272+
that are a <a>fetch</a> responses. The map's entries are ordered based on their insertion order.
273+
Access to this map should be thread-safe.
274+
275+
The <a>module responses map</a> exists to ensure that {{WorkletGlobalScope}}s created at different
276+
times contain the same set of script source text and have the same behaviour. The creation of
277+
additional {{WorkletGlobalScope}}s should be transparent to the author.
264278

265279
When the <dfn method for=Worklet>import(|moduleURL|)</dfn> method is called on a {{Worklet}} object,
266280
the user agent <em>must</em> run the following steps:
267281
1. Let |promise| be <a>a new promise</a>.
268282

269-
2. Run the following steps <a>in parallel</a>:
270-
1. Let |resolvedModuleURL| be the result of <a>resolving</a> the |moduleURL| relative to the
271-
<a>API base URL</a> specified by the <a>entry settings object</a> when the method was
272-
invoked.
283+
2. Let |resolvedModuleURL| be the result of <a>parsing</a> the |moduleURL| argument relative to
284+
the <a>relevant settings object</a> of <b>this</b>.
285+
286+
3. If this fails, reject |promise| with a "<a>SyntaxError</a>" <a>DOMException</a> and abort
287+
these steps.
273288

274-
2. If this fails, reject |promise| with a <a>SyntaxError</a> exception and abort these
275-
steps.
289+
4. Ensure that there is at least one {{WorkletGlobalScope}} in the <a>worklet's
290+
WorkletGlobalScopes</a>. If not <a>create a WorkletGlobalScope</a> given the current
291+
{{Worklet}}.
276292

277-
3. Add |resolvedModuleURL| to the list of <a>worklet's resolved module URLs</a>.
293+
The user-agent may also create additional {{WorkletGlobalScope}}s at this time.
278294

279-
4. Ensure that there is at least one {{WorkletGlobalScope}} in the <a>worklet's
280-
WorkletGlobalScopes</a>. If not <a>create a WorkletGlobalScope</a> given the current
281-
{{Worklet}}.
295+
5. Let |outsideSettings| be the <a>relevant settings object</a> of <b>this</b>.
282296

283-
5. For each {{WorkletGlobalScope}} in the <a>worklet's WorkletGlobalScopes</a>, run these
297+
6. Run the following steps <a>in parallel</a>:
298+
299+
1. For each {{WorkletGlobalScope}} in the <a>worklet's WorkletGlobalScopes</a>, run these
284300
substeps:
285-
1. Let |settings| be the {{WorkletGlobalScope}}'s associated <a>environment settings
286-
object</a>.
287301

288-
2. Let |script| be the result of <a>fetch a module script tree</a> given
289-
|resolvedModuleURL|, "anonymous" for the <a>CORS setting attribute</a>, and
290-
|settings|.
302+
1. Let |insideSettings| be the {{WorkletGlobalScope}}'s associated <a>environment
303+
settings object</a>.
304+
305+
2. <a>Fetch a module script tree</a> given |resolvedModuleURL|, "omit", the empty string
306+
(as no cryptographic nonce is present for worklets), "not parser-inserted",
307+
"script", |outsideSettings|, and |insideSettings|.
308+
309+
To <a>perform the request</a> given |request|, perform the following steps:
310+
311+
1. Let |cache| be the current {{Worklet}}'s <a>module responses map</a>.
312+
313+
2. Let |url| be |request|'s <a >url</a>.
314+
315+
3. If |cache| contains an entry with key |url| whose value is "fetching", wait
316+
(<a>in parallel</a>) until that entry's value changes, then proceed to the
317+
next step.
318+
319+
4. If |cache| contains an entry with key |url|, asynchronously complete this
320+
algorithm with that entry's value, and abort these steps.
321+
322+
5. Create an entry in |cache| with key |url| and value "fetching".
323+
324+
6. <a>Fetch</a> |request|.
325+
326+
7. Let |response| be the result of <a>fetch</a> when it asynchronously
327+
completes.
328+
329+
8. Set the value of the entry in |cache| whose key is |url| to |response|, and
330+
asynchronously complete this algorithm with |response|.
291331

292-
Note: Worklets follow <a>web workers</a> here in not allowing "use-credientials" for
293-
fetching resources.
332+
3. Let |script| be the result of <a>fetch a module script tree</a> when it
333+
asynchronously completes.
294334

295-
3. <a>Run a module script</a> given |script|.
335+
4. <a>Run a module script</a> given |script|.
296336

297-
6. If <em>all</em> the steps above succeeded (in particular, if all of the scripts parsed
337+
2. If <em>all</em> the steps above succeeded (in particular, if all of the scripts parsed
298338
and loaded into the global scopes), resolve |promise|.
299339
<br>Otherwise, reject |promise|.
300340

301341
Note: Specifically, if a script fails to parse or fails to load over the network, it should
302342
reject the promise; if the script throws an error while first evaluating the promise
303343
should resolve as a classes may have been registered correctly.
304344

305-
3. Return |promise|.
345+
7. Return |promise|.
306346

307-
Issue(w3c/css-houdini-drafts#47): Need ability to load code into {{WorkletGlobalScope}} declaratively.
347+
Issue(w3c/css-houdini-drafts#47): Need ability to load code into a {{WorkletGlobalScope}}
348+
declaratively.
308349

309350
Lifetime of the Worklet {#lifetime-of-the-worklet}
310351
--------------------------------------------------

0 commit comments

Comments
 (0)