- From: Notifier <notifier@aries.w3.org>
- Date: Tue, 07 Jun 2016 08:17:05 +0000
- To: public-review-announce@w3.org
- Message-Id: <E1bACC9-0000ci-Tb@aries.w3.org>
Worklets Level 1 http://www.w3.org/TR/2016/WD-worklets-1-20160607/ Abstract This specification defines an API for running scripts in stages of the rendering pipeline independent of the main javascript execution environment. Status of the Document This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/. This document is a First Public Working Draft. Publication as a First Public Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress. The (archived) public mailing list public-houdini@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “worklets” in the subject, preferably like this: “[worklets] …summary of comment…” This document was jointly produced by the CSS Working Group and the Technical Architecture Group. This document was produced by groups operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures ( CSS, TAG) made in connection with the deliverables of the groups; those pages also include instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy. This document is governed by the 1 September 2015 W3C Process Document. Table of Contents 1 Introduction 1.1 Motivations 1.2 Code Idempotency 2 Infrastructure 2.1 The Global Scope 2.1.1 The event loop 2.1.2 Creating a WorkletGlobalScope 2.1.3 Script settings for worklets 2.2 Worklet 2.3 Lifetime of the Worklet 3 Security Considerations 4 Examples 4.1 Loading scripts into a worklet. 4.2 Loading scripts into multiple worklets. 4.3 Create a registered class and invoke a method. Conformance Document conventions Conformance classes Partial implementations Experimental implementations Non-experimental implementations Index Terms defined by this specification Terms defined by reference References Normative References Informative References IDL Index Issues Index1. Introduction 1.1. Motivations This section is not normative. Allowing extension points defined in the document's global scope is difficult, as rendering engines would need to abandon previously held assumptions for what could happen in the middle of a phase. For example, during the layout phase the rendering engine assues that no DOM will be modified. Additionally defining extension points in the document's global scope would restrict rendering engines to performing work in the same thread as the document's global scope. (Unless rendering engines added complex, high-overhead infrastructure to allow thread-safe APIs in addition to thread joining guarantees). The worklet is designed to allow such extension points in rendering engines, while keeping guarantees which rendering engines rely currently on. Worklets are similar to web workers however they: Are thread-agnostic. That is, they are not defined to run on a particular thread. Rendering engines may run them wherever they choose. Are able to have multiple duplicate instances of the global scope created for the purpose of parallelism. Are not event API based. Instead classes are registered on the global scope, whose methods are to be invoked by the user agent. Have a reduced API surface on the global scope. Have a lifetime for the global scope which is defined b subsequent specifications or user agents. They aren’t tied to the lifetime of the document. As worklets have a relatively high overhead, they should be used sparingly. Due to this worklets are expected to be shared between separate scripts. This is similar to the document's global scope. 1.2. Code Idempotency This section is not normative. Multiple instances of WorkletGlobalScope can be created for each Worklet that they belong to. User agents may choose to do this in order to parallelize work over multiple threads, or to move work between threads as required. Additionally different user agents may invoke a method on a class in a different order to other user agents. As a result of this, to prevent this compatibility risk between user agents, authors who register classes on the global scope should make their code idempotent. That is, a method or set of methods on a class should produce the same output given a particular input. The following techniques should be used in order to encourage authors to rite code in an idempotent way: No reference to the global object, e.g. self on a DedicatedWorkerGlobalScope. Code is loaded as a module script which resulting in the code being executed in strict mode code without a shared this. This prevents two different module scripts sharing state be referencing shared objects on the global scope. User agents may choose to always have at least two WorkletGlobalScopes per Worklet and randomly assign a method or set of methods on a class to a particular global scope. User agents may create and destroy WorkletGlobalScopes at any time. 2. Infrastructure 2.1. The Global Scope The WorkletGlobalScope object provides a worklet global scope which represents the global execution context of a Worklet. [Exposed=Worklet] interface WorkletGlobalScope { attribute Console console; }; A WorkletGlobalScope has an associated environment settings object. Note: The WorkletGlobalScope has a limited global scope when compared to a DedicatedWorkerGlobalScope. It is expected that other specifiations will extend WorkletGlobalScope with registerAClass methods which will allow authors to register classes for the user agent create and invoke methods on. 2.1.1. The event loop Each WorkletGlobalScope object has a distinct event loop. This event loop has no associated browsing context, and only its microtask queue is used (all other task queues are not used). The event loop is created by the create a WorkletGlobalScope algorithm. 2.1.2. Creating a WorkletGlobalScope When a user agent is to create a WorkletGlobalScope, for a given worklet, it must run the following steps: Let workletGlobalScopeType be the worklet’s worklet global scope type. Call the JavaScript InitializeHostDefinedRealm abstract operation with the following customizations: For the global object, create a new workletGlobalScopeType object. Let workletGlobalScope be the created object. Let realmExecutionContext be the created JavaScript execution context. Do not obtain any source texts for scripts or modules. Let settingsObject be the esult of set up a worklet environment settings object with realmExecutionContext. Associate the settingsObject with workletGlobalScope. For each resolvedModuleURL in the given worklet’s worklet’s resolved module URLs, run the following substeps: Let script be the result of fetch a module script tree given resolvedModuleURL, "anonymous" for the CORS setting attribute, and settingsObject. Note: Worklets follow web workers here in not allowing "use-credientials" for fetching resources. Run a module script given script. 2.1.3. Script settings for worklets When a user agent is to set up a worklet environment settings object, given a executionContext, it must run the following steps: Let inheritedResponsibleBrowsingContext be the responsible browsing context specified by the incumbent settings object. Let inheritedOrigin be the origin specified by the incumbent settings object. Let inheritedAPIBaseURL be the API base URL specified by the incumbent settings object. Let workletEventLoop be anewly created event loop. Let workletGlobalScope be executionContext’s global object. Let settingsObject be a new environment settings object whose algorithms are defined as follows: The realm execution context Return executionContext. The global object Return workletGlobalScope. The responsible browsing context Return inheritedResponsibleBrowsingContext. The responsible event loop Return workletEventLoop. The responsible document Not applicable (the responsible event loop is not a browsing context event loop). The API URL character encoding Return UTF-8. The API base URL Return inheritedAPIBaseURL. The origin Return inheritedOrigin. The creation URL Not applicable. The HTTPS state Return workletGlobalScope’s HTTPS state. Return settingsObject. Merge this with https://html.spec.whatwg.org/multipage/workers.html#set-up-a-worker-environment-settings-object 2.2. Worklet The Worklet object provides the capability to import module scripts into its associated WorkletGlobalScopes. The user agent can then creat classes registered on the WorkletGlobalScopes and invoke their methods. interface Worklet { [NewObject] Promise<void> import(DOMString moduleURL); }; A Worklet has a worklet global scope type. This is used for creating new WorkletGlobalScope and the type must inherit from WorkletGlobalScope. Note: As an example the worklet global scope type might be a PaintWorkletGlobalScope. A Worklet has a list of the worklet’s WorkletGlobalScopes. Initially this list is empty; it is populated when the user agent chooses to create its WorkletGlobalScope. A Worklet has a list of the worklet’s resolved module URLs. Initially this list is empty; it is populated when module scripts resolved. When the import(moduleURL) method is called on a Worklet object, the user agent must run the following steps: Let promise be a new promise. Run the following steps in parallel: Let resolvedModuleURL be the result of resolving the moduleURL relative to the API base URL specified by the entry settings object when the method was ivoked. If this fails, reject promise with a SyntaxError exception and abort these steps. Add resolvedModuleURL to the list of worklet’s resolved module URLs. Ensure that there is at least one WorkletGlobalScope in the worklet’s WorkletGlobalScopes. If not create a WorkletGlobalScope given the current Worklet. For each WorkletGlobalScope in the worklet’s WorkletGlobalScopes, run these substeps: Let settings be the WorkletGlobalScope's associated environment settings object. Let script be the result of fetch a module script tree given resolvedModuleURL, "anonymous" for the CORS setting attribute, and settings. Note: Worklets follow web workers here in not allowing "use-credientials" for fetching resources. Run a module script given script. If all the steps above succeeded (in particular, if all of the scripts parsed and loaded into the global scopes), resolve promise. Otherwise, reject promise. Note: Specifically, if a script fails to parse or fails to load over the network, it hould reject the promise; if the script throws an error while first evaluating the promise should resolve as a classes may have been registered correctly. Return promise. Need ability to load code into WorkletGlobalScope declaratively. <https://github.com/w3c/css-houdini-drafts/issues/47> 2.3. Lifetime of the Worklet The lifetime of a Worklet is tied to the object it belongs to, for example the Window. The lifetime of a WorkletGlobalScope should be defined by subsequent specifications which inherit from WorkletGlobalScope. Subsequent specifications may define that a WorkletGlobalScope can be terminated at any time particularly if there are no pending operations, or detects abnormal operation such as infinite loops and callbacks exceeding imposed time limits. 3. Security Considerations Need to decide if to allow worklets for unsecure context, etc. <https://github.com/w3c/css-houdini-drafts/issues/92> 4. Examples This section is not normative. For these examples we’ll use a fake worklet on windo. partial interface Window { [SameObject] readonly attribute Worklet fakeWorklet1; [SameObject] readonly attribute Worklet fakeWorklet2; }; callback Function = any (any... arguments); [Global=(Worklet,FakeWorklet),Exposed=FakeWorklet] interface FakeWorkletGlobalScope : WorkletGlobalScope { void registerAnArbitaryClass(DOMString type, Function classConstructor); }; Each FakeWorkletGlobalScope has a map of the registered class constructors map. When the registerAnArbitaryClass(type, classConstructor) method is called, the user agent will add the classConstructor of type to the map of registered class constructors map. 4.1. Loading scripts into a worklet. window.fakeWorklet1.import('script1.js'); window.fakeWorklet1.import('script2.js'); // Assuming no other calls to fakeWorklet1 valid script loading orderings are: // 1. 'script1.js', 'script2.js' // 2. 'script2.js', 'script1.js' 4.2. Loading scripts into multiple worklets. Promise.all([ window.fakeWoklet1.import('script1.js'), window.fakeWorklet2.import('script2.js') ]).then(function() { // Both scripts now have loaded code, can do a task which relies on this. }); 4.3. Create a registered class and invoke a method. // Inside FakeWorkletGlobalScope registerAnArbitaryClass('key', class FooClass { process(arg) { return !arg; } }); As an example, if the user agent wants to invoke "process" on a new class instance, the user agent could follow the following steps: Let workletGlobalScope be a FakeWorkletGlobalScope from the list of worklet’s WorkletGlobalScopes from the fake Worklet. The user agent may also create a WorkletGlobalScope given the fake Worklet and use that. Let classCtor be the result of performing a lookup in registered class constructors map with "key" as the key. Let classInstance be the result of Construct(classCtor). Let result be the result of Invoke(O=classInstance, P="process", Arguments=["true"]). Return result.ConfrmanceDocument conventions Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification. All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119] Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this: This is an example of an informative example. Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this: Note, this is an informative note. Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.Conformance classes Conformance to this specification is defined for three conformance classes:style sheet A CSS style sheet. renderer A UA that interprets the semantics of a style sheet and renders documents that use them. authoring tool A UA that writes a style sheet. A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module. A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.) An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.Partial implementations So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.Experimental implementations To avoid clashes with future CSS features, the CSS2.1 specification reserves a prefixed syntax for proprietary and experimental extensions to CSS. Prior to a specification reaching the Candidate Recommendation stage in the W3C process, all implementations of a CSS feature are considered experimental. The CSS Working Group recommends that implementations use a vendor-prefixed syntax for such features, including those in W3C Working Drafts. This avoids incompatibilities with future changes in the draft.Non-experimental implementations Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec. To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group. Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.IndexTerms defined by this specificationconsole, in §2.1 create a WorkletGlobalScope, in §2.1.2 fakeWorklet1, in §4 fakeWorklet2, in §4 FakeWorkletGlobalScope, in §4 import(moduleURL), in §2.2 registerAnArbitaryClass(type, classConstructor), in §4 registered class constructors map, in §4 set up a worklet environment settings object, in §2.1.3 Worklet, in §2.2 WorkletGlobalScope, in §2.1 worklet global scope, in §2.1 worklet global scope type, in §2.2 worklet’s resolved module URLs, in §2.2 worklet’s WorkletGlobalScopes, in §2.2Terms defined by reference[CONSOLE] defines the following terms: Console [css-paint-api-1] defines the following terms: PaintWorkletGlobalScope [HTML] defines the following terms: DedicatedWorkerGlobalScope Window [WebIDL-1] defies the following terms: FunctionReferencesNormative References[CONSOLE] Terin Stock; Robert Kowalski. Console Standard. Living Standard. URL: https://console.spec.whatwg.org/ [HTML] Ian Hickson. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/ [RFC2119] S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119 [WebIDL-1] Cameron McCormack; Boris Zbarsky. WebIDL Level 1. 8 March 2016. CR. URL: http://www.w3.org/TR/WebIDL-1/Informative References[CSS-PAINT-API-1] CSS Painting API Module Level 1 URL: https://drafts.css-houdini.org/css-paint-api-1/IDL Index[Exposed=Worklet] interface WorkletGlobalScope { attribute Console console; }; interface Worklet { [NewObject] Promise<void> import(DOMString moduleURL); }; partial interface Window { [SameObject] readonly attribute Worklet fakeWorklet1; [SameObject] readonly attribute Worklet fakeWorklet2; }; callback Function = any (any... arguments); [Gobal=(Worklet,FakeWorklet),Exposed=FakeWorklet] interface FakeWorkletGlobalScope : WorkletGlobalScope { void registerAnArbitaryClass(DOMString type, Function classConstructor); };Issues Index ↵ WorkletGlobalScope<https://github.com/w3c/css-houdini-drafts/issues/47>↵ <https://github.com/w3c/css-houdini-drafts/issues/92>↵#workletglobalscopeReferenced in: 1.2. Code Idempotency (2) (3) 2.1. The Global Scope (2) (3) (4) 2.1.1. The event loop 2.2. Worklet (2) (3) (4) (5) (6) (7) (8) (9) 2.3. Lifetime of the Worklet (2) (3) 4. Examples#create-a-workletglobalscopeReferenced in: 2.1.1. The event loop 2.2. Worklet 4.3. Create a registered class and invoke a method.#set-up-a-worklet-environment-settings-objectReferenced in: 2.1.2. Creating a WorkletGlobalScope#workletReferenced in: 1.2. Code Idempotency (2) 2.1. The Global Scope 2.2. Worklet (2) (3) (4) (5) (6) 2.3. Lifetime of the Worklet 4. Examples (2) (3) 4.3. Create a registered class and invoke a method. (2)#worklet-global-scope-typeReferenced in: 2.1.2. Creating a WorkletGlobalScope 2.2. Worklet#worklets-workletglobalscopesReferenced in: 2.2. Worklet (2) 4.3. Create a registered class and invoke a method.#worklets-resolved-module-urlsReferenced in: 2.1.2. Creating a WorkletGlobalScope 2.2. Worklet#dom-worklet-imporReferenced in: 2.2. Worklet#fakeworkletglobalscopeReferenced in: 4. Examples 4.3. Create a registered class and invoke a method.#registered-class-constructors-mapReferenced in: 4. Examples 4.3. Create a registered class and invoke a method.#dom-fakeworkletglobalscope-registeranarbitaryclassReferenced in: 4. Examples
Received on Tuesday, 7 June 2016 08:17:17 UTC