Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "bindings/core/v8/V8SnapshotUtil.h" | |
| 6 | |
| 7 #include <array> | |
| 8 #include <cstring> | |
| 9 | |
| 10 #include "bindings/core/v8/GeneratedCodeHelper.h" | |
| 11 #include "bindings/core/v8/V8Document.h" | |
| 12 #include "bindings/core/v8/V8EventTarget.h" | |
| 13 #include "bindings/core/v8/V8HTMLDocument.h" | |
| 14 #include "bindings/core/v8/V8Initializer.h" | |
| 15 #include "bindings/core/v8/V8Node.h" | |
| 16 #include "bindings/core/v8/V8Window.h" | |
| 17 #include "platform/bindings/DOMWrapperWorld.h" | |
| 18 #include "platform/bindings/V8ObjectConstructor.h" | |
| 19 #include "platform/bindings/V8PerIsolateData.h" | |
| 20 #include "platform/bindings/V8PrivateProperty.h" | |
| 21 #include "platform/instrumentation/tracing/TraceEvent.h" | |
| 22 #include "v8/include/v8.h" | |
| 23 | |
| 24 namespace blink { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 intptr_t* g_snapshot_reference_table = nullptr; | |
| 29 | |
| 30 // TODO(peria): This method is almost a copy of | |
| 31 // V8PerContext::ConstructorForTypeSlowCase(), so merge with it. | |
| 32 v8::Local<v8::Function> ConstructPlainType(v8::Isolate* isolate, | |
| 33 const DOMWrapperWorld& world, | |
| 34 v8::Local<v8::Context> context, | |
| 35 const WrapperTypeInfo* type) { | |
| 36 v8::Context::Scope scope(context); | |
|
haraken
2017/07/06 13:15:52
Add CHECK(V8HTMLDocument::wrapperTypeInfo.Equals(t
peria
2017/07/07 06:22:00
if we need it, we need
V8HTMLDocument::... || V8
| |
| 37 // We shouldn't reach this point for the types that are implemented in v8 such | |
| 38 // as typed arrays and hence don't have domTemplateFunction. | |
| 39 DCHECK(type->dom_template_function); | |
| 40 v8::Local<v8::FunctionTemplate> interface_template = | |
| 41 type->domTemplate(isolate, world); | |
| 42 // Getting the function might fail if we're running out of stack or memory. | |
| 43 v8::Local<v8::Function> interface_object = | |
| 44 interface_template->GetFunction(context).ToLocalChecked(); | |
| 45 | |
| 46 if (type->parent_class) { | |
| 47 v8::Local<v8::Object> prototype_template = | |
| 48 ConstructPlainType(isolate, world, context, type->parent_class); | |
| 49 CHECK(interface_object->SetPrototype(context, prototype_template) | |
| 50 .ToChecked()); | |
| 51 } | |
| 52 | |
| 53 v8::Local<v8::Value> prototype_value = | |
| 54 interface_object->Get(context, V8AtomicString(isolate, "prototype")) | |
| 55 .ToLocalChecked(); | |
| 56 CHECK(prototype_value->IsObject()); | |
| 57 v8::Local<v8::Object> prototype_object = prototype_value.As<v8::Object>(); | |
| 58 if (prototype_object->InternalFieldCount() == | |
| 59 kV8PrototypeInternalFieldcount && | |
| 60 type->wrapper_type_prototype == | |
| 61 WrapperTypeInfo::kWrapperTypeObjectPrototype) { | |
| 62 prototype_object->SetAlignedPointerInInternalField( | |
| 63 kV8PrototypeTypeIndex, const_cast<WrapperTypeInfo*>(type)); | |
| 64 } | |
| 65 type->PreparePrototypeAndInterfaceObject( | |
| 66 context, world, prototype_object, interface_object, interface_template); | |
| 67 | |
| 68 return interface_object; | |
| 69 } | |
| 70 | |
| 71 // TODO(peria): This method is almost a copy of | |
| 72 // V8PerContext::CreateWrapperFromCacheSlowCase(), so merge with it. | |
| 73 v8::Local<v8::Object> CreatePlainWrapper(v8::Isolate* isolate, | |
| 74 const DOMWrapperWorld& world, | |
| 75 v8::Local<v8::Context> context, | |
| 76 const WrapperTypeInfo* type) { | |
| 77 CHECK(V8HTMLDocument::wrapperTypeInfo.Equals(type)); | |
| 78 | |
| 79 v8::Context::Scope scope(context); | |
| 80 v8::Local<v8::Function> interface_object = | |
| 81 ConstructPlainType(isolate, world, context, type); | |
| 82 CHECK(!interface_object.IsEmpty()); | |
| 83 v8::Local<v8::Object> instance_template = | |
| 84 V8ObjectConstructor::NewInstance(isolate, interface_object) | |
| 85 .ToLocalChecked(); | |
| 86 v8::Local<v8::Object> wrapper = instance_template->Clone(); | |
| 87 wrapper->SetAlignedPointerInInternalField(kV8DOMWrapperTypeIndex, | |
| 88 const_cast<WrapperTypeInfo*>(type)); | |
| 89 return wrapper; | |
| 90 } | |
| 91 | |
| 92 constexpr int kWorldIdForNonMainWorld = | |
| 93 DOMWrapperWorld::WorldId::kIsolatedWorldIdLimit - 1; | |
| 94 | |
| 95 int GetSnapshotIndexForWorld(const DOMWrapperWorld& world) { | |
| 96 return world.IsMainWorld() ? 0 : 1; | |
| 97 } | |
| 98 | |
| 99 struct SnapshotInterface { | |
| 100 const WrapperTypeInfo* wrapper_type_info; | |
| 101 InstallRuntimeEnabledFeaturesOnTemplateFunction install_function; | |
| 102 }; | |
| 103 | |
| 104 SnapshotInterface g_snapshot_interfaces[] = { | |
|
haraken
2017/07/06 13:15:53
Add a detailed comment about what are contained in
peria
2017/07/07 06:22:00
Done.
| |
| 105 {&V8Window::wrapperTypeInfo, | |
| 106 V8Window::InstallRuntimeEnabledFeaturesOnTemplate}, | |
| 107 {&V8HTMLDocument::wrapperTypeInfo, | |
| 108 V8HTMLDocument::InstallRuntimeEnabledFeaturesOnTemplate}, | |
| 109 {&V8EventTarget::wrapperTypeInfo, | |
| 110 V8EventTarget::InstallRuntimeEnabledFeaturesOnTemplate}, | |
| 111 {&V8Node::wrapperTypeInfo, V8Node::InstallRuntimeEnabledFeaturesOnTemplate}, | |
| 112 {&V8Document::wrapperTypeInfo, | |
| 113 V8Document::InstallRuntimeEnabledFeaturesOnTemplate}, | |
| 114 }; | |
| 115 constexpr size_t kSnapshotInterfaceSize = | |
| 116 WTF_ARRAY_LENGTH(g_snapshot_interfaces); | |
| 117 | |
| 118 enum class InternalFieldType : uint8_t { | |
| 119 kNone, | |
| 120 kNodeType, | |
| 121 kDocumentType, | |
| 122 kHTMLDocumentType, | |
| 123 kHTMLDocumentObject, | |
| 124 }; | |
| 125 | |
| 126 const WrapperTypeInfo* FieldTypeToWrapperTypeInfo(InternalFieldType type) { | |
| 127 switch (type) { | |
| 128 case InternalFieldType::kNone: | |
| 129 NOTREACHED(); | |
| 130 break; | |
| 131 case InternalFieldType::kNodeType: | |
| 132 return &V8Node::wrapperTypeInfo; | |
| 133 case InternalFieldType::kDocumentType: | |
| 134 return &V8Document::wrapperTypeInfo; | |
| 135 case InternalFieldType::kHTMLDocumentType: | |
| 136 return &V8HTMLDocument::wrapperTypeInfo; | |
| 137 case InternalFieldType::kHTMLDocumentObject: | |
| 138 return &V8HTMLDocument::wrapperTypeInfo; | |
| 139 } | |
| 140 NOTREACHED(); | |
| 141 return nullptr; | |
| 142 } | |
| 143 | |
| 144 struct DataForDeserializer { | |
|
haraken
2017/07/06 13:15:53
struct => class
Yuki
2017/07/06 14:12:28
I'm okay with "struct => class", but in that case,
Yuki
2017/07/06 14:15:25
Oops, maybe not okay. I'm not sure, but V8 would
peria
2017/07/07 06:22:00
Hmm, is there any background to do it?
I feel it n
| |
| 145 STACK_ALLOCATED(); | |
| 146 Member<Document> document; | |
| 147 }; | |
| 148 | |
| 149 int CountExternalReferenceEntries() { | |
| 150 if (!g_snapshot_reference_table) | |
| 151 return 0; | |
| 152 | |
| 153 int count = 0; | |
| 154 for (intptr_t* p = g_snapshot_reference_table; *p; ++p) | |
| 155 ++count; | |
| 156 return count; | |
| 157 } | |
| 158 | |
| 159 } // namespace | |
| 160 | |
| 161 v8::Local<v8::Context> V8SnapshotUtil::CreateContext( | |
|
haraken
2017/07/06 13:15:53
CreateContextFromSnapshot
peria
2017/07/07 06:21:58
Done.
| |
| 162 v8::Isolate* isolate, | |
| 163 const DOMWrapperWorld& world, | |
| 164 v8::ExtensionConfiguration* extension_configuration, | |
| 165 v8::Local<v8::Object> global_proxy, | |
| 166 Document* document) { | |
| 167 if (!CanCreateContextFromSnapshot(isolate, world, document)) { | |
| 168 return v8::Local<v8::Context>(); | |
| 169 } | |
| 170 | |
| 171 const int index = GetSnapshotIndexForWorld(world); | |
| 172 DataForDeserializer data{document}; | |
| 173 v8::DeserializeInternalFieldsCallback callback = | |
| 174 v8::DeserializeInternalFieldsCallback(&DeserializeInternalField, &data); | |
| 175 v8::Local<v8::Context> context = | |
| 176 v8::Context::FromSnapshot(isolate, index, callback, | |
| 177 extension_configuration, global_proxy) | |
| 178 .ToLocalChecked(); | |
| 179 VLOG(1) << "A context is created from snapshot for " | |
| 180 << (world.IsMainWorld() ? "" : "non-") << "main world"; | |
| 181 | |
| 182 return context; | |
| 183 } | |
| 184 | |
| 185 void V8SnapshotUtil::SetupContext(v8::Local<v8::Context> context, | |
|
haraken
2017/07/06 13:15:54
SetupContext => InstallRuntimeEnabledFeatures
peria
2017/07/07 06:22:02
Done.
| |
| 186 Document* document) { | |
| 187 ScriptState* script_state = ScriptState::From(context); | |
| 188 v8::Isolate* isolate = script_state->GetIsolate(); | |
| 189 const DOMWrapperWorld& world = script_state->World(); | |
| 190 if (!CanCreateContextFromSnapshot(isolate, world, document)) { | |
| 191 return; | |
| 192 } | |
| 193 | |
| 194 TRACE_EVENT1("v8", "InstallRuntimeEnabled", "IsMainFrame", | |
|
haraken
2017/07/06 13:15:53
"V8SnapshotUtil::InstallRuntimeEnabledFeatures"
peria
2017/07/07 06:22:03
Done.
| |
| 195 world.IsMainWorld()); | |
| 196 | |
| 197 v8::Local<v8::String> prototype_str = V8AtomicString(isolate, "prototype"); | |
| 198 V8PerContextData* data = script_state->PerContextData(); | |
| 199 | |
| 200 v8::Local<v8::Object> global_proxy = context->Global(); | |
| 201 { | |
| 202 v8::Local<v8::Object> window_wrapper = | |
| 203 global_proxy->GetPrototype().As<v8::Object>(); | |
| 204 const WrapperTypeInfo* type = &V8Window::wrapperTypeInfo; | |
| 205 v8::Local<v8::Function> interface = data->ConstructorForType(type); | |
|
haraken
2017/07/06 13:15:53
interface => instance
Yuki
2017/07/06 14:12:28
No, this is not a platform object (aka instance ob
peria
2017/07/07 06:21:58
+1 to Yuki.
| |
| 206 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) | |
| 207 .ToLocalChecked() | |
| 208 .As<v8::Object>(); | |
| 209 V8Window::install_runtime_enabled_features_function_( | |
| 210 isolate, world, window_wrapper, prototype, interface); | |
| 211 } | |
| 212 { | |
| 213 const WrapperTypeInfo* type = &V8EventTarget::wrapperTypeInfo; | |
| 214 v8::Local<v8::Function> interface = data->ConstructorForType(type); | |
|
haraken
2017/07/06 13:15:53
interface => instance
peria
2017/07/07 06:21:58
Acknowledged.
| |
| 215 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) | |
| 216 .ToLocalChecked() | |
| 217 .As<v8::Object>(); | |
| 218 V8EventTarget::InstallRuntimeEnabledFeatures( | |
| 219 isolate, world, v8::Local<v8::Object>(), prototype, interface); | |
| 220 } | |
| 221 | |
| 222 if (!world.IsMainWorld()) { | |
|
haraken
2017/07/06 13:15:53
Why is it okay to skip the rest of this method for
Yuki
2017/07/06 14:12:28
We lazily instantiate a V8 wrapper for document in
haraken
2017/07/06 14:22:13
Makes sense.
The logic to handle the eagerly inst
| |
| 223 return; | |
| 224 } | |
| 225 | |
| 226 { | |
| 227 CHECK(document); | |
| 228 DCHECK(document->IsHTMLDocument()); | |
| 229 CHECK(document->ContainsWrapper()); | |
| 230 v8::Local<v8::Object> document_wrapper = | |
| 231 ToV8(document, global_proxy, isolate).As<v8::Object>(); | |
| 232 const WrapperTypeInfo* type = &V8HTMLDocument::wrapperTypeInfo; | |
| 233 v8::Local<v8::Function> interface = data->ConstructorForType(type); | |
|
haraken
2017/07/06 13:15:53
interface => instance
peria
2017/07/07 06:22:01
Acknowledged.
| |
| 234 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) | |
| 235 .ToLocalChecked() | |
| 236 .As<v8::Object>(); | |
| 237 V8HTMLDocument::InstallRuntimeEnabledFeatures( | |
| 238 isolate, world, document_wrapper, prototype, interface); | |
| 239 } | |
| 240 { | |
| 241 const WrapperTypeInfo* type = &V8Document::wrapperTypeInfo; | |
| 242 v8::Local<v8::Function> interface = data->ConstructorForType(type); | |
|
haraken
2017/07/06 13:15:53
interface => instance
peria
2017/07/10 03:39:12
Acknowledged.
| |
| 243 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) | |
| 244 .ToLocalChecked() | |
| 245 .As<v8::Object>(); | |
| 246 V8Document::InstallRuntimeEnabledFeatures( | |
| 247 isolate, world, v8::Local<v8::Object>(), prototype, interface); | |
| 248 } | |
| 249 { | |
| 250 const WrapperTypeInfo* type = &V8Node::wrapperTypeInfo; | |
| 251 v8::Local<v8::Function> interface = data->ConstructorForType(type); | |
|
haraken
2017/07/06 13:15:54
interface => instance
peria
2017/07/07 06:21:57
Acknowledged.
| |
| 252 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) | |
| 253 .ToLocalChecked() | |
| 254 .As<v8::Object>(); | |
| 255 V8Node::InstallRuntimeEnabledFeatures( | |
| 256 isolate, world, v8::Local<v8::Object>(), prototype, interface); | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 void V8SnapshotUtil::EnsureInterfaceTemplates(v8::Isolate* isolate) { | |
| 261 if (V8PerIsolateData::From(isolate)->GetV8ContextMode() != | |
| 262 V8PerIsolateData::V8ContextMode::kUseSnapshot) { | |
| 263 return; | |
| 264 } | |
| 265 | |
| 266 SnapshotInterface& snapshot_window = g_snapshot_interfaces[0]; | |
| 267 DCHECK(V8Window::wrapperTypeInfo.Equals(snapshot_window.wrapper_type_info)); | |
| 268 // Update the install function for V8Window to work for partial interfaces. | |
| 269 snapshot_window.install_function = | |
| 270 V8Window::install_runtime_enabled_features_on_template_function_; | |
| 271 | |
| 272 EnsureInterfaceTemplatesForWorld(isolate, DOMWrapperWorld::MainWorld()); | |
| 273 EnsureInterfaceTemplatesForWorld( | |
| 274 isolate, | |
| 275 *DOMWrapperWorld::EnsureIsolatedWorld(isolate, kWorldIdForNonMainWorld)); | |
|
haraken
2017/07/06 13:15:53
This forcibly creates an isolated world. Not only
peria
2017/07/10 03:39:12
Done.
| |
| 276 } | |
| 277 | |
| 278 void V8SnapshotUtil::SetReferenceTable(intptr_t* table) { | |
| 279 DCHECK(!g_snapshot_reference_table); | |
| 280 g_snapshot_reference_table = table; | |
| 281 } | |
| 282 | |
| 283 intptr_t* V8SnapshotUtil::GetReferenceTable() { | |
| 284 DCHECK(g_snapshot_reference_table); | |
| 285 return g_snapshot_reference_table; | |
| 286 } | |
| 287 | |
| 288 v8::StartupData V8SnapshotUtil::TakeSnapshot() { | |
| 289 DCHECK_EQ(V8PerIsolateData::From(V8PerIsolateData::MainThreadIsolate()) | |
| 290 ->GetV8ContextMode(), | |
| 291 V8PerIsolateData::V8ContextMode::kTakeSnapshot); | |
| 292 | |
| 293 v8::SnapshotCreator* creator = | |
| 294 V8PerIsolateData::From(V8PerIsolateData::MainThreadIsolate()) | |
| 295 ->GetSnapshotCreator(); | |
| 296 v8::Isolate* isolate = creator->GetIsolate(); | |
| 297 CHECK_EQ(isolate, v8::Isolate::GetCurrent()); | |
| 298 | |
| 299 VLOG(1) << "External reference table has " << CountExternalReferenceEntries() | |
| 300 << " entries."; | |
| 301 | |
| 302 // Disable all runtime enabled featuers | |
|
haraken
2017/07/06 13:15:53
features
peria
2017/07/07 06:21:58
Done.
| |
| 303 RuntimeEnabledFeatures::SetStableFeaturesEnabled(false); | |
| 304 RuntimeEnabledFeatures::SetExperimentalFeaturesEnabled(false); | |
| 305 RuntimeEnabledFeatures::SetTestFeaturesEnabled(false); | |
| 306 | |
| 307 { | |
| 308 v8::HandleScope handleScope(isolate); | |
| 309 creator->SetDefaultContext(v8::Context::New(isolate)); | |
|
haraken
2017/07/06 13:15:54
Why do we need to create a context here? Won't it
Yuki
2017/07/06 14:12:28
This is the *default* context that is not associat
| |
| 310 | |
| 311 TakeSnapshotForWorld(creator, DOMWrapperWorld::MainWorld()); | |
| 312 // For non main worlds, we can use any type to create a context. | |
| 313 TakeSnapshotForWorld(creator, *DOMWrapperWorld::EnsureIsolatedWorld( | |
| 314 isolate, kWorldIdForNonMainWorld)); | |
| 315 } | |
| 316 | |
| 317 // Snapshot is taken on the main thread, but it can be used on other threads. | |
| 318 // So we remove a message handler for the main thread. | |
|
haraken
2017/07/06 13:15:53
Other threads don't use the snapshot, right?
peria
2017/07/07 06:21:56
As for now, it is correct, but we don't have to ma
| |
| 319 isolate->RemoveMessageListeners(V8Initializer::MessageHandlerInMainThread); | |
| 320 | |
| 321 return creator->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); | |
| 322 } | |
| 323 | |
| 324 v8::StartupData V8SnapshotUtil::SerializeInternalField( | |
| 325 v8::Local<v8::Object> object, | |
| 326 int index, | |
| 327 void*) { | |
| 328 InternalFieldType field_type = InternalFieldType::kNone; | |
| 329 const WrapperTypeInfo* wrapper_type = ToWrapperTypeInfo(object); | |
| 330 if (kV8DOMWrapperObjectIndex == index) { | |
| 331 if (blink::V8HTMLDocument::wrapperTypeInfo.Equals(wrapper_type)) { | |
| 332 field_type = InternalFieldType::kHTMLDocumentObject; | |
| 333 } | |
| 334 } else if (kV8DOMWrapperTypeIndex == index) { | |
| 335 if (blink::V8HTMLDocument::wrapperTypeInfo.Equals(wrapper_type)) { | |
| 336 field_type = InternalFieldType::kHTMLDocumentType; | |
| 337 } else if (blink::V8Document::wrapperTypeInfo.Equals(wrapper_type)) { | |
| 338 field_type = InternalFieldType::kDocumentType; | |
| 339 } else if (blink::V8Node::wrapperTypeInfo.Equals(wrapper_type)) { | |
| 340 field_type = InternalFieldType::kNodeType; | |
| 341 } | |
| 342 } | |
| 343 CHECK_NE(field_type, InternalFieldType::kNone); | |
| 344 | |
| 345 int size = sizeof(InternalFieldType); | |
| 346 char* data = new char[size]; | |
|
haraken
2017/07/06 13:15:52
Who deallocates the memory?
peria
2017/07/07 06:22:00
V8's snapshot serializer does.
https://cs.chromium
haraken
2017/07/07 06:59:11
OK, let's add a comment.
peria
2017/07/10 03:39:13
Done.
| |
| 347 std::memcpy(data, &field_type, size); | |
| 348 | |
| 349 return {data, size}; | |
| 350 } | |
| 351 | |
| 352 void V8SnapshotUtil::DeserializeInternalField(v8::Local<v8::Object> object, | |
| 353 int index, | |
| 354 v8::StartupData payload, | |
| 355 void* ptr) { | |
| 356 // DeserializeInternalField() expects to be called in the main world | |
| 357 // with |document| being HTMLDocument. | |
| 358 CHECK_EQ(payload.raw_size, static_cast<int>(sizeof(InternalFieldType))); | |
| 359 InternalFieldType type = | |
| 360 *reinterpret_cast<const InternalFieldType*>(payload.data); | |
| 361 | |
| 362 const WrapperTypeInfo* wrapper_type_info = FieldTypeToWrapperTypeInfo(type); | |
| 363 switch (type) { | |
| 364 case InternalFieldType::kNodeType: | |
| 365 case InternalFieldType::kDocumentType: | |
| 366 case InternalFieldType::kHTMLDocumentType: { | |
| 367 CHECK_EQ(index, kV8DOMWrapperTypeIndex); | |
| 368 object->SetAlignedPointerInInternalField( | |
| 369 index, const_cast<WrapperTypeInfo*>(wrapper_type_info)); | |
| 370 return; | |
| 371 } | |
| 372 case InternalFieldType::kHTMLDocumentObject: { | |
| 373 CHECK_EQ(index, kV8DOMWrapperObjectIndex); | |
| 374 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 375 DataForDeserializer* data = static_cast<DataForDeserializer*>(ptr); | |
| 376 ScriptWrappable* document = data->document; | |
| 377 DCHECK(document); | |
| 378 | |
| 379 // Make reference from wrapper to document | |
| 380 object->SetAlignedPointerInInternalField(index, document); | |
| 381 // Make reference from document to wrapper | |
| 382 CHECK(document->SetWrapper(isolate, wrapper_type_info, object)); | |
| 383 WrapperTypeInfo::WrapperCreated(); | |
| 384 return; | |
| 385 } | |
| 386 case InternalFieldType::kNone: | |
| 387 NOTREACHED(); | |
| 388 return; | |
| 389 } | |
| 390 | |
| 391 NOTREACHED(); | |
| 392 } | |
| 393 | |
| 394 bool V8SnapshotUtil::CanCreateContextFromSnapshot(v8::Isolate* isolate, | |
| 395 const DOMWrapperWorld& world, | |
| 396 Document* document) { | |
| 397 DCHECK(document); | |
| 398 if (V8PerIsolateData::From(isolate)->GetV8ContextMode() != | |
| 399 V8PerIsolateData::V8ContextMode::kUseSnapshot) { | |
| 400 return false; | |
| 401 } | |
| 402 | |
| 403 // In case we create a context for the main world from snapshot, we also need | |
| 404 // a HTMLDocument. | |
|
haraken
2017/07/06 13:15:52
Would you help me understand why we need this cond
Yuki
2017/07/06 14:12:28
We eagerly instantiates the V8 wrapper for window.
peria
2017/07/07 06:21:55
Done.
peria
2017/07/07 06:22:00
Acknowledged.
| |
| 405 return !world.IsMainWorld() || document->IsHTMLDocument(); | |
| 406 } | |
| 407 | |
| 408 void V8SnapshotUtil::EnsureInterfaceTemplatesForWorld( | |
| 409 v8::Isolate* isolate, | |
| 410 const DOMWrapperWorld& world) { | |
| 411 V8PerIsolateData* data = V8PerIsolateData::From(isolate); | |
| 412 const int index_offset = world.IsMainWorld() ? 0 : kSnapshotInterfaceSize; | |
| 413 | |
| 414 for (size_t i = 0; i < kSnapshotInterfaceSize; ++i) { | |
| 415 auto& snapshot_interface = g_snapshot_interfaces[i]; | |
| 416 const WrapperTypeInfo* wrapper_type_info = | |
| 417 snapshot_interface.wrapper_type_info; | |
| 418 v8::Local<v8::FunctionTemplate> interface_template = | |
| 419 v8::FunctionTemplate::FromSnapshot(isolate, index_offset + i) | |
|
haraken
2017/07/06 13:15:54
Add a comment about what index_offset+i is doing.
peria
2017/07/07 06:21:58
Done.
| |
| 420 .ToLocalChecked(); | |
| 421 snapshot_interface.install_function(isolate, world, interface_template); | |
| 422 CHECK(!interface_template.IsEmpty()); | |
| 423 data->SetInterfaceTemplate(world, wrapper_type_info, interface_template); | |
| 424 } | |
| 425 } | |
| 426 | |
| 427 void V8SnapshotUtil::TakeSnapshotForWorld(v8::SnapshotCreator* creator, | |
| 428 const DOMWrapperWorld& world) { | |
| 429 v8::Isolate* isolate = creator->GetIsolate(); | |
| 430 CHECK_EQ(isolate, v8::Isolate::GetCurrent()); | |
| 431 | |
| 432 // Function templates | |
| 433 v8::HandleScope handleScope(isolate); | |
| 434 std::array<v8::Local<v8::FunctionTemplate>, kSnapshotInterfaceSize> | |
|
haraken
2017/07/06 13:15:52
Use WTF::Vector.
Yuki
2017/07/06 14:12:28
IIUC, it's wrong to put a v8::Local<T> on heap. v
| |
| 435 interface_templates; | |
| 436 v8::Local<v8::FunctionTemplate> window_template; | |
| 437 for (size_t i = 0; i < kSnapshotInterfaceSize; ++i) { | |
| 438 const WrapperTypeInfo* wrapper_type_info = | |
| 439 g_snapshot_interfaces[i].wrapper_type_info; | |
| 440 v8::Local<v8::FunctionTemplate> interface_template = | |
| 441 wrapper_type_info->domTemplate(isolate, world); | |
| 442 CHECK(!interface_template.IsEmpty()); | |
| 443 interface_templates[i] = interface_template; | |
| 444 if (V8Window::wrapperTypeInfo.Equals(wrapper_type_info)) { | |
| 445 window_template = interface_template; | |
| 446 } | |
| 447 } | |
| 448 CHECK(!window_template.IsEmpty()); | |
| 449 | |
| 450 v8::Local<v8::ObjectTemplate> window_instance_template = | |
| 451 window_template->InstanceTemplate(); | |
| 452 CHECK(!window_instance_template.IsEmpty()); | |
| 453 | |
| 454 v8::Local<v8::Context> context; | |
| 455 { | |
| 456 V8PerIsolateData::UseCounterDisabledScope use_counter_disabled( | |
|
haraken
2017/07/06 13:15:54
Add a comment about why we need this.
Yuki
2017/07/06 14:12:28
UseCOunterDisabledScope has its own excuse on thei
peria
2017/07/07 06:21:57
+1
peria
2017/07/07 06:21:59
Acknowledged.
| |
| 457 V8PerIsolateData::From(isolate)); | |
| 458 context = v8::Context::New(isolate, nullptr, window_instance_template); | |
| 459 } | |
| 460 CHECK(!context.IsEmpty()); | |
| 461 | |
| 462 if (world.IsMainWorld()) { | |
|
haraken
2017/07/06 13:15:53
Add a comment about what this block is doing.
peria
2017/07/07 06:21:55
Done.
| |
| 463 v8::Context::Scope scope(context); | |
| 464 v8::Local<v8::Object> document_wrapper = CreatePlainWrapper( | |
| 465 isolate, world, context, &V8HTMLDocument::wrapperTypeInfo); | |
| 466 int indices[] = {kV8DOMWrapperObjectIndex, kV8DOMWrapperTypeIndex}; | |
| 467 void* values[] = {nullptr, const_cast<WrapperTypeInfo*>( | |
| 468 &V8HTMLDocument::wrapperTypeInfo)}; | |
| 469 document_wrapper->SetAlignedPointerInInternalFields( | |
| 470 WTF_ARRAY_LENGTH(indices), indices, values); | |
| 471 | |
| 472 // Set the cached accessor for window.document. | |
| 473 CHECK(V8PrivateProperty::GetWindowDocumentCachedAccessor(isolate).Set( | |
| 474 context->Global(), document_wrapper)); | |
| 475 } | |
| 476 | |
| 477 for (auto& interface_template : interface_templates) { | |
| 478 creator->AddTemplate(interface_template); | |
| 479 } | |
| 480 creator->AddContext(context, SerializeInternalField); | |
| 481 | |
| 482 V8PerIsolateData::From(isolate)->ClearPersistentsForV8Snapshot(); | |
|
haraken
2017/07/06 13:15:53
We should call this outside TakeSnapshotForWorld.
Yuki
2017/07/06 14:12:28
I don't see a benefit to move this out.
TakeSna
haraken
2017/07/06 14:22:13
I'm confused. Why do we need to call CleanUp() eve
Yuki
2017/07/06 14:40:09
My understanding is that:
a) For snapshotting, we
peria
2017/07/07 06:21:58
Yes, Yuki's understanding is correct, and c) is th
| |
| 483 } | |
| 484 | |
| 485 } // namespace blink | |
| OLD | NEW |