Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1375)

Side by Side Diff: third_party/WebKit/Source/platform/bindings/V8PerIsolateData.cpp

Issue 2841443005: [Bindings] Create and use V8 context snapshots (Closed)
Patch Set: Rebase Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "platform/bindings/V8PerIsolateData.h" 26 #include "platform/bindings/V8PerIsolateData.h"
27 27
28 #include <memory> 28 #include <memory>
29 29
30 #include "platform/RuntimeEnabledFeatures.h"
30 #include "platform/ScriptForbiddenScope.h" 31 #include "platform/ScriptForbiddenScope.h"
31 #include "platform/WebTaskRunner.h" 32 #include "platform/WebTaskRunner.h"
32 #include "platform/bindings/DOMDataStore.h" 33 #include "platform/bindings/DOMDataStore.h"
33 #include "platform/bindings/V8Binding.h" 34 #include "platform/bindings/V8Binding.h"
34 #include "platform/bindings/V8ObjectConstructor.h" 35 #include "platform/bindings/V8ObjectConstructor.h"
35 #include "platform/bindings/V8PrivateProperty.h" 36 #include "platform/bindings/V8PrivateProperty.h"
36 #include "platform/bindings/V8ValueCache.h" 37 #include "platform/bindings/V8ValueCache.h"
37 #include "platform/wtf/LeakAnnotations.h" 38 #include "platform/wtf/LeakAnnotations.h"
38 #include "platform/wtf/PtrUtil.h" 39 #include "platform/wtf/PtrUtil.h"
39 #include "public/platform/Platform.h" 40 #include "public/platform/Platform.h"
40 #include "v8/include/v8-debug.h" 41 #include "v8/include/v8-debug.h"
41 42
42 namespace blink { 43 namespace blink {
43 44
44 static V8PerIsolateData* g_main_thread_per_isolate_data = 0; 45 static V8PerIsolateData* g_main_thread_per_isolate_data = 0;
45 46
46 static void BeforeCallEnteredCallback(v8::Isolate* isolate) { 47 static void BeforeCallEnteredCallback(v8::Isolate* isolate) {
47 // TODO(jochen): Re-enable this once https://crbug.com/728583 48 // TODO(jochen): Re-enable this once https://crbug.com/728583
48 // CHECK(!ScriptForbiddenScope::IsScriptForbidden()); 49 // CHECK(!ScriptForbiddenScope::IsScriptForbidden());
49 } 50 }
50 51
51 static void MicrotasksCompletedCallback(v8::Isolate* isolate) { 52 static void MicrotasksCompletedCallback(v8::Isolate* isolate) {
52 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks(); 53 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks();
53 } 54 }
54 55
55 V8PerIsolateData::V8PerIsolateData(WebTaskRunner* task_runner) 56 V8PerIsolateData::V8PerIsolateData(
56 : isolate_holder_( 57 WebTaskRunner* task_runner,
58 intptr_t* table,
59 V8ContextSnapshotMode v8_context_snapshot_mode)
60 : v8_context_snapshot_mode_(v8_context_snapshot_mode),
61 isolate_holder_(
57 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr, 62 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr,
58 gin::IsolateHolder::kSingleThread, 63 gin::IsolateHolder::kSingleThread,
59 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait 64 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait
60 : gin::IsolateHolder::kAllowAtomicsWait), 65 : gin::IsolateHolder::kAllowAtomicsWait,
66 table,
67 v8_context_snapshot_mode_ == V8ContextSnapshotMode::kUseSnapshot
68 ? &startup_data_
69 : nullptr),
70 interface_template_map_for_v8_context_snapshot_(GetIsolate()),
61 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))), 71 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))),
62 private_property_(V8PrivateProperty::Create()), 72 private_property_(V8PrivateProperty::Create()),
63 constructor_mode_(ConstructorMode::kCreateNewObject), 73 constructor_mode_(ConstructorMode::kCreateNewObject),
64 use_counter_disabled_(false), 74 use_counter_disabled_(false),
65 is_handling_recursion_level_error_(false), 75 is_handling_recursion_level_error_(false),
66 is_reporting_exception_(false) { 76 is_reporting_exception_(false) {
77 // If it fails to load the snapshot file, falls back to kDontUseSnapshot mode.
78 // TODO(peria): Remove this fallback routine.
79 if (v8_context_snapshot_mode_ == V8ContextSnapshotMode::kUseSnapshot &&
80 !startup_data_.data) {
81 v8_context_snapshot_mode_ = V8ContextSnapshotMode::kDontUseSnapshot;
82 }
83
67 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. 84 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
68 GetIsolate()->Enter(); 85 GetIsolate()->Enter();
69 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); 86 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
70 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); 87 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback);
71 if (IsMainThread()) 88 if (IsMainThread())
72 g_main_thread_per_isolate_data = this; 89 g_main_thread_per_isolate_data = this;
73 } 90 }
74 91
92 // This constructor is used for taking a V8 context snapshot. It must run on the
93 // main thread.
94 V8PerIsolateData::V8PerIsolateData(intptr_t* reference_table)
95 : v8_context_snapshot_mode_(V8ContextSnapshotMode::kTakeSnapshot),
96 isolate_holder_(reference_table, nullptr),
97 interface_template_map_for_v8_context_snapshot_(GetIsolate()),
98 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))),
99 private_property_(V8PrivateProperty::Create()),
100 constructor_mode_(ConstructorMode::kCreateNewObject),
101 use_counter_disabled_(false),
102 is_handling_recursion_level_error_(false),
103 is_reporting_exception_(false) {
104 CHECK(IsMainThread());
105
106 // SnapshotCreator enters the isolate, so we don't call Isolate::Enter() here.
107 g_main_thread_per_isolate_data = this;
108 }
109
75 V8PerIsolateData::~V8PerIsolateData() {} 110 V8PerIsolateData::~V8PerIsolateData() {}
76 111
77 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { 112 v8::Isolate* V8PerIsolateData::MainThreadIsolate() {
78 DCHECK(g_main_thread_per_isolate_data); 113 DCHECK(g_main_thread_per_isolate_data);
79 return g_main_thread_per_isolate_data->GetIsolate(); 114 return g_main_thread_per_isolate_data->GetIsolate();
80 } 115 }
81 116
82 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner) { 117 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner,
83 V8PerIsolateData* data = new V8PerIsolateData(task_runner); 118 intptr_t* reference_table,
119 V8ContextSnapshotMode context_mode) {
120 DCHECK(context_mode == V8ContextSnapshotMode::kDontUseSnapshot ||
121 reference_table);
122
123 V8PerIsolateData* data = nullptr;
124 if (context_mode == V8ContextSnapshotMode::kTakeSnapshot) {
125 CHECK(reference_table);
126 data = new V8PerIsolateData(reference_table);
127 } else {
128 data = new V8PerIsolateData(task_runner, reference_table, context_mode);
129 }
130 DCHECK(data);
131
84 v8::Isolate* isolate = data->GetIsolate(); 132 v8::Isolate* isolate = data->GetIsolate();
85 isolate->SetData(gin::kEmbedderBlink, data); 133 isolate->SetData(gin::kEmbedderBlink, data);
86 return isolate; 134 return isolate;
87 } 135 }
88 136
89 void V8PerIsolateData::EnableIdleTasks( 137 void V8PerIsolateData::EnableIdleTasks(
90 v8::Isolate* isolate, 138 v8::Isolate* isolate,
91 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { 139 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) {
92 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); 140 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner));
93 } 141 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( 205 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(
158 GetIsolate(), callback, data, signature, length); 206 GetIsolate(), callback, data, signature, length);
159 templ->RemovePrototype(); 207 templ->RemovePrototype();
160 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); 208 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ));
161 return templ; 209 return templ;
162 } 210 }
163 211
164 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( 212 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate(
165 const DOMWrapperWorld& world, 213 const DOMWrapperWorld& world,
166 const void* key) { 214 const void* key) {
215 if (GetV8ContextSnapshotMode() == V8ContextSnapshotMode::kTakeSnapshot) {
216 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key);
217 return interface_template_map_for_v8_context_snapshot_.Get(type);
218 }
219
167 auto& map = SelectInterfaceTemplateMap(world); 220 auto& map = SelectInterfaceTemplateMap(world);
168 auto result = map.find(key); 221 auto result = map.find(key);
169 if (result != map.end()) 222 if (result != map.end())
170 return result->value.Get(GetIsolate()); 223 return result->value.Get(GetIsolate());
171 return v8::Local<v8::FunctionTemplate>(); 224 return v8::Local<v8::FunctionTemplate>();
172 } 225 }
173 226
174 void V8PerIsolateData::SetInterfaceTemplate( 227 void V8PerIsolateData::SetInterfaceTemplate(
175 const DOMWrapperWorld& world, 228 const DOMWrapperWorld& world,
176 const void* key, 229 const void* key,
177 v8::Local<v8::FunctionTemplate> value) { 230 v8::Local<v8::FunctionTemplate> value) {
178 auto& map = SelectInterfaceTemplateMap(world); 231 if (GetV8ContextSnapshotMode() == V8ContextSnapshotMode::kTakeSnapshot) {
179 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); 232 auto& map = interface_template_map_for_v8_context_snapshot_;
233 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key);
234 map.Set(type, value);
235 } else {
236 auto& map = SelectInterfaceTemplateMap(world);
237 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value));
238 }
239 }
240
241 void V8PerIsolateData::ClearPersistentsForV8ContextSnapshot() {
242 interface_template_map_for_v8_context_snapshot_.Clear();
243 private_property_.reset();
180 } 244 }
181 245
182 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( 246 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache(
183 const void* lookup_key, 247 const void* lookup_key,
184 const char* const names[], 248 const char* const names[],
185 size_t count) { 249 size_t count) {
186 auto it = eternal_name_cache_.find(lookup_key); 250 auto it = eternal_name_cache_.find(lookup_key);
187 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; 251 const Vector<v8::Eternal<v8::Name>>* vector = nullptr;
188 if (UNLIKELY(it == eternal_name_cache_.end())) { 252 if (UNLIKELY(it == eternal_name_cache_.end())) {
189 v8::Isolate* isolate = this->GetIsolate(); 253 v8::Isolate* isolate = this->GetIsolate();
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 ScriptWrappableVisitor* current = CurrentVisitor(); 369 ScriptWrappableVisitor* current = CurrentVisitor();
306 if (current) 370 if (current)
307 current->PerformCleanup(); 371 current->PerformCleanup();
308 372
309 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( 373 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap(
310 saved_visitor_); 374 saved_visitor_);
311 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); 375 isolate_->SetEmbedderHeapTracer(CurrentVisitor());
312 } 376 }
313 377
314 } // namespace blink 378 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/bindings/V8PerIsolateData.h ('k') | third_party/WebKit/public/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698