1. 序論
1.1. 動機
~INFORMATIVE`文書~環境@ †内に拡張~地点を定義できるようにするのは、困難である — 具現化~engineは、~phaseの途中で何が起こり得るかについて,~~従来の前提を放棄する必要があるので。 【†原文によるリンク先の定義(~HTML仕様)は、現在は廃されている。】 ◎ Allowing extension points defined in the document environment is difficult, as rendering engines would need to abandon previously held assumptions for what could happen in the middle of a phase.
例えば具現化~engineは、~layout~phaseの間は,~DOMは改変されないものと見做している。 ◎ For example, during the layout phase the rendering engine assumes that no DOM will be modified.
加えて、`文書~環境$内に拡張~地点を定義することは、具現化~engineが,`文書~環境$と同じ~thread内で作業を遂行することを制約する。 (具現化~engineが,~thread~joining保証【?】に加えて ~thread安全な~APIも許容するような,複雑, かつ~overheadの高い基盤を追加しない限り)。 ◎ Additionally defining extension points in the document environment would restrict rendering engines to performing work in the same thread as the document environment. (Unless rendering engines added complex, high-overhead infrastructure to allow thread-safe APIs in addition to thread joining guarantees).
~workletは、具現化~engineが現在~依拠しているものは保証し続けつつ,そのような拡張~地点を具現化~engine内に許容するよう設計されている。 ◎ The worklet is designed to allow such extension points in rendering engines, while keeping guarantees which rendering engines rely currently on.
~workletは`~web~worker$に類似するが: ◎ Worklets are similar to web workers however they:
- ~threadに非依存である。 すなわち、特定0の~thread上で走らすように定義されない。 具現化~engineは、それらを自身が選ぶどこで走らせても~MAY。 ◎ Are thread-agnostic. That is, they are not defined to run on a particular thread. Rendering engines may run them wherever they choose.
- 並列性の目的で、複数の[ 大域~scopeの~instance ]を重複して作成できる。 ◎ Are able to have multiple duplicate instances of the global scope created for the purpose of parallelism.
- ~event~APIに基づかない。 代わりに,いくつかの~classが大域~scopeに登録され、それらの~methodは,~UAにより呼出されることになる。 ◎ Are not event API based. Instead classes are registered on the global scope, whose methods are to be invoked by the user agent.
-
大域~scope上の~API~surfaceは、抑制される。
【 `WebIDL$r の用語で述べるならば、 `Exposed$ 拡張属性を通して`~worklet大域~scope$に公開される~interfaceは,他( ~workerや~window )に比して限られている。 】
◎ Have a reduced API surface on the global scope. - [ 後続の仕様や~UAにより定義される大域~scope ]用として,存続期間がある。 それらは、文書の存続期間に束ねられない。 ◎ Have a lifetime for the global scope which is defined by subsequent specifications or user agents. They aren’t tied to the lifetime of the document.
- ~overheadが他に比して高いので,控えめに利用されるべきである。 そのため、~workletは,別々の~script間で共有されるものと期待される。 このことは、`文書~環境$に類似する。 ◎ 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 environment.
1.2. ~codeの冪等性
~workletを利用する仕様 — 以下、この節を通して,単に %当該~仕様 と記す — のうち一部のもの(例: `css-paint-api-1$r )は、各 作業を[ 複数の~threadに並列化する / 必要に応じて別~threadへ移動する ]ことを,~UAに許容する。 ◎ Some specifications which use worklets ([css-paint-api-1]), allow user agents to parallelize work over multiple threads, or to move work between threads as required.
%当該~仕様 の下で~class上の~methodを呼出す順序は、複数の~UA間で異なるかもしれない。 ◎ In these specifications user agents might invoke methods on a class in a different order to other user agents.
その結果による~UA間の互換性~riskを防止するため、 %当該~仕様 の~APIを用いて大域~scopeに~classを登録する作者は,~codeを冪等にするべきである。 すなわち,ある~class上の[ 1 個以上の~methodが成す組 ]は、所与の特定0の入力に対し同じ出力を生産するべきある。 ◎ As a result of this, to prevent this compatibility risk between user agents, authors who register classes on the global scope using these APIs, 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.
次に挙げる技法は、 %当該~仕様 が,~codeを冪等な仕方で書くのを作者に促すために利用される: ◎ The following techniques are used in order to encourage authors to write code in an idempotent way:
- 大域~objへの参照-(例: `DedicatedWorkerGlobalScope$I 上の `self$m )は、持たないようにする。 ◎ No reference to the global object, e.g. self on a DedicatedWorkerGlobalScope.
- ~codeは、`~module~script$として読込むようにする — それにより,~codeは、 `this^jv を共有することなく,`~strict-mode$で実行される結果、大域~scope上の共用~objが 状態を共有している複数の~module~scriptから参照されるのは,防止される。 ◎ 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.
-
%当該~仕様 は、次を~UAに要求し~MUST:
- `Worklet$I ごとに 2 個~以上の `WorkletGlobalScope$I が常にあること。
- ある~class上の[ 1 個以上の~methodが成す組 ]を 特定0の大域~scopeに~randomにあてがうこと。
%当該~仕様 は、~memoryが拘束された下では,~opt-outを供せる。
◎ These specifications must require user agents 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. These specifications can provide an opt-out under memory constraints. - ~UAは、 %当該~仕様 が定義する `WorkletGlobalScope$I 【の拡張】を,いつでも作成でき, 破壊できる。 ◎ User agents can create and destroy WorkletGlobalScopes at any time for these specifications.
1.3. 投機的な評価
~workletを利用する一部の仕様(例: `css-paint-api-1$r )は、~UAの状態に基づいて~class上の~methodを呼出して~MAY。 複~thread間の同時並行性を高めるため、~UAは,未来にあり得る状態に基づいて,~methodを投機的に呼出して~MAY。 ◎ Some specifications which use worklets ([css-paint-api-1]) may invoke methods on a class based on the state of the user agent. To increase the concurrency between threads, a user agent may invoke a method speculatively, based on potential future states.
これらの仕様においては、~UAは,~class上の~methodを[ いつでも,および ~UAの現在の状態に対応するものに限らず 他の引数でも ]呼出して, ~cacheして~MAY — そのような投機的な評価の結果は,即時には表示されないが、~UAの状態が投機された状態に合致するとき,利用できる。 これは、~UAと~worklet~thread間の同時並行性を高めるであろう。 ◎ In these specifications user agents might invoke methods on a class at any time, and with any arguments, not just ones corresponding to the current state of the user agent. The results of such speculative evaluations are not displayed immediately, but may be cached for use if the user agent state matches the speculated state. This may increase the concurrency between the user agent and worklet threads.
この結果による,~UA間の互換性~riskを防止するため、これらの~APIを用いて大域~scopeに ~classを登録する作者は、自身の~codeを~statelessにするべきである。 すなわち,~methodを呼出すことによる効果は、その結果に限られるべきである — 変異-可能な状態を更新するような副作用は,一切伴わずに。 ◎ As a result of this, to prevent this compatibility risk between user agents, authors who register classes on the global scope using these APIs, should make their code stateless. That is, the only effect of invoking a method should be its result, not any side-effects such as updating mutable state.
~code冪等性を促す同じ技法は、~stateless~codeを書くことも,作者に促す。 ◎ The same techniques which encourage code idempotence also encourage authors to write stateless code.
2. 基盤
2.1. 大域~scope
`WorkletGlobalScope$I ~objは、 `~worklet大域~scope@ を供する。 それは、 `Worklet$I の大域~実行~文脈を表現する。 ◎ The WorkletGlobalScope object provides a worklet global scope which represents the global execution context of a Worklet.
【 ~workerのときと同様に,概念的には、[ `WorkletGlobalScope$I / `Worklet$I ]が,同じ~workletが[ 内側/外側 ]に “見せる顔” になる。 】
[`Exposed$=Worklet] interface `WorkletGlobalScope@I { };
各 `WorkletGlobalScope$I には、次のものが結付けられる:
- `所有者~文書@wL
- 初期~時には ~NULL とする。 それは、 `~WorkletGlobalScopeを作成する$~algoの内側で設定される。 ◎ Each WorkletGlobalScope has an assocated owner document. It is initially null and set inside the create a WorkletGlobalScope algorithm.
- `Document$I ~obj %文書 が【`属する閲覧文脈$が】`破棄され$るときは、 %文書 を`所有者~文書$wLとしているどの `WorkletGlobalScope$I に対しても,その`所有者~文書$wLは ~NULL にされるべきである。 ◎ Whenever a Document object is discarded, each WorkletGlobalScope whose owner document is that Document object, should clear its owner document.
- `環境~設定群~obj@wL
- `環境~設定群~obj$。 【`~workletの環境~設定群~objを設定しておく$ときに, `HostDefined^sl に格納される。】 ◎ Each WorkletGlobalScope has an associated environment settings object.
- `~module~map@wL
- `~module~map$。 初期~時には空とする。 ◎ Each WorkletGlobalScope has an associated module map. It is a module map, initially empty.
- `~worklet大域~scope実行~環境@wL
- この実行~環境は、並列的にしても(すなわち,別々の~thread等 — ~thread, ~process, その他の等価な構成子 — に分ける), それが属する `Worklet$I ~objが他と同じ~thread等に住まうようにしても~MAY。 どの~thread等に住まうかは、~UAが決める。 ◎ Each WorkletGlobalScope has a worklet global scope execution environment. This execution environment may be parallel (i.e. it may be on a separate thread, process, or other equivalent construct), or it may live on the same thread or process as the Worklet object it belongs to. Which thread or process it lives on is decided by the user agent.
注記: `WorkletGlobalScope$I は、 `DedicatedWorkerGlobalScope$I に比して制限された大域~scopeである。 他の仕様は、 `WorkletGlobalScope$I を拡張するものと期待される — [ ~UAが 作成して~methodを呼出すための~classを,作者が登録する ]のを許容する~methodを加えることにより(例えば `registerAClass()^m の様な名前の)。 ◎ Note: The WorkletGlobalScope has a limited global scope when compared to a DedicatedWorkerGlobalScope. It is expected that other specifications will extend WorkletGlobalScope with registerAClass methods which will allow authors to register classes for the user agent create and invoke methods on.
【この仕様において】`例外を報告する$所では、任意選択で,何もしないか, 開発者~consoleに例外を報告する。 ◎ When asked to report an exception, do nothing instead, or optionally report the exception to a developer console.
~HTMLによる`例外を報告する$は、 `EventTarget$I でない大域~objでも働けるよう,更新する必要がある。 <https://github.com/whatwg/html/issues/2611> ◎ HTML’s report an exception needs updating to work with non-EventTarget global objects. <https://github.com/whatwg/html/issues/2611>
2.1.1. ~event-loop
各 `WorkletGlobalScope$I ~objは、個別に`~event-loop$を持つ — この~event-loopは: ◎ Each WorkletGlobalScope object has a distinct event loop.\
- `閲覧文脈$には結付けられない。 ◎ This event loop has no associated browsing context.\
- `~WorkletGlobalScopeを作成する$~algoにより作成される。 ◎ The event loop is created by the create a WorkletGlobalScope algorithm.
- 上で定義した`~worklet大域~scope実行~環境$wL下で走る。 ◎ The event loop is run on the worklet global scope execution environment defined above.
- それを利用するのは、次に挙げるものに限られるものと予期されている ⇒# `addModule()$m に結付けられている~task / 作者により定義され,~UAが呼出す~callback / `小task$ ◎ It is expected that only tasks associated addModule(), the user agent invoking author defined callbacks, and microtasks will use this event loop.
注記: ~event-loop処理~model は,断続的に~loopするものと指定していても、実施上は,実装がそれを行うものとは期待されてはいない。 `小task~queue$は、作者により供された`~callback関数を呼出す$間に,空にされる。 ◎ Note: Even through the event loop processing model specifies that it loops continually, practically implementations aren’t expected to do this. The microtask queue is emptied while invoking callback functions provided by the author.
2.1.2. `WorkletGlobalScope^I の作成-法
~UAは `~WorkletGlobalScopeを作成する@ ときは、所与の ⇒# %~worklet大域~scope型, %~module応答~map, %外側~設定群, %資格証~options ◎終 に対し,次の手続きを走らせ~MUST: ◎ When a user agent is to create a WorkletGlobalScope, given workletGlobalScopeType, moduleResponsesMap, and outsideSettings, it must run the following steps:
- 新たな`~worklet大域~scope実行~環境$wLを作成する — この手続きの以降は、その文脈の下で走らす ◎ Create the worklet global scope execution environment and run the rest of these steps in that context.
- %~worklet大域~scope ~LET 型 %~worklet大域~scope型 の新たな~obj ◎ ↓
- %~realm実行~文脈 ~LET [ 大域~objに %~worklet大域~scope を用いるように~custom化する ]下で,[ ~JS `InitializeHostDefinedRealm$jA 抽象~演算 ]を~callして作成される~JS実行~文脈 ◎ 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.
- %内側~設定群 ~LET `~workletの環境~設定群~objを設定しておく$( %~realm実行~文脈, %外側~設定群 ) ◎ Let insideSettings be the result of set up a worklet environment settings object given realmExecutionContext, and outsideSettings.
- %内側~設定群 と %~worklet大域~scope を結付ける ◎ Associate the insideSettings with workletGlobalScope.
- %~worklet大域~scope の`所有者~文書$wL ~SET %外側~設定群 の`担当の文書$enV ◎ Set workletGlobalScope’s owner document to outsideSettings’s responsible document.
- `大域~objの~CSP~listを初期化する$( %~worklet大域~scope, † ) 【† 何らかの応答も引数に必要だが、言及されていない】 ◎ Invoke the initialize a global object’s CSP list algorithm given workletGlobalScope.
-
%~module応答~map 内の~EACH( %~entry ) に対し: ◎ For each entry in the given moduleResponsesMap (in insertion order), run the following substeps:
- %~module~URL~record ~LET %~entry の~key ◎ Let moduleURLRecord be entry’s key.
- %~script ~LET 次を走らせて`非同期に完了する$まで待機した結果の`~module~script$ ⇒ `~worklet~scriptを~fetchする$( %~module~URL~record, %~module応答~map, %資格証~options, %外側~設定群, %内側~設定群 ) ◎ Let script be the result of fetch a worklet script given moduleURLRecord, moduleResponsesMap, outsideSettings, and insideSettings when it asynchronously completes.
- `~module~scriptを走らす$( %~script ) ◎ Run a module script given script.
注記: この段において`~worklet~scriptを~fetchする$ときには、実際に~network要請が遂行されることはない — 対する応答は、~workletの`~module応答~map$wL内に~cache済みなので。 また、構文解析~errorにもならない — この時点では、別の~worklet大域~scopeにより成功裡に構文解析されているはずなので。 すなわち、ここで %~script が ~NULL になることは,決してないはずである。 ◎ Note: Fetch a worklet script won’t actually perform a network request as it will hit the worklet’s module responses map. It also won’t have a parsing error as at this point it should have successfully been parsed by another worklet global scope. I.e. script should never be null here.
- %内側~設定群 により指定される`担当の~event-loop$enVを走らす ◎ Run the responsible event loop specified by insideSettings.
2.1.3. ~worklet用の~script設定群
`~workletの環境~設定群~objを設定しておく@ ときは、所与の ( %実行~文脈, %外側~設定群 ) に対し,次の手続きを走らせ~MUST: ◎ When a user agent is to set up a worklet environment settings object, given a executionContext, and outsideSettings, it must run the following steps:
- %継承される担当の閲覧文脈 ~LET %外側~設定群 の`担当の閲覧文脈$enV ◎ Let inheritedResponsibleBrowsingContext be outsideSettings’s responsible browsing context.
- %継承される~API用~基底~URL ~LET %外側~設定群 の`~API用~基底~URL$enV ◎ Let inheritedAPIBaseURL be outsideSettings’s API base URL.
- %生成元 ~LET 一意かつ`不透明な生成元$ ◎ Let origin be a unique opaque origin.
- %継承される~HTTPS状態 ~LET %外側~設定群 の`~HTTPS状態$enV ◎ Let inheritedHTTPSState be outsideSettings’s HTTPS state.
- %継承される~referrer施策 ~LET %外側~設定群 の`~referrer施策$enV ◎ Let inheritedReferrerPolicy be outsideSettings’s referrer policy.
- %~worklet~event-loop ~LET 新たな`~event-loop$ ◎ Let workletEventLoop be a newly created event loop.
- %~realm ~LET %実行~文脈 の~Realm成分の値 ◎ Let realm be the value of executionContext’s Realm component.
- %~worklet大域~scope ~LET %~realm の`大域~obj$enV ◎ Let workletGlobalScope be realm’s global object.
-
%設定群~obj ~LET 各種~algoが次のように定義される,新たな`環境~設定群~obj$: ◎ Let settingsObject be a new environment settings object whose algorithms are defined as follows:
- `~realm実行~文脈$enV ⇒ ~RET %実行~文脈 ◎ The realm execution context • Return executionContext.
- `~module~map$enV ⇒ ~RET %~worklet大域~scope の`~module~map$wL ◎ The module map. • Return workletGlobalScope’s module map.
- `担当の閲覧文脈$enV ⇒ ~RET %継承される担当の閲覧文脈 ◎ The responsible browsing context • Return inheritedResponsibleBrowsingContext.
- `担当の~event-loop$enV ⇒ ~RET %~worklet~event-loop ◎ The responsible event loop • Return workletEventLoop.
- `担当の文書$enV ⇒ 適用-可能でない(`担当の~event-loop$enVは、`閲覧文脈~event-loop$でない) ◎ The responsible document • Not applicable (the responsible event loop is not a browsing context event loop).
- `~API用~URL文字~符号化法$enV ⇒ ~RET `UTF-8$ ◎ The API URL character encoding • Return UTF-8.
- `~API用~基底~URL$enV ⇒ ~RET %継承される~API用~基底~URL ◎ The API base URL • Return inheritedAPIBaseURL.
- `生成元$enV ⇒ ~RET %生成元 ◎ The origin • Return origin.
- `~HTTPS状態$enV ⇒ ~RET %継承される~HTTPS状態 ◎ The HTTPS state • Return inheritedHTTPSState.
- `~referrer施策$enV ⇒ ~RET %継承される~referrer施策 ◎ The referrer policy • Return inheritedReferrerPolicy.
- %設定群~obj の ⇒# `~id$enV ~SET 新たな一意かつ不透明な文字列, `作成時の~URL$enV ~SET %継承される~API用~基底~URL, `~target閲覧文脈$enV ~SET ~NULL , `作動中の~service-worker$enV ~SET ~NULL ◎ Set settingsObject’s id to a new unique opaque string, settingsObject’s creation URL to inheritedAPIBaseURL, settingsObject’s target browsing context to null, and settingsObject’s active service worker to null.
- %~realm の `HostDefined^sl ~field ~SET %設定群~obj ◎ Set realm’s [[HostDefined]] field to settingsObject.
- ~RET %設定群~obj ◎ Return settingsObject.
これを,`~workerの環境~設定群~objを設定しておく$~algoと併合する。 ◎ Merge this with https://html.spec.whatwg.org/multipage/workers.html#set-up-a-worker-environment-settings-object
2.2. ~worklet
`Worklet$I ~objは、 結付けられている `WorkletGlobalScope$I の中に, 1 個~以上の~module~scriptを追加する能力を供する。 ~UAは、 `WorkletGlobalScope$I に登録された各~classを作成して,それらの~methodを呼出せるようになる。 ◎ The Worklet object provides the capability to add module scripts into its associated WorkletGlobalScopes. The user agent can then create classes registered on the WorkletGlobalScopes and invoke their methods.
interface `Worklet@I { [`NewObject$] Promise<void> `addModule$m( USVString %moduleURL, optional `WorkletOptions$I %options ); }; dictionary `WorkletOptions@I { `RequestCredentials$I `credentials@m = "same-origin"; };
各 `Worklet$I は、次のものを持つ:
- `~worklet大域~scope型@wL
- `WorkletGlobalScope$I を`継承-$する~interface型(例: `PaintWorkletGlobalScope$I )。 新たな `WorkletGlobalScope$I ~objを作成するときに利用される。 ◎ 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.
- `~WorkletGlobalScope~list@wL
- 初期~時には空とする。 この~listは、~UAが新たな `WorkletGlobalScope$I ~objを作成することを選んだときに拡充される。 ◎ 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.
- `~module応答~map@wL
- `有順序~map$。 その各~entryは、~module~URLを`~fetch$応答に対応付ける。 ~entryたちの順序は挿入-順になる。 この~mapへの~accessは、~thread安全になるべきある。 ◎ A Worklet has a module responses map. This is a ordered map of module URLs to values that are a fetch responses. The map’s entries are ordered based on their insertion order. Access to this map should be thread-safe.
- この~mapは、異なる時点に作成される `WorkletGlobalScope$I が[ 同じ~script~source~textの集合を包含し,挙動が同じになる ]ことを確保するために存在する。 追加の `WorkletGlobalScope$I ~objの作成は、作者からは透過的になるべきである。 ◎ The module responses map exists to ensure that WorkletGlobalScopes created at different times contain the same set of script source text and have the same behaviour. The creation of additional WorkletGlobalScopes should be transparent to the author.
-
注記: 実施上は、 `addModule()$m の~algoを実装する際に~thread安全な~mapを利用することは,~UAに期待されていない。 ~UAは代わりに,その~methodが~callされたとき、~main~thread上で~module~graphを~fetchして,~fetchされた~source(`~module応答~map$wL内に包含される~data)を `WorkletGlobalScope$I を有する各~threadに送信できる。 ◎ Practically user agents aren’t expected to implement the following algorithm using a thread-safe map. Instead when addModule() is called user agents can fetch the module graph on the main thread, and send the fetched sources (the data contained in the module responses map) to each thread which has a WorkletGlobalScope.
~UAは,新たな `WorkletGlobalScope$I を作成したいと望む場合には、単純に[ ~main~threadから~fetchされたすべての~sourceからなる~list ]を[ `WorkletGlobalScope$I を所有している~thread ]へ送信できる。 ◎ If the user agent wishes to create a new WorkletGlobalScope it can simply sent the list of all fetched sources from the main thread to the thread which owns the WorkletGlobalScope.
`処理待ち~task構造体@ は、次のものからなる`構造体$である: ◎ A pending tasks struct is a struct consisting of:
- `計数@ — これは、下の~algoにより利用される。 【これ一つしかないのに構造体で包装している理由は、整数~値をとる計数を参照として渡せるようにするため。】 ◎ A counter. ◎ This is used by the algorithms below.
`Worklet$I ~objの `addModule(moduleURL, options)@m ~methodの被呼出時には、次を走らせ~MUST: ◎ When the addModule(moduleURL, options) method is called on a Worklet object, the user agent must run the following steps:
- %~promise ~LET `新たな~promise$ ◎ Let promise be a new promise. ◎ Let worklet be this Worklet.
- %外側~設定群 ~LET 此れに`関連する設定群~obj$ ◎ Let outsideSettings be the relevant settings object of this.
- %~module~URL~record ~LET %moduleURL を %外側~設定群 に`相対的に構文解析-$した`結果の~URL~record$ ◎ Let moduleURLRecord be the result of parsing the moduleURL argument relative to outsideSettings.
- ~IF[ %~module~URL~record ~EQ `失敗^i ] ⇒# `SyntaxError$E 例外で~promiseを却下する; ~RET %~promise ◎ If moduleURLRecord is failure, then reject promise with a "SyntaxError" DOMException and return promise.
- ~RET %~promise — ただし、この~algoは`並列的$に継続する ◎ Return promise, and then continue running this algorithm in parallel.
- %資格証~options ~LET %options の `credentials$m ~member ◎ Let credentialOptions be the credentials member of options.
- %~module応答~map ~LET 此れの`~module応答~map$wL ◎ Let moduleResponsesMap be worklet’s module responses map.
- %~worklet大域~scope型 ~LET 此れの`~worklet大域~scope型$wL ◎ Let workletGlobalScopeType be worklet’s worklet global scope type.
- %大域~scope~list ~LET 此れの`~WorkletGlobalScope~list$wL ◎ ↓
-
~IF[ %大域~scope~list は空である ]: ◎ If the worklet’s WorkletGlobalScopes is empty, run the following steps:
- %O ~LET 次を走らせて`非同期に完了する$まで待機した結果 ⇒ `~WorkletGlobalScopeを作成する$( %~worklet大域~scope型, %~module応答~map, %外側~設定群, %資格証~options ) ◎ Create a WorkletGlobalScope given workletGlobalScopeType, moduleResponsesMap, and outsideSettings.
- %大域~scope~list に %O を追加する ◎ Add the WorkletGlobalScope to worklet’s WorkletGlobalScopes.
~UAは、~workletの型に依存して,この時点で追加の `WorkletGlobalScope$I を作成しても~MAY。 ◎ Depending on the type of worklet the user agent may create additional WorkletGlobalScopes at this time.
注記: 特定的には、 `css-paint-api-1$r は 複数の大域~scopeを許容する一方で, `webaudio$r ~APIは許容しない。 ◎ Note: Specifically the [css-paint-api-1] allows for multiple global scopes, while the [webaudio] API does not. ◎ ↑↑Wait for this step to complete before continuing.
- %処理待ち~task構造体 ~LET 次のように初期化された,新たな`処理待ち~task構造体$ ⇒ `計数$ ~SET `~WorkletGlobalScope~list$wLの長さ ◎ Let pendingTaskStruct be a new pending tasks struct with counter initialized to the length of worklet’s WorkletGlobalScopes.
- `~WorkletGlobalScope~list$wL内の~EACH( %~worklet大域~scope ) に対し ⇒ %~worklet大域~scope 【の~event-loop】 上に,次を走らす`~taskを~queueする$ ⇒ `~worklet~scriptを~fetchして呼出す$( %~worklet大域~scope, %~module~URL~record, %~module応答~map, %資格証~options, %外側~設定群, %処理待ち~task構造体, %~promise ) ◎ For each workletGlobalScope in the worklet’s WorkletGlobalScopes, queue a task on the workletGlobalScope to fetch and invoke a worklet script given workletGlobalScope, moduleURLRecord, moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct, and promise.
注記: %~promise は、`~worklet~scriptを~fetchして呼出す$~algoの中で[ 却下- / 解決- ]される。 ◎ Note: The rejecting and resolving of the promise occurs within the fetch and invoke a worklet script algorithm.
`~worklet~scriptを~fetchして呼出す@ ときは、所与の ⇒# %~worklet大域~scope, %~module~URL~record, %~module応答~map, %資格証~options, %外側~設定群, %処理待ち~task構造体, %~promise ◎終 に対し,次の手続きを走らせ~MUST: ◎ When the user agent is to fetch and invoke a worklet script given workletGlobalScope, moduleURLRecord, moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct, and promise, the user agent must run the following steps:
注記: この~algoは、`~worklet大域~scope実行~環境$wLの中で走ることになる。 ◎ Note: This algorithm is to be run within the worklet global scope execution environment.
- %内側~設定群 ~LET %~worklet大域~scope の`環境~設定群~obj$wL ◎ Let insideSettings be the workletGlobalScope’s associated environment settings object.
- %~script ~LET 次を走らせて`非同期に完了する$まで待機した結果 ⇒ `~worklet~scriptを~fetchする$( %~module~URL~record, %~module応答~map, %資格証~options, %外側~設定群, %内側~設定群 ) ◎ Let script by the result of fetch a worklet script given moduleURLRecord, moduleResponsesMap, credentialOptions, outsideSettings, and insideSettings when it asynchronously completes.
-
~IF[ %~script ~EQ ~NULL ] ⇒ %外側~設定群 の`担当の~event-loop$enV上に,次を走らす`~taskを~queueする$: ◎ If script is null, then queue a task on outsideSettings’s responsible event loop to run these steps:
-
~IF[ %処理待ち~task構造体 の`計数$ ~NEQ −1 ]: ◎ If pendingTaskStruct’s counter is not -1, then run these steps:
- %処理待ち~task構造体 の`計数$ ~SET −1 ◎ Set pendingTaskStruct’s counter to -1.
- `AbortError$E 例外で %~promise を却下する ◎ Reject promise with an "AbortError" DOMException.
-
- `~module~scriptを走らす$( %~script ) ◎ Run a module script given script.
-
%外側~設定群 の`担当の~event-loop$enV上に,次を走らす`~taskを~queueする$: ◎ Queue a task on outsideSettings’s responsible event loop to run these steps:
-
~IF[ %処理待ち~task構造体 の`計数$ ~NEQ −1 ]: ◎ If pendingTaskStruct’s counter is not -1, then run these steps:
- %処理待ち~task構造体 の`計数$ ~DECBY 1 ◎ Decrement pendingTaskStruct’s counter by 1.
- ~IF[ %処理待ち~task構造体 の`計数$ ~EQ 0 ] ⇒ %~promise を解決する 【何で解決するか述べられていない】 ◎ If pendingTaskStruct’s counter is 0, then resolve promise.
-
`~worklet~scriptを~fetchする@ ときは、所与の ⇒# %~module~URL~record, %~module応答~map, %資格証~options, %外側~設定群, %内側~設定群 ◎終 に対し,次を走らせ~MUST: ◎ When the user agent is to fetch a worklet script given moduleURLRecord, moduleResponsesMap, credentialOptions, outsideSettings, and insideSettings, the user agent must run the following steps:
注記: この~algoは、`~worklet大域~scope実行~環境$wLの中で走ることになる。 ◎ Note: This algorithm is to be run within the worklet global scope execution environment.
-
%~fetch ~LET 次を走らす手続き ⇒ `~worker用~module~script~graphを~fetchする$( %~module~URL~record, %外側~設定群, `script^l, %資格証~options, %内側~設定群 ) — 加えて、次に与える`~fetchを遂行する$手続きも渡す ◎ Fetch a module worker script graph given moduleURLRecord, outsideSettings, "script", credentialOptions, and insideSettings.
手続きは、所与の ( %要請 ) に対し,次を遂行する: ◎ To perform the fetch given request, perform the following steps:
- %~cache ~LET %~module応答~map ◎ Let cache be the moduleResponsesMap.
- %~url ~LET %要請 の`~url$rq ◎ Let url be request’s url.
- ~IF[ %~cache[ %~url ] ~EQ `fetching^l ] ⇒ %~cache[ %~url ] が変化するまで待機する ◎ If cache contains an entry with key url whose value is "fetching", wait until that entry’s value changes, then proceed to the next step.
- ~IF[ %~cache[ %~url ] ~NEQ ε ] ⇒ %~cache[ %~url ] を結果として,`非同期に完了する$ ◎ If cache contains an entry with key url, asynchronously complete this algorithm with that entry’s value, and abort these steps.
- %~cache[ %~url ] ~SET `fetching^l ◎ Create an entry in cache with key url and value "fetching".
- %応答 ~LET 次を走らせて`非同期に完了する$まで待機した結果 ⇒ %要請 を`~fetch$する ◎ Fetch request. ◎ Let response be the result of fetch when it asynchronously completes.
- %~cache[ %~url ] ~SET %応答 ◎ Set the value of the entry in cache whose key is url to response, and\
- %応答 を結果として`非同期に完了する$ ◎ asynchronously complete this algorithm with response.
- ~RET %~fetch を遂行して,`非同期に完了する$まで待機した結果 ◎ Return the result of fetch a module worker script graph when it asynchronously completes.
注記: 特定的には、[ ~scriptを構文解析する / ~network越しに読込む ]のに失敗した場合,~promiseは却下されることになる。 ~promiseを最初に評価する間に,~scriptが~errorを投出した場合、解決されることになる — それが解決される値を与える~classが、正しく登録されていたなら。 ◎ Note: Specifically, if a script fails to parse or fails to load over the network, it will reject the promise. If the script throws an error while first evaluating the promise it will resolve as a classes may have been registered correctly.
作者が `Worklet$I の中に~codeを追加するとき、その~codeは,複数の `WorkletGlobalScope$I 上で走ることにもなり得る。 例えば: ◎ When an author adds code into a Worklet the code may run against multiple WorkletGlobalScopes, for example:
// script.js console.log('Hello from a WorkletGlobalScope!');
// main.js await CSS.paintWorklet.addModule('script.js');
~UAは、舞台裏では, `script.js^c を 4 個の大域~scopeの中に読込んで~MAY — その事例では、~UAの~debug用~toolは,次を~printすることになる: ◎ Behind the scenes the user agent may load the script.js into 4 global scopes, in which case the debugging tools for the user agent would print:
[paintWorklet#1] Hello from a WorkletGlobalScope! [paintWorklet#4] Hello from a WorkletGlobalScope! [paintWorklet#2] Hello from a WorkletGlobalScope! [paintWorklet#3] Hello from a WorkletGlobalScope!
~UAが,この例の `WorkletGlobalScope$I #3 を~killして再開始することにした場合、再開始-時に,~debug用~tool内に再度 `[paintWorklet#3] Hello from a WorkletGlobalScope!^c を~printすることになる。 ◎ If the user agent decided to kill and restart a WorkletGlobalScope number 3 in this example, it would print [paintWorklet#3] Hello from a WorkletGlobalScope! again in the debugging tools when this occurs.
`WorkletGlobalScope$I の中に ~codeを宣言的に読込む能が必要。 <https://github.com/w3c/css-houdini-drafts/issues/47> ◎ Need ability to load code into a WorkletGlobalScope declaratively. <https://github.com/w3c/css-houdini-drafts/issues/47>
2.3. ~workletの存続期間
`Worklet$I の存続期間は、それが属する~obj — 例えば `Window$I — に束ねられる。 ◎ The lifetime of a Worklet is tied to the object it belongs to, for example the Window.
`WorkletGlobalScope$I を継承する~interface %A を定義する仕様は: ◎ ↓
- %A の存続期間を定義するべきである。 ◎ The lifetime of a WorkletGlobalScope should be defined by subsequent specifications which inherit from WorkletGlobalScope.
- %A を いつでも終了できるものと定義して~MAY — 特に、処理待ち演算が無い場合や, [ 課される制限-時間を超過するような,無限~loopや~callback ]などの異常な演算が検出された場合には。 ◎ 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上の考慮点
~workletを~secureでない文脈にも許容するかどうか, 等々を決める必要がある <https://github.com/w3c/css-houdini-drafts/issues/92> ◎ Need to decide if to allow worklets for unsecure context, etc. <https://github.com/w3c/css-houdini-drafts/issues/92>
4. 例
~INFORMATIVE以下の例では、~window上で `FakeWorklet^I (“~fakeの” ~worklet )を利用する。 ◎ For these examples we’ll use a fake worklet on window.
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$I { void `registerAnArbitaryClass^m(DOMString %type, Function %classConstructor); };
各 `FakeWorkletGlobalScope^I は、 `登録-済み~class構築子~map@ を持つ。 ◎ Each FakeWorkletGlobalScope has a map of the registered class constructors map.
`registerAnArbitaryClass(type, classConstructor)^m ~methodが~callされたとき、~UAは,`登録-済み~class構築子~map$に型 %type の %classConstructor を追加することになる。 ◎ 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. ~workletの中へ~scriptを読込むとき
window.fakeWorklet1.addModule('script1.js');
window.fakeWorklet1.addModule('script2.js');
/*
他からは `fakeWorklet1^c は~callされていないとするなら、次のいずれも,~scriptを読込む妥当な順序になる:
◎
Assuming no other calls to fakeWorklet1 valid script loading orderings are:
*/
// 1. 'script1.js', 'script2.js'
// 2. 'script2.js', 'script1.js'
4.2. 複数の~workletの中へ~scriptを読込むとき
Promise.all([
window.fakeWorklet1.addModule('script1.js'),
window.fakeWorklet2.addModule('script2.js')
]).then(function() {
/*
両~scriptの~codeとも読込まれたので,ここで それらに依拠する~taskを行える。
◎
Both scripts now have loaded code, can do a task which relies on this.
*/
});
4.3. 登録-済み~classを作成して~methodを呼出す
/*
`FakeWorkletGlobalScope^I の内側
◎
Inside FakeWorkletGlobalScope
*/
registerAnArbitaryClass('key', class FooClass {
process(%arg) {
return !%arg;
}
});
例として,~UAが 新たな~class~instance上で `process^m を呼出したいと求めるなら、次の手続きに従うこともできる ◎ As an example, if the user agent wants to invoke "process" on a new class instance, the user agent could follow the following steps:
-
%~worklet大域~scope ~LET 当の~fake `Worklet$I の`~WorkletGlobalScope~list$wL からの `FakeWorkletGlobalScope^I 【複数あり得るので、~UAが適切などれかを選ぶことになる。】 ◎ Let workletGlobalScope be a FakeWorkletGlobalScope from the list of worklet’s WorkletGlobalScopes from the fake Worklet.
~UAは、次を行った上で,その結果を利用してもよい ⇒ ~fake `Worklet$I を与える下で `~WorkletGlobalScopeを作成する$ ◎ The user agent may also create a WorkletGlobalScope given the fake Worklet and use that.
- %~class構築子 ~LET `登録-済み~class構築子~map$[ `key^l ] ◎ Let classCtor be the result of performing a lookup in registered class constructors map with "key" as the key.
- %~class~instance ~LET `Construct$jA( %~class構築子 ) ◎ Let classInstance be the result of Construct(classCtor).
- %結果 ~LET `Invoke$jA( %~class~instance, `process^l, « `true^l » ) ◎ Let result be the result of Invoke(O=classInstance, P="process", Arguments=["true"]).
- ~RET %結果 ◎ Return result.