Skip to content

Commit 62beb3b

Browse files
committed
[worklets] Move worklets to using modules. \o/
1 parent 86646d1 commit 62beb3b

File tree

1 file changed

+64
-68
lines changed

1 file changed

+64
-68
lines changed

worklets/Overview.bs

+64-68
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@ Editor: Ian Kilpatrick, ikilpatrick@chromium.org
1010
</pre>
1111

1212
<pre class="anchors">
13-
urlPrefix: https://fetch.spec.whatwg.org/; type: dfn;
14-
text: ok status
15-
urlPrefix: #concept-response-;
16-
text: body
17-
text: status
1813
urlPrefix: https://html.spec.whatwg.org/multipage/browsers.html; type: dfn;
1914
text: effective script origin
2015
url: #origin-2; text: origin
2116
urlPrefix: https://html.spec.whatwg.org/multipage/workers.html; type: dfn;
22-
text: get a fetch result
23-
text: import scripts into worker global scope
24-
text: importing scripts and libraries
25-
text: postprocess the fetch result
26-
text: validate the state
2717
text: web workers
2818
urlPrefix: #dom-workerglobalscope-;
2919
text: self
@@ -32,39 +22,38 @@ urlPrefix: https://html.spec.whatwg.org/multipage/webappapis.html; type: dfn;
3222
text: api url character encoding
3323
text: browsing context
3424
text: code entry-point
35-
text: create a script
3625
text: creation url
3726
text: document environment
27+
text: entry settings object
3828
text: environment settings object
3929
text: event loop
30+
text: fetch a module script tree
4031
text: global object
4132
text: https state
4233
text: incumbent settings object
4334
text: microtask queue
35+
text: module script
4436
text: realm execution context
4537
text: responsible browsing context
4638
text: responsible document
4739
text: responsible event loop
40+
text: run a module script
4841
text: script execution environment
4942
text: task queues
5043
urlPrefix: https://html.spec.whatwg.org/multipage/infrastructure.html; type: dfn;
44+
text: cors setting attribute
5145
text: in parallel
5246
text: javascript global environment
53-
text: unsafe response
54-
text: utf-8 decode
5547
urlPrefix: #js-;
5648
text: syntaxerror;
5749
url: resolve-a-url; text: resolve;
5850
urlPrefix: https://www.w3.org/2001/tag/doc/promises-guide; type: dfn;
5951
text: a new promise
6052
urlPrefix: http://www.ecma-international.org/ecma-262/6.0/#sec-; type: dfn;
6153
text: Construct
62-
text: FunctionCreate
6354
text: InitializeHostDefinedRealm
6455
text: Invoke
65-
text: NewObjectEnvironment
6656
text: strict mode code
67-
url: function-definitions; text: FunctionBody;
6857
</pre>
6958

7059
Introduction {#intro}
@@ -121,9 +110,9 @@ on a class should produce the same output given a particular input.
121110
The following techniques should be used in order to encourage authors to write code in an idempotent
122111
way:
123112
- No reference to the global object, e.g. <a>self</a> on a {{DedicatedWorkerGlobalScope}}.
124-
- Code is loaded similar to how ES6 Modules are loaded, in <a>strict mode code</a> inside an
125-
anonymous function. This prevents two different scripts sharing state by referencing shared
126-
objects on the global scope.
113+
- Code is loaded as a <a>module script</a> which resulting in the code being executed in <a>strict
114+
mode code</a> without a shared this. This prevents two different module scripts sharing
115+
state be referencing shared objects on the global scope.
127116
- User agents may choose to always have at least two {{WorkletGlobalScope}}s per {{Worklet}} and
128117
randomly assign a method or set of methods on a class to a particular global scope.
129118
- User agents may create and destroy {{WorkletGlobalScope}}s at any time.
@@ -179,8 +168,15 @@ and |worklet|, it <em>must</em> run the following steps:
179168

180169
2. Associate the |settingsObject| with |workletGlobalScope|.
181170

182-
3. For each |script| in the given |worklet|'s <b>worklet's loaded scripts</b> run the |script|
183-
in the |workletGlobalScope|.
171+
3. For each |resolvedModuleURL| in the given |worklet|'s <b>worklet's resolved module URLs</b>,
172+
run the following substeps:
173+
1. Let |script| be the result of <a>fetch a module script tree</a> given
174+
|resolvedModuleURL|, "anonymous" for the <a>CORS setting attribute</a>, and
175+
|settingsObject|.
176+
177+
Issue: Decide if "use-credientials" should be valid for worklets.
178+
179+
2. <a>Run a module script</a> given |script|.
184180

185181
### Script settings for worklets ### {#script-settings-for-worklets}
186182

@@ -238,60 +234,56 @@ Issue: Merge this with https://html.spec.whatwg.org/multipage/workers.html#set-u
238234
Worklet {#worklet-section}
239235
--------------------------
240236

241-
The {{Worklet}} object provides the capability to import scripts into its associated {{WorkletGlobalScope}}s. The user agent can then create classes registered
242-
on the {{WorkletGlobalScope}}s and invoke their methods.
237+
The {{Worklet}} object provides the capability to import module scripts into its associated
238+
{{WorkletGlobalScope}}s. The user agent can then create classes registered on the
239+
{{WorkletGlobalScope}}s and invoke their methods.
243240

244241
<pre class='idl'>
245242
interface Worklet {
246-
[NewObject] Promise&lt;void&gt; importScripts(DOMString... urls);
243+
[NewObject] Promise&lt;void> import(DOMString moduleURL);
247244
};
248245
</pre>
249246

250-
A {{Worklet}} has a list of the <b>worklet's {{WorkletGlobalScope}}s</b>. Initially this list is empty; it is populated when the user agent chooses to create its {{WorkletGlobalScope}}.
247+
A {{Worklet}} has a list of the <b>worklet's WorkletGlobalScopes</b>. Initially this list is empty;
248+
it is populated when the user agent chooses to create its {{WorkletGlobalScope}}.
251249

252-
A {{Worklet}} has a list of the <b>worklet's loaded scripts</b>. Initially this list is empty; it is populated when scripts are fetched and parsed.
250+
A {{Worklet}} has a list of the <b>worklet's resolved module URLs</b>. Initially this list is empty; it is
251+
populated when module scripts resolved.
253252

254-
When the <dfn method for=Worklet>importScripts(urls)</dfn> method is called on a {{Worklet}} object, the user agent <em>must</em> run the following steps:
253+
When the <dfn method for=Worklet>import(moduleURL)</dfn> method is called on a {{Worklet}} object,
254+
the user agent <em>must</em> run the following steps:
255255
1. Let |promise| be <a>a new promise</a>.
256+
256257
2. Run the following steps <a>in parallel</a>:
257-
1. Let |settingsObject| be the <a>incumbent settings object</a>.
258-
2. If there are no arguments, return without doing anything. Abort these steps.
259-
3. <a>Resolve</a> each argument relative to settings object's <a>API base URL</a>.
260-
4. If any fail, throw a SyntaxError</a> exception and abort these steps.
261-
5. For each URL in the resulting absolute URLs, run these substeps:
262-
1. Let response be the result of <a>get a fetch result</a>, given |url| and |settingsObject|.
263-
2. Let |source| be the result of running the <a>UTF-8 decode</a> algorithm on response's <a>unsafe response</a>'s <a>body</a>.
264-
3. Let |script| be the result of running <a>create an isolated script</a> using |source| as the script source, the <a>URL</a> from which source was obtained, and |settingsObject| as the environment settings object.
265-
266-
Parse the newly created |script|. If it failed to parse, then reject the |promise| and abort all these steps.
267-
268-
4. Add |script| to the list of the <b>worklet's loaded scripts</b>.
269-
6. For each {{WorkletGlobalScope}} in the <b>worklet's {{WorkletGlobalScope}}s</b>, run these substeps.
270-
1. For each newly created |script| in <b>worklet's loaded scripts</b> run the script in the {{WorkletGlobalScope}}.
271-
7. If <em>all</em> the steps above succeeded (in particular, if all of the scripts parsed and loaded into the global scopes), resolve |promise|
272-
<br>Otherwise, reject |promise|.
273-
3. Return |promise|.
258+
1. If there are no arguments, return without doing anything. Abort these steps.
274259

275-
Issue(w3c/css-houdini-drafts#51): What should happen when a script throws an exception while executing for the first time?
260+
2. Let |resolvedModuleURL| be the result of <a>resolving</a> the |moduleURL| relative to the
261+
<a>API base URL</a> specified by the <a>entry settings object</a> when the method was
262+
invoked.
263+
264+
3. If this fails, reject |promise| with a <a>SyntaxError</a> exception and abort these steps.
276265

277-
Issue(w3c/css-houdini-drafts#52): Is importScripts the right API call? Should it be singular instead?
266+
4. Add |resolvedModuleURL| to the list of <b>worklet's resolved module URLs</b>.
278267

279-
When a user agent is to <dfn>create an isolated script</dfn> given some |scriptSource|, an <a>environment settings object</a>,
280-
it must run the following steps:
281-
1. Let |script| be a new script that this algorithm will subsequently initialise.
282-
2. Obtain the appropriate <a>script execution environment</a> for the given scripting language from the <a>environment settings object</a> provided.
283-
3. Let |kind| be Normal.
268+
5. For each {{WorkletGlobalScope}} in the <b>worklet's WorkletGlobalScopes</b>, run these
269+
substeps:
270+
1. Let |settings| be the {{WorkletGlobalScope}}'s associated
271+
<a>environment settings object</a>.
284272

285-
Let |parameterList| be an empty list.
273+
2. Let |script| be the result of <a>fetch a module script tree</a> given
274+
|resolvedModuleURL|, "anonymous" for the <a>CORS setting attribute</a>, and |settings|.
286275

287-
Let |parsedBodySource| be the result of parsing |scriptSource| as a <a>FunctionBody</a> using the <a>script execution environment</a>.
276+
Issue: Decide if "use-credientials" should be valid for worklets.
288277

289-
Let |strict| be true.
278+
3. <a>Run a module script</a> given |script|.
290279

291-
Let |scope| be the result of <a>NewObjectEnvironment</a>(O=<em>null</em>, E=<em>the global environment</em>).
292-
4. Let |function| be the result of <a>FunctionCreate</a>(kind=|kind|, ParameterList=|parameterList|, Body=|parsedBodySource|, Scope=|scope|, Strict=|strict|).
293-
5. Let |script|'s <a>code entry-point</a> be |function|.
294-
6. Return |script|.
280+
6. If <em>all</em> the steps above succeeded (in particular, if all of the scripts parsed and
281+
loaded into the global scopes), resolve |promise|
282+
<br>Otherwise, reject |promise|.
283+
284+
3. Return |promise|.
285+
286+
Issue(w3c/css-houdini-drafts#51): What should happen when a script throws an exception while executing for the first time?
295287

296288
Issue(w3c/css-houdini-drafts#47): Need ability to load code into {{WorkletGlobalScope}} declaratively.
297289

@@ -302,16 +294,20 @@ Lifetime of the Worklet {#lifetime-of-the-worklet}
302294

303295
The lifetime of a {{Worklet}} is tied to the object it belongs to, for example the {{Window}}.
304296

305-
The lifetime of a {{WorkletGlobalScope}} is tied to the execution lifetime of a method or set of methods on a class, not to the lifetime of the {{Worklet}} object.
297+
The lifetime of a {{WorkletGlobalScope}} is tied to the execution lifetime of a method or set of
298+
methods on a class, not to the lifetime of the {{Worklet}} object.
306299

307-
The user agent <em>may</em> terminate a {{WorkletGlobalScope}} at any time it has no callbacks to handle or detects abnormal operation such as infinite loops and
308-
callbacks exceeding imposed time limits.
300+
The user agent <em>may</em> terminate a {{WorkletGlobalScope}} at any time it has no callbacks to
301+
handle or detects abnormal operation such as infinite loops and callbacks exceeding imposed time
302+
limits.
309303

310304
Issue(w3c/css-houdini-drafts#53): Worth adding dispose to classes to allow clean-up?
311305

312306
Processing Model {#processing-model}
313307
------------------------------------
314308

309+
Issue: Split this up.
310+
315311
The <a>invoke a method on a class inside a Worklet</a> has two hooks for algorithm steps that may be customized by any callers:
316312
- <dfn export>lookup a class instance on a worklet global scope</dfn>
317313

@@ -381,21 +377,21 @@ Loading scripts into a worklet. {#example-single}
381377
-------------------------------------------------
382378

383379
<pre class='lang-javascript'>
384-
window.fakeWorklet1.importScripts('script1.js');
385-
window.fakeWorklet1.importScripts('script2.js', 'script3.js');
380+
window.fakeWorklet1.import('script1.js');
381+
window.fakeWorklet1.import('script2.js');
386382

387383
// Assuming no other calls to fakeWorklet1 valid script loading orderings are:
388-
// 1. 'script1.js', 'script2.js', 'script3.js'
389-
// 2. 'script2.js', 'script3.js', 'script1.js'
384+
// 1. 'script1.js', 'script2.js'
385+
// 2. 'script2.js', 'script1.js'
390386
</pre>
391387

392388
Loading scripts into multiple worklets. {#example-multiple}
393389
-----------------------------------------------------------
394390

395391
<pre class='lang-javascript'>
396392
Promise.all([
397-
window.fakeWorklet1.importScripts('script1.js'),
398-
window.fakeWorklet2.importScripts('script2.js')
393+
window.fakeWorklet1.import('script1.js'),
394+
window.fakeWorklet2.import('script2.js')
399395
]).then(function() {
400396
// Both scripts now have loaded code, can do a task which relies on this.
401397
});
@@ -414,7 +410,7 @@ registerAnArbitaryClass('foo', class FooClass {
414410
</pre>
415411

416412
When the user agent wants to invoke <b>process</b> on an instance of class with key <b>foo</b> it will:
417-
1. Call <a>invoke a method on a class inside a {{Worklet}}</a> given <b>process</b> as the
413+
1. Call <a>invoke a method on a class inside a Worklet</a> given <b>process</b> as the
418414
|methodPropertyKey|, FakeWorkletGlobalScope as |workletGlobalScopeType| and some |arguments| with the following options:
419415
- To <a>lookup a class instance on a worklet global scope</a> given a |workletGlobalScope|, the user agent will:
420416
1. Let |classConstructor| be the result of looking up <b>foo</b> on the |workletGlobalScope|'s map of <b>registered class constructors</b>.

0 commit comments

Comments
 (0)