 Chromium Code Reviews
 Chromium Code Reviews Issue 2841443005:
  [Bindings] Create and use V8 context snapshots  (Closed)
    
  
    Issue 2841443005:
  [Bindings] Create and use V8 context snapshots  (Closed) 
  | OLD | NEW | 
|---|---|
| 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 CHECK(!ScriptForbiddenScope::IsScriptForbidden()); | 48 CHECK(!ScriptForbiddenScope::IsScriptForbidden()); | 
| 48 } | 49 } | 
| 49 | 50 | 
| 50 static void MicrotasksCompletedCallback(v8::Isolate* isolate) { | 51 static void MicrotasksCompletedCallback(v8::Isolate* isolate) { | 
| 51 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks(); | 52 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks(); | 
| 52 } | 53 } | 
| 53 | 54 | 
| 54 V8PerIsolateData::V8PerIsolateData(WebTaskRunner* task_runner) | 55 V8PerIsolateData::V8PerIsolateData(WebTaskRunner* task_runner, | 
| 56 intptr_t* table, | |
| 57 V8ContextMode context_mode) | |
| 55 : isolate_holder_( | 58 : isolate_holder_( | 
| 56 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr, | 59 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr, | 
| 57 gin::IsolateHolder::kSingleThread, | 60 gin::IsolateHolder::kSingleThread, | 
| 58 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait | 61 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait | 
| 59 : gin::IsolateHolder::kAllowAtomicsWait), | 62 : gin::IsolateHolder::kAllowAtomicsWait, | 
| 63 table, | |
| 64 context_mode == V8ContextMode::kUseSnapshot ? &startup_data_ | |
| 
Yuki
2017/05/30 14:35:58
Better to set v8_context_mode_ first, and use memb
 
peria
2017/06/01 08:33:34
Done.
 | |
| 65 : nullptr), | |
| 66 v8_context_mode_(context_mode), | |
| 60 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))), | 67 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))), | 
| 61 private_property_(V8PrivateProperty::Create()), | 68 private_property_(V8PrivateProperty::Create()), | 
| 62 constructor_mode_(ConstructorMode::kCreateNewObject), | 69 constructor_mode_(ConstructorMode::kCreateNewObject), | 
| 63 use_counter_disabled_(false), | 70 use_counter_disabled_(false), | 
| 64 is_handling_recursion_level_error_(false), | 71 is_handling_recursion_level_error_(false), | 
| 65 is_reporting_exception_(false) { | 72 is_reporting_exception_(false) { | 
| 73 // In case if failed to load snapshot file | |
| 74 if (v8_context_mode_ == V8ContextMode::kUseSnapshot && !startup_data_.data) { | |
| 75 v8_context_mode_ = V8ContextMode::kDontUseSnapshot; | |
| 
Yuki
2017/05/30 14:35:58
Probably I asked this question before.
Do we reall
 
peria
2017/06/01 08:33:34
Let's chat again.
 | |
| 76 } | |
| 77 | |
| 66 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. | 78 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. | 
| 67 GetIsolate()->Enter(); | 79 GetIsolate()->Enter(); | 
| 68 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); | 80 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); | 
| 69 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); | 81 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); | 
| 70 if (IsMainThread()) | 82 if (IsMainThread()) | 
| 71 g_main_thread_per_isolate_data = this; | 83 g_main_thread_per_isolate_data = this; | 
| 72 } | 84 } | 
| 73 | 85 | 
| 86 V8PerIsolateData::V8PerIsolateData(intptr_t* table) | |
| 87 : snapshot_creator_(new v8::SnapshotCreator(table)), | |
| 88 isolate_holder_(snapshot_creator_->GetIsolate()), | |
| 89 v8_context_mode_(V8ContextMode::kTakeSnapshot), | |
| 90 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))), | |
| 91 private_property_(V8PrivateProperty::Create()), | |
| 92 constructor_mode_(ConstructorMode::kCreateNewObject), | |
| 93 use_counter_disabled_(false), | |
| 94 is_handling_recursion_level_error_(false), | |
| 95 is_reporting_exception_(false) { | |
| 96 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. | |
| 97 // SnapshotCreator enters the isolate, so don't do it again. | |
| 98 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); | |
| 99 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); | |
| 100 g_main_thread_per_isolate_data = this; | |
| 101 } | |
| 102 | |
| 74 V8PerIsolateData::~V8PerIsolateData() {} | 103 V8PerIsolateData::~V8PerIsolateData() {} | 
| 75 | 104 | 
| 76 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { | 105 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { | 
| 77 DCHECK(g_main_thread_per_isolate_data); | 106 DCHECK(g_main_thread_per_isolate_data); | 
| 78 return g_main_thread_per_isolate_data->GetIsolate(); | 107 return g_main_thread_per_isolate_data->GetIsolate(); | 
| 79 } | 108 } | 
| 80 | 109 | 
| 81 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner) { | 110 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner, | 
| 82 V8PerIsolateData* data = new V8PerIsolateData(task_runner); | 111 intptr_t* table, | 
| 112 V8ContextMode context_mode) { | |
| 113 V8PerIsolateData* data = nullptr; | |
| 114 if (context_mode == V8ContextMode::kTakeSnapshot) { | |
| 115 CHECK(table); | |
| 116 data = new V8PerIsolateData(table); | |
| 117 v8::Isolate* isolate = data->GetIsolate(); | |
| 118 CHECK(isolate); | |
| 119 isolate->SetData(gin::kEmbedderBlink, data); | |
| 120 return isolate; | |
| 121 } | |
| 122 | |
| 123 if (!RuntimeEnabledFeatures::v8ContextSnapshotEnabled()) { | |
| 124 context_mode = V8ContextMode::kDontUseSnapshot; | |
| 125 table = nullptr; | |
| 126 } | |
| 127 data = new V8PerIsolateData(task_runner, table, context_mode); | |
| 83 v8::Isolate* isolate = data->GetIsolate(); | 128 v8::Isolate* isolate = data->GetIsolate(); | 
| 84 isolate->SetData(gin::kEmbedderBlink, data); | 129 isolate->SetData(gin::kEmbedderBlink, data); | 
| 85 return isolate; | 130 return isolate; | 
| 86 } | 131 } | 
| 87 | 132 | 
| 88 void V8PerIsolateData::EnableIdleTasks( | 133 void V8PerIsolateData::EnableIdleTasks( | 
| 89 v8::Isolate* isolate, | 134 v8::Isolate* isolate, | 
| 90 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { | 135 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { | 
| 91 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); | 136 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); | 
| 92 } | 137 } | 
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( | 201 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( | 
| 157 GetIsolate(), callback, data, signature, length); | 202 GetIsolate(), callback, data, signature, length); | 
| 158 templ->RemovePrototype(); | 203 templ->RemovePrototype(); | 
| 159 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); | 204 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); | 
| 160 return templ; | 205 return templ; | 
| 161 } | 206 } | 
| 162 | 207 | 
| 163 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( | 208 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( | 
| 164 const DOMWrapperWorld& world, | 209 const DOMWrapperWorld& world, | 
| 165 const void* key) { | 210 const void* key) { | 
| 211 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) { | |
| 212 auto& map = interface_template_persistent_map_; | |
| 213 auto result = map.find(key); | |
| 214 if (result != map.end()) | |
| 215 return result->value.Get(GetIsolate()); | |
| 216 return v8::Local<v8::FunctionTemplate>(); | |
| 217 } | |
| 218 | |
| 166 auto& map = SelectInterfaceTemplateMap(world); | 219 auto& map = SelectInterfaceTemplateMap(world); | 
| 167 auto result = map.find(key); | 220 auto result = map.find(key); | 
| 168 if (result != map.end()) | 221 if (result != map.end()) | 
| 169 return result->value.Get(GetIsolate()); | 222 return result->value.Get(GetIsolate()); | 
| 170 return v8::Local<v8::FunctionTemplate>(); | 223 return v8::Local<v8::FunctionTemplate>(); | 
| 171 } | 224 } | 
| 172 | 225 | 
| 173 void V8PerIsolateData::SetInterfaceTemplate( | 226 void V8PerIsolateData::SetInterfaceTemplate( | 
| 174 const DOMWrapperWorld& world, | 227 const DOMWrapperWorld& world, | 
| 175 const void* key, | 228 const void* key, | 
| 176 v8::Local<v8::FunctionTemplate> value) { | 229 v8::Local<v8::FunctionTemplate> value) { | 
| 177 auto& map = SelectInterfaceTemplateMap(world); | 230 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) { | 
| 178 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); | 231 auto& map = interface_template_persistent_map_; | 
| 232 map.insert(key, CopyablePersistent(GetIsolate(), value)); | |
| 233 } else { | |
| 234 auto& map = SelectInterfaceTemplateMap(world); | |
| 235 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void V8PerIsolateData::ClearPersistents() { | |
| 240 interface_template_persistent_map_.clear(); | |
| 241 private_property_.reset(); | |
| 179 } | 242 } | 
| 180 | 243 | 
| 181 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( | 244 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( | 
| 182 const void* lookup_key, | 245 const void* lookup_key, | 
| 183 const char* const names[], | 246 const char* const names[], | 
| 184 size_t count) { | 247 size_t count) { | 
| 185 auto it = eternal_name_cache_.find(lookup_key); | 248 auto it = eternal_name_cache_.find(lookup_key); | 
| 186 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; | 249 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; | 
| 187 if (UNLIKELY(it == eternal_name_cache_.end())) { | 250 if (UNLIKELY(it == eternal_name_cache_.end())) { | 
| 188 v8::Isolate* isolate = this->GetIsolate(); | 251 v8::Isolate* isolate = this->GetIsolate(); | 
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 ScriptWrappableVisitor* current = CurrentVisitor(); | 365 ScriptWrappableVisitor* current = CurrentVisitor(); | 
| 303 if (current) | 366 if (current) | 
| 304 current->PerformCleanup(); | 367 current->PerformCleanup(); | 
| 305 | 368 | 
| 306 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( | 369 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( | 
| 307 saved_visitor_); | 370 saved_visitor_); | 
| 308 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); | 371 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); | 
| 309 } | 372 } | 
| 310 | 373 | 
| 311 } // namespace blink | 374 } // namespace blink | 
| OLD | NEW |