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