OLD | NEW |
---|---|
1 # Design of V8 bindings | 1 # Design of V8 bindings |
2 | 2 |
3 This document explains key concepts in the V8 binding architecture | 3 This document explains key concepts in the V8 binding architecture |
4 except the lifetime management of DOM wrappers. | 4 except the lifetime management of DOM wrappers. |
5 See [V8GCController.md](V8GCController.md) to learn the lifetime management. | 5 See [V8GCController.md](V8GCController.md) to learn the lifetime management. |
6 | 6 |
7 [TOC] | 7 [TOC] |
8 | 8 |
9 ## Isolate | 9 ## Isolate |
10 | 10 |
11 An isolate is a concept of a thread in V8. | 11 An isolate is a concept of a heap in V8. |
jochen (gone - plz use gerrit)
2016/01/08 14:09:20
What about an isolate is an instance of V8
haraken
2016/01/08 14:26:49
Done.
| |
12 Isolates and threads are in 1:1 relationship. | 12 In Blink, isolates and threads are in 1:1 relationship |
13 (except compositor workers which map multiple worker threads on one isolate). | |
jochen (gone - plz use gerrit)
2016/01/08 14:09:20
hum, I don't think that's correct, they all share
haraken
2016/01/08 14:26:49
That's just a wording issue. I meant a user-level
| |
13 One isolate is associated with the main thread. | 14 One isolate is associated with the main thread. |
14 One isolate is associated with one worker thread. | 15 One isolate is associated with one worker thread. |
15 | 16 |
16 ## Context | 17 ## Context |
17 | 18 |
18 A context is a concept of a global variable scope in V8. | 19 A context is a concept of a global variable scope in V8. |
19 Roughly speaking, one window object corresponds to one context. | 20 Roughly speaking, one window object corresponds to one context. |
20 For example, `<iframe>` has a window object different from a window object | 21 For example, `<iframe>` has a window object different from a window object |
21 of its parent frame. So the context of the `<iframe>` is different from | 22 of its parent frame. So the context of the `<iframe>` is different from |
22 the context of the parent frame. Since these contexts create their own | 23 the context of the parent frame. Since these contexts create their own |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 | 100 |
100 There is a second stack that operates on a much coarser granularity. | 101 There is a second stack that operates on a much coarser granularity. |
101 This stack is managed by V8 binding (not by V8). | 102 This stack is managed by V8 binding (not by V8). |
102 When V8 binding invokes JavaScript, V8 binding enters a context | 103 When V8 binding invokes JavaScript, V8 binding enters a context |
103 and pushes the context onto the stack. | 104 and pushes the context onto the stack. |
104 The JavaScript starts running on the context. When the JavaScript finishes | 105 The JavaScript starts running on the context. When the JavaScript finishes |
105 and the control returns back to V8 binding, V8 binding pops the context | 106 and the control returns back to V8 binding, V8 binding pops the context |
106 from the stack. Given that the control between V8 binding and V8 can be nested | 107 from the stack. Given that the control between V8 binding and V8 can be nested |
107 (i.e., V8 binding invokes JavaScript, which calls into V8 binding, | 108 (i.e., V8 binding invokes JavaScript, which calls into V8 binding, |
108 which invokes another JavaScript etc), these contexts form a stack. | 109 which invokes another JavaScript etc), these contexts form a stack. |
109 The pushing and popping are done by calling v8::Context::Enter() and | 110 The pushing and popping are done by any V8 API that takes a context argument |
110 v8::Context::Exit() (or v8::Context::Scope). We call the most recently entered | 111 or by explicitly calling v8::Context::Enter() and v8::Context::Exit(). |
111 context an entered context. | 112 We call the most recently entered context an entered context. |
112 | 113 |
113 In the above example, at the point when func() is running, | 114 In the above example, at the point when func() is running, |
114 the entered context is the context of the main frame | 115 the entered context is the context of the main frame |
115 (not the context of `<iframe>`). | 116 (not the context of `<iframe>`). |
116 | 117 |
117 The entered context is a concept to implement the | 118 The entered context is a concept to implement the |
118 [entry settings object](https://html.spec.whatwg.org/multipage/webappapis.html#e ntry-settings-object) | 119 [entry settings object](https://html.spec.whatwg.org/multipage/webappapis.html#e ntry-settings-object) |
119 of the HTML spec. The current context is a concept to implement the | 120 of the HTML spec. The current context is a concept to implement the |
120 [incumbent settings object](https://html.spec.whatwg.org/multipage/webappapis.ht ml#incumbent-settings-object) | 121 [incumbent settings object](https://html.spec.whatwg.org/multipage/webappapis.ht ml#incumbent-settings-object) |
121 of the HTML spec. | 122 of the HTML spec. |
122 | 123 |
123 In summary, the entered context is a context from which the current JavaScript | 124 In summary, the entered context is a context from which the current JavaScript |
124 execution was started. The current context is a context of | 125 execution was started. The current context is a context of |
125 the JavaScript function that is currently running. | 126 the JavaScript function that is currently running. |
126 | 127 |
128 There is another special context called a debugger context. | |
129 If a debugger is active, the debugger context may be inserted to | |
130 the context stack. | |
131 | |
127 ## World | 132 ## World |
128 | 133 |
129 A world is a concept to sandbox DOM wrappers among content scripts of | 134 A world is a concept to sandbox DOM wrappers among content scripts of |
130 Chrome extensions. There are three kinds of worlds: a main world, | 135 Chrome extensions. There are three kinds of worlds: a main world, |
131 an isolated world and a worker world. | 136 an isolated world and a worker world. |
132 A main world is a world where a normal JavaScript downloaded from the web | 137 A main world is a world where a normal JavaScript downloaded from the web |
133 is executed. | 138 is executed. |
134 An isolated world is a world where a content script of a Chrome extension. | 139 An isolated world is a world where a content script of a Chrome extension. |
135 An isolate of the main thread has 1 main world and N isolated worlds. | 140 An isolate of the main thread has 1 main world and N isolated worlds. |
136 An isolate of a worker thread has 1 worker world and 0 isolated world. | 141 An isolate of a worker thread has 1 worker world and 0 isolated world. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 For example, when the main thread is operating on a frame X using a JavaScript | 191 For example, when the main thread is operating on a frame X using a JavaScript |
187 in a world Y, the current context is set to a context for the pair of (X, Y). | 192 in a world Y, the current context is set to a context for the pair of (X, Y). |
188 The current context of the main thread changes over its lifetime. | 193 The current context of the main thread changes over its lifetime. |
189 | 194 |
190 On the other hand, a worker thread has 0 frame and 1 world. | 195 On the other hand, a worker thread has 0 frame and 1 world. |
191 Thus a worker thread has only 1 context. | 196 Thus a worker thread has only 1 context. |
192 The current context of the worker thread never changes. | 197 The current context of the worker thread never changes. |
193 | 198 |
194 ## DOM wrappers and worlds | 199 ## DOM wrappers and worlds |
195 | 200 |
196 For compatibility reasons (although this is not speced), | 201 For compatibility reasons, we need to make sure that the same DOM wrapper |
197 we need to make sure that the same DOM wrapper is returned to JavaScript | 202 is returned to JavaScript as long as the underlying C++ DOM object is alive. |
198 as long as the underlying C++ DOM object is alive. | |
199 We should not return different DOM wrappers for the same C++ DOM object. | 203 We should not return different DOM wrappers for the same C++ DOM object. |
200 | 204 |
201 Here is an example: | 205 Here is an example: |
202 | 206 |
203 ```html | 207 ```html |
204 var div = document.createElement("div"); | 208 var div = document.createElement("div"); |
205 div.foo = 1234; // expando | 209 div.foo = 1234; // expando |
206 var p = document.createElement("p"); | 210 var p = document.createElement("p"); |
207 p.appendChild(div); | 211 p.appendChild(div); |
208 div = null; | 212 div = null; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 // iframe.html | 254 // iframe.html |
251 <script> | 255 <script> |
252 </script> | 256 </script> |
253 ``` | 257 ``` |
254 | 258 |
255 To make sure that a DOM wrapper is created in a correct context, you need to | 259 To make sure that a DOM wrapper is created in a correct context, you need to |
256 make sure that the current context must be set to the correct context | 260 make sure that the current context must be set to the correct context |
257 whenever you call toV8(). If you're not sure what context to use, | 261 whenever you call toV8(). If you're not sure what context to use, |
258 ask haraken@chromium.org. | 262 ask haraken@chromium.org. |
259 | 263 |
OLD | NEW |