回答編集履歴
7
まとめを修正した。
answer
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
仕様のどの部分によるものか、私も気になっていましたので、
|
19
19
|
この機会に調べてみました。
|
20
20
|
|
21
|
-
まとめると、
|
21
|
+
仕様にあること、無いことをまとめると、以下の通りです。
|
22
22
|
|
23
23
|
**ECMAScript 6**
|
24
24
|
・[[Job]]を要素とするキューを持っており、キュー内の[[Job]]を順次実行していく。
|
6
まとめを追記した。
answer
CHANGED
@@ -18,11 +18,24 @@
|
|
18
18
|
仕様のどの部分によるものか、私も気になっていましたので、
|
19
19
|
この機会に調べてみました。
|
20
20
|
|
21
|
+
まとめると、
|
22
|
+
|
23
|
+
**ECMAScript 6**
|
24
|
+
・[[Job]]を要素とするキューを持っており、キュー内の[[Job]]を順次実行していく。
|
25
|
+
・キューの割り込み処理は発生しうるが、[[Job]] の実行中は割り込みされない。
|
26
|
+
**ECMAScript 5.1**
|
27
|
+
・実行コンテキストのスタックがあり、スタックのトップにある実行コンテキストを使って処理が実行される。
|
28
|
+
・キューや割り込みについては記述が無い。
|
29
|
+
・スタックを新しく作るような記述が無いことから、シングルスレッドと推定できる。
|
30
|
+
**WHATWG仕様**
|
31
|
+
・イベント実行中は一つの [[Job]] と考えられ、イベント実行中は割り込み処理されない(setTimeout も同様)
|
32
|
+
|
33
|
+
|
21
|
-
そもそも、HTML5にはWeb Workerがあるので、
|
34
|
+
そもそも、HTML5にはWeb Workerがあるので、シングルスレッドというのは正確ではないと思います。
|
22
|
-
シングルスレッドというのは正確ではないと思います。
|
23
35
|
ただ、Web Workerからはwindowオブジェクトは参照できない様なので
|
24
36
|
以降の記述では触れません。
|
25
37
|
|
38
|
+
### ECMAScript 6仕様から分かること
|
26
39
|
ECMAScript 6の仕様には、シングルスレッドでの動作しか書かれていません。
|
27
40
|
以下の箇所が該当すると思います。
|
28
41
|
[8.4 Jobs and Job Queues](http://www.ecma-international.org/ecma-262/6.0/#sec-jobs-and-job-queues)
|
@@ -33,16 +46,16 @@
|
|
33
46
|
一応、初期化部分を見ると、
|
34
47
|
[8.5 ECMAScript Initialization()](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-initialization)
|
35
48
|
> 8. In an implementation dependent manner, obtain the ECMAScript source texts (see clause 10) for zero or more ECMAScript scripts and/or ECMAScript modules.
|
36
|
-
>
|
49
|
+
> For each such sourceText do,
|
37
|
-
>
|
50
|
+
> a. If sourceText is the source code of a script, then
|
38
|
-
>
|
51
|
+
> i. Perform EnqueueJob("ScriptJobs", ScriptEvaluationJob, « sourceText »).
|
39
|
-
>
|
52
|
+
> b. Else sourceText is the source code of a module,
|
40
|
-
>
|
53
|
+
> i. Perform EnqueueJob("ScriptJobs", TopLevelModuleEvaluationJob, « sourceText »).
|
41
54
|
のように、JobをEnqueueすることで処理を開始しており、その後の処理も
|
42
55
|
Jobの実行によって行われると解釈できると思います。
|
43
56
|
|
44
|
-
|
57
|
+
###WHATWGの仕様から分かること
|
45
|
-
以下のようにイベントループの説明があります。
|
58
|
+
WHATWGの仕様を見ても、以下のようにイベントループの説明があります。
|
46
59
|
[8.1.4 Event loops](https://html.spec.whatwg.org/multipage/webappapis.html#event-loops)
|
47
60
|
taskのキューがあって、それをイベントループが処理していくというような記述です。
|
48
61
|
> There must be at least one browsing context event loop per user agent, and at most one per unit of related similar-origin browsing contexts.
|
@@ -50,7 +63,7 @@
|
|
50
63
|
|
51
64
|
1つのイベントループは1スレッドかどうか、ということになると思いますが、
|
52
65
|
イベントループの[処理モデル](https://html.spec.whatwg.org/multipage/webappapis.html#processing-model-8)を見ても、
|
53
|
-
>
|
66
|
+
> An event loop must continually run through the following steps for as long as it exists:
|
54
67
|
の後に順番に処理するステップが書かれているだけで、マルチスレッドで処理するとは書かれていません。
|
55
68
|
|
56
69
|
また、
|
@@ -101,3 +114,4 @@
|
|
101
114
|
の
|
102
115
|
> Concurrency, Asynchrony, and Distributed Programming
|
103
116
|
あたりが元のようで、ES6より前には仕様化されていなかったように見えます。
|
117
|
+
|
5
ECMAScript 6より前に、Jobのキューが仕様に無いことを追記した。
answer
CHANGED
@@ -32,8 +32,8 @@
|
|
32
32
|
|
33
33
|
一応、初期化部分を見ると、
|
34
34
|
[8.5 ECMAScript Initialization()](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-initialization)
|
35
|
-
> 8. In an implementation dependent manner, obtain the ECMAScript source texts (see clause 10)
|
35
|
+
> 8. In an implementation dependent manner, obtain the ECMAScript source texts (see clause 10) for zero or more ECMAScript scripts and/or ECMAScript modules.
|
36
|
-
>
|
36
|
+
> For each such sourceText do,
|
37
37
|
> a. If sourceText is the source code of a script, then
|
38
38
|
> i. Perform EnqueueJob("ScriptJobs", ScriptEvaluationJob, « sourceText »).
|
39
39
|
> b. Else sourceText is the source code of a module,
|
@@ -72,6 +72,8 @@
|
|
72
72
|
### ECMAScript 6より前について
|
73
73
|
ECMAScript 5.1の仕様には、**シングルスレッドで処理する**ことについて、
|
74
74
|
はっきりとした記述が無い様に思います。
|
75
|
+
Jobのキューについても記述がありません。
|
76
|
+
|
75
77
|
仕様に書かれているのは、
|
76
78
|
「実行コンテキストのスタックがあり、スタックのトップにある
|
77
79
|
実行コンテキストを使って処理が実行される。」
|
4
日本語訳部分を見易く整形した。
answer
CHANGED
@@ -85,13 +85,9 @@
|
|
85
85
|
|
86
86
|
[10.3 Execution Contexts](http://www.ecma-international.org/ecma-262/5.1/#sec-10.3)
|
87
87
|
> When control is transferred to ECMAScript executable code, control is entering an execution context. Active execution contexts logically form a stack. The top execution context on this logical stack is the running execution context. A new execution context is created whenever control is transferred from the executable code associated with the currently running execution context to executable code that is not associated with that execution context. The newly created execution context is pushed onto the stack and becomes the running execution context.
|
88
|
-
|
89
|
-
アクティブな実行コンテキストは論理的にスタックを構成する。
|
90
|
-
|
91
|
-
実行中の実行コンテキストに関連付いた実行コードから
|
92
|
-
関連付いていない実行コンテキストに制御が移るときはいつも、
|
93
|
-
新しい実行コンテキストが生成される。
|
94
|
-
新しい実行コンテキストは、スタックのトップにプッシュされ、実行中の実行コンテキストになる。
|
88
|
+
```
|
89
|
+
ECMAScriptの実行可能コードに制御が移ると、実行コンテキストに入る。アクティブな実行コンテキストは論理的にスタックを構成する。スタックのトップの実行コンテキストは、実行中の実行コンテキストである。実行中の実行コンテキストに関連付いた実行コードから関連付いていない実行コンテキストに制御が移るときはいつも、新しい実行コンテキストが生成される。新しい実行コンテキストは、スタックのトップにプッシュされ、実行中の実行コンテキストになる。
|
90
|
+
```
|
95
91
|
(executeもrunも「実行」と訳したので、「実行中の実行コンテキスト」となり、
|
96
92
|
日本語として少しおかしくなってしまいました)
|
97
93
|
|
3
改行位置などを修正した。
answer
CHANGED
@@ -26,9 +26,7 @@
|
|
26
26
|
ECMAScript 6の仕様には、シングルスレッドでの動作しか書かれていません。
|
27
27
|
以下の箇所が該当すると思います。
|
28
28
|
[8.4 Jobs and Job Queues](http://www.ecma-international.org/ecma-262/6.0/#sec-jobs-and-job-queues)
|
29
|
-
|
30
29
|
> Once execution of a Job is initiated, the Job always executes to completion. No other Job may be initiated until the currently running Job completes. However, the currently running Job or external events may cause the enqueuing of additional PendingJobs that may be initiated sometime after completion of the currently running Job.
|
31
|
-
|
32
30
|
Jobの実行中には、他のJobは実行されないこと、Jobの十個中にPendingJobsという別の
|
33
31
|
Jobをキューに追加することはあることが書かれています。
|
34
32
|
|
@@ -45,15 +43,11 @@
|
|
45
43
|
|
46
44
|
また、WHATWGの仕様を見ても、
|
47
45
|
以下のようにイベントループの説明があります。
|
48
|
-
[8.1.4 Event loops](
|
46
|
+
[8.1.4 Event loops](https://html.spec.whatwg.org/multipage/webappapis.html#event-loops)
|
49
|
-
taskのキューがあって、それをイベントループが処理していくというような
|
47
|
+
taskのキューがあって、それをイベントループが処理していくというような記述です。
|
50
|
-
記述です。
|
51
|
-
|
52
48
|
> There must be at least one browsing context event loop per user agent, and at most one per unit of related similar-origin browsing contexts.
|
49
|
+
ブラウジングコンテキストに対して、多くても1つしかイベントループがないと読めます。
|
53
50
|
|
54
|
-
ブラウジングコンテキストに対して、多くても1つしかイベントループがないと
|
55
|
-
読めます。
|
56
|
-
|
57
51
|
1つのイベントループは1スレッドかどうか、ということになると思いますが、
|
58
52
|
イベントループの[処理モデル](https://html.spec.whatwg.org/multipage/webappapis.html#processing-model-8)を見ても、
|
59
53
|
> An event loop must continually run through the following steps for as long as it exists:
|
@@ -61,10 +55,9 @@
|
|
61
55
|
|
62
56
|
また、
|
63
57
|
> Each event loop has a currently running task.
|
58
|
+
とあり、「event loop has a currently running task 」なので、同時に実行するtaskは
|
59
|
+
イベントループ1つにつき1つと考えられます。
|
64
60
|
|
65
|
-
とあり、「event loop has a currently running task 」なので、同時に実行するtaskはイベントループ1つにつき1つと
|
66
|
-
考えられます。
|
67
|
-
|
68
61
|
気にされている**setTimeoutについて**も、
|
69
62
|
taskの例として挙がっている
|
70
63
|
Events、Parsing、Callbacks、Using a resource、Reacting to DOM manipulation
|
@@ -74,8 +67,7 @@
|
|
74
67
|
> The timer initialisation steps, which are invoked with some method arguments, a method context, a repeat flag which can be true or false, and optionally (and only if the repeat flag is true) a previous handle, are as follows:
|
75
68
|
に記述されているステップの中に、
|
76
69
|
> 14. Queue the task task.
|
77
|
-
とあり、これがイベントループのキューを使っていることを
|
70
|
+
とあり、これがイベントループのキューを使っていることを意味していると思います。
|
78
|
-
意味していると思います。
|
79
71
|
|
80
72
|
### ECMAScript 6より前について
|
81
73
|
ECMAScript 5.1の仕様には、**シングルスレッドで処理する**ことについて、
|
2
ECMAScript 6より前について追記した。
answer
CHANGED
@@ -65,7 +65,7 @@
|
|
65
65
|
とあり、「event loop has a currently running task 」なので、同時に実行するtaskはイベントループ1つにつき1つと
|
66
66
|
考えられます。
|
67
67
|
|
68
|
-
気にされているsetTimeoutについても、
|
68
|
+
気にされている**setTimeoutについて**も、
|
69
69
|
taskの例として挙がっている
|
70
70
|
Events、Parsing、Callbacks、Using a resource、Reacting to DOM manipulation
|
71
71
|
の中のCallacksに該当すると考えられます。
|
@@ -75,4 +75,39 @@
|
|
75
75
|
に記述されているステップの中に、
|
76
76
|
> 14. Queue the task task.
|
77
77
|
とあり、これがイベントループのキューを使っていることを
|
78
|
-
意味していると思います。
|
78
|
+
意味していると思います。
|
79
|
+
|
80
|
+
### ECMAScript 6より前について
|
81
|
+
ECMAScript 5.1の仕様には、**シングルスレッドで処理する**ことについて、
|
82
|
+
はっきりとした記述が無い様に思います。
|
83
|
+
仕様に書かれているのは、
|
84
|
+
「実行コンテキストのスタックがあり、スタックのトップにある
|
85
|
+
実行コンテキストを使って処理が実行される。」
|
86
|
+
「新しく作られた実行コンテキストは、スタックにプッシュされる。」
|
87
|
+
ことです。
|
88
|
+
別のスレッドを作るのであれば、スタックを新しく作る必要があると思いますが、
|
89
|
+
スタックを新しく作る、と言う記述は仕様中に見当たらないため、
|
90
|
+
スレッドは作られない、つまりシングルスレッドと解釈できるかな、というところです。
|
91
|
+
|
92
|
+
以下、参照した仕様と、私なりの日本語訳です。
|
93
|
+
|
94
|
+
[10.3 Execution Contexts](http://www.ecma-international.org/ecma-262/5.1/#sec-10.3)
|
95
|
+
> When control is transferred to ECMAScript executable code, control is entering an execution context. Active execution contexts logically form a stack. The top execution context on this logical stack is the running execution context. A new execution context is created whenever control is transferred from the executable code associated with the currently running execution context to executable code that is not associated with that execution context. The newly created execution context is pushed onto the stack and becomes the running execution context.
|
96
|
+
ECMAScriptの実行可能コードに制御が移ると、実行コンテキストに入る。
|
97
|
+
アクティブな実行コンテキストは論理的にスタックを構成する。
|
98
|
+
スタックのトップの実行コンテキストは、実行中の実行コンテキストである。
|
99
|
+
実行中の実行コンテキストに関連付いた実行コードから
|
100
|
+
関連付いていない実行コンテキストに制御が移るときはいつも、
|
101
|
+
新しい実行コンテキストが生成される。
|
102
|
+
新しい実行コンテキストは、スタックのトップにプッシュされ、実行中の実行コンテキストになる。
|
103
|
+
(executeもrunも「実行」と訳したので、「実行中の実行コンテキスト」となり、
|
104
|
+
日本語として少しおかしくなってしまいました)
|
105
|
+
|
106
|
+
ECMAScript 3以前については、調べていませんが、
|
107
|
+
同様の記述があるのではないかと思います。
|
108
|
+
|
109
|
+
**setTimeoutなどの非同期呼び出し**については、
|
110
|
+
[strawman:oldes6 (ES Wiki)](http://wiki.ecmascript.org/doku.php?id=strawman:oldes6)
|
111
|
+
の
|
112
|
+
> Concurrency, Asynchrony, and Distributed Programming
|
113
|
+
あたりが元のようで、ES6より前には仕様化されていなかったように見えます。
|
1
ECMAScript 6を見たことを明記した。
answer
CHANGED
@@ -23,7 +23,7 @@
|
|
23
23
|
ただ、Web Workerからはwindowオブジェクトは参照できない様なので
|
24
24
|
以降の記述では触れません。
|
25
25
|
|
26
|
-
ECMAScriptの仕様には、シングルスレッドでの動作しか書かれていません。
|
26
|
+
ECMAScript 6の仕様には、シングルスレッドでの動作しか書かれていません。
|
27
27
|
以下の箇所が該当すると思います。
|
28
28
|
[8.4 Jobs and Job Queues](http://www.ecma-international.org/ecma-262/6.0/#sec-jobs-and-job-queues)
|
29
29
|
|