OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/api.h" | 5 #include "src/api.h" |
6 | 6 |
7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 Isolate* isolate = data->isolate_; | 422 Isolate* isolate = data->isolate_; |
423 isolate->Exit(); | 423 isolate->Exit(); |
424 isolate->Dispose(); | 424 isolate->Dispose(); |
425 delete data; | 425 delete data; |
426 } | 426 } |
427 | 427 |
428 Isolate* SnapshotCreator::GetIsolate() { | 428 Isolate* SnapshotCreator::GetIsolate() { |
429 return SnapshotCreatorData::cast(data_)->isolate_; | 429 return SnapshotCreatorData::cast(data_)->isolate_; |
430 } | 430 } |
431 | 431 |
432 void SnapshotCreator::AddContext(Local<Context> context) { | 432 int SnapshotCreator::AddContext(Local<Context> context) { |
433 DCHECK(!context.IsEmpty()); | 433 DCHECK(!context.IsEmpty()); |
434 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); | 434 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); |
435 DCHECK(!data->created_); | 435 DCHECK(!data->created_); |
436 Isolate* isolate = data->isolate_; | 436 Isolate* isolate = data->isolate_; |
437 CHECK_EQ(isolate, context->GetIsolate()); | 437 CHECK_EQ(isolate, context->GetIsolate()); |
| 438 int index = data->contexts_.Size(); |
438 data->contexts_.Append(context); | 439 data->contexts_.Append(context); |
| 440 return index; |
439 } | 441 } |
440 | 442 |
441 StartupData SnapshotCreator::CreateBlob( | 443 StartupData SnapshotCreator::CreateBlob( |
442 SnapshotCreator::FunctionCodeHandling function_code_handling) { | 444 SnapshotCreator::FunctionCodeHandling function_code_handling) { |
443 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); | 445 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); |
444 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_); | 446 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_); |
445 DCHECK(!data->created_); | 447 DCHECK(!data->created_); |
446 | 448 |
447 // If we don't do this then we end up with a stray root pointing at the | 449 // If we don't do this then we end up with a stray root pointing at the |
448 // context even after we have disposed of the context. | 450 // context even after we have disposed of the context. |
449 isolate->heap()->CollectAllAvailableGarbage("mksnapshot"); | 451 isolate->heap()->CollectAllAvailableGarbage("mksnapshot"); |
450 isolate->heap()->CompactWeakFixedArrays(); | 452 isolate->heap()->CompactWeakFixedArrays(); |
451 | 453 |
452 i::DisallowHeapAllocation no_gc_from_here_on; | 454 i::DisallowHeapAllocation no_gc_from_here_on; |
453 | 455 |
454 int num_contexts = static_cast<int>(data->contexts_.Size()); | 456 int num_contexts = static_cast<int>(data->contexts_.Size()); |
455 i::List<i::Object*> contexts(num_contexts); | 457 i::List<i::Object*> contexts(num_contexts); |
456 for (int i = 0; i < num_contexts; i++) { | 458 for (int i = 0; i < num_contexts; i++) { |
457 i::HandleScope scope(isolate); | 459 i::HandleScope scope(isolate); |
458 i::Handle<i::Context> context = | 460 i::Handle<i::Context> context = |
459 v8::Utils::OpenHandle(*data->contexts_.Get(i)); | 461 v8::Utils::OpenHandle(*data->contexts_.Get(i)); |
460 contexts.Add(*context); | 462 contexts.Add(*context); |
461 } | 463 } |
462 data->contexts_.Clear(); | 464 data->contexts_.Clear(); |
463 | 465 |
464 i::StartupSerializer startup_serializer(isolate, function_code_handling); | 466 i::StartupSerializer startup_serializer(isolate, function_code_handling); |
465 startup_serializer.SerializeStrongReferences(); | 467 startup_serializer.SerializeStrongReferences(); |
466 | 468 |
467 i::PartialSerializer context_serializer(isolate, &startup_serializer); | 469 // Serialize each context with a new partial serializer. |
468 context_serializer.Serialize(&contexts[0]); | 470 i::List<i::SnapshotData*> context_snapshots(num_contexts); |
| 471 for (int i = 0; i < num_contexts; i++) { |
| 472 i::PartialSerializer partial_serializer(isolate, &startup_serializer); |
| 473 partial_serializer.Serialize(&contexts[i]); |
| 474 context_snapshots.Add(new i::SnapshotData(&partial_serializer)); |
| 475 } |
| 476 |
469 startup_serializer.SerializeWeakReferencesAndDeferred(); | 477 startup_serializer.SerializeWeakReferencesAndDeferred(); |
| 478 i::SnapshotData startup_snapshot(&startup_serializer); |
| 479 StartupData result = |
| 480 i::Snapshot::CreateSnapshotBlob(&startup_snapshot, &context_snapshots); |
470 | 481 |
| 482 // Delete heap-allocated context snapshot instances. |
| 483 for (const auto& context_snapshot : context_snapshots) { |
| 484 delete context_snapshot; |
| 485 } |
471 data->created_ = true; | 486 data->created_ = true; |
472 return i::Snapshot::CreateSnapshotBlob(&startup_serializer, | 487 return result; |
473 &context_serializer); | |
474 } | 488 } |
475 | 489 |
476 StartupData V8::CreateSnapshotDataBlob(const char* embedded_source) { | 490 StartupData V8::CreateSnapshotDataBlob(const char* embedded_source) { |
477 // Create a new isolate and a new context from scratch, optionally run | 491 // Create a new isolate and a new context from scratch, optionally run |
478 // a script to embed, and serialize to create a snapshot blob. | 492 // a script to embed, and serialize to create a snapshot blob. |
479 StartupData result = {nullptr, 0}; | 493 StartupData result = {nullptr, 0}; |
480 base::ElapsedTimer timer; | 494 base::ElapsedTimer timer; |
481 timer.Start(); | 495 timer.Start(); |
482 { | 496 { |
483 SnapshotCreator snapshot_creator; | 497 SnapshotCreator snapshot_creator; |
(...skipping 5026 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5510 void v8::V8::InitializeExternalStartupData(const char* natives_blob, | 5524 void v8::V8::InitializeExternalStartupData(const char* natives_blob, |
5511 const char* snapshot_blob) { | 5525 const char* snapshot_blob) { |
5512 i::InitializeExternalStartupData(natives_blob, snapshot_blob); | 5526 i::InitializeExternalStartupData(natives_blob, snapshot_blob); |
5513 } | 5527 } |
5514 | 5528 |
5515 | 5529 |
5516 const char* v8::V8::GetVersion() { | 5530 const char* v8::V8::GetVersion() { |
5517 return i::Version::GetVersion(); | 5531 return i::Version::GetVersion(); |
5518 } | 5532 } |
5519 | 5533 |
5520 | |
5521 static i::Handle<i::Context> CreateEnvironment( | 5534 static i::Handle<i::Context> CreateEnvironment( |
5522 i::Isolate* isolate, v8::ExtensionConfiguration* extensions, | 5535 i::Isolate* isolate, v8::ExtensionConfiguration* extensions, |
5523 v8::Local<ObjectTemplate> global_template, | 5536 v8::Local<ObjectTemplate> global_template, |
5524 v8::Local<Value> maybe_global_proxy) { | 5537 v8::Local<Value> maybe_global_proxy, int context_snapshot_index) { |
5525 i::Handle<i::Context> env; | 5538 i::Handle<i::Context> env; |
5526 | 5539 |
5527 // Enter V8 via an ENTER_V8 scope. | 5540 // Enter V8 via an ENTER_V8 scope. |
5528 { | 5541 { |
5529 ENTER_V8(isolate); | 5542 ENTER_V8(isolate); |
5530 v8::Local<ObjectTemplate> proxy_template = global_template; | 5543 v8::Local<ObjectTemplate> proxy_template = global_template; |
5531 i::Handle<i::FunctionTemplateInfo> proxy_constructor; | 5544 i::Handle<i::FunctionTemplateInfo> proxy_constructor; |
5532 i::Handle<i::FunctionTemplateInfo> global_constructor; | 5545 i::Handle<i::FunctionTemplateInfo> global_constructor; |
5533 | 5546 |
5534 if (!global_template.IsEmpty()) { | 5547 if (!global_template.IsEmpty()) { |
(...skipping 24 matching lines...) Expand all Loading... |
5559 } | 5572 } |
5560 } | 5573 } |
5561 | 5574 |
5562 i::Handle<i::Object> proxy = Utils::OpenHandle(*maybe_global_proxy, true); | 5575 i::Handle<i::Object> proxy = Utils::OpenHandle(*maybe_global_proxy, true); |
5563 i::MaybeHandle<i::JSGlobalProxy> maybe_proxy; | 5576 i::MaybeHandle<i::JSGlobalProxy> maybe_proxy; |
5564 if (!proxy.is_null()) { | 5577 if (!proxy.is_null()) { |
5565 maybe_proxy = i::Handle<i::JSGlobalProxy>::cast(proxy); | 5578 maybe_proxy = i::Handle<i::JSGlobalProxy>::cast(proxy); |
5566 } | 5579 } |
5567 // Create the environment. | 5580 // Create the environment. |
5568 env = isolate->bootstrapper()->CreateEnvironment( | 5581 env = isolate->bootstrapper()->CreateEnvironment( |
5569 maybe_proxy, proxy_template, extensions); | 5582 maybe_proxy, proxy_template, extensions, context_snapshot_index); |
5570 | 5583 |
5571 // Restore the access check info on the global template. | 5584 // Restore the access check info on the global template. |
5572 if (!global_template.IsEmpty()) { | 5585 if (!global_template.IsEmpty()) { |
5573 DCHECK(!global_constructor.is_null()); | 5586 DCHECK(!global_constructor.is_null()); |
5574 DCHECK(!proxy_constructor.is_null()); | 5587 DCHECK(!proxy_constructor.is_null()); |
5575 global_constructor->set_access_check_info( | 5588 global_constructor->set_access_check_info( |
5576 proxy_constructor->access_check_info()); | 5589 proxy_constructor->access_check_info()); |
5577 global_constructor->set_needs_access_check( | 5590 global_constructor->set_needs_access_check( |
5578 proxy_constructor->needs_access_check()); | 5591 proxy_constructor->needs_access_check()); |
5579 } | 5592 } |
5580 } | 5593 } |
5581 // Leave V8. | 5594 // Leave V8. |
5582 | 5595 |
5583 return env; | 5596 return env; |
5584 } | 5597 } |
5585 | 5598 |
5586 Local<Context> v8::Context::New(v8::Isolate* external_isolate, | 5599 Local<Context> v8::Context::New(v8::Isolate* external_isolate, |
5587 v8::ExtensionConfiguration* extensions, | 5600 v8::ExtensionConfiguration* extensions, |
5588 v8::Local<ObjectTemplate> global_template, | 5601 v8::Local<ObjectTemplate> global_template, |
5589 v8::Local<Value> global_object) { | 5602 v8::Local<Value> global_object, |
| 5603 int context_snapshot_index) { |
5590 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); | 5604 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); |
5591 LOG_API(isolate, Context, New); | 5605 LOG_API(isolate, Context, New); |
5592 i::HandleScope scope(isolate); | 5606 i::HandleScope scope(isolate); |
5593 ExtensionConfiguration no_extensions; | 5607 ExtensionConfiguration no_extensions; |
5594 if (extensions == NULL) extensions = &no_extensions; | 5608 if (extensions == NULL) extensions = &no_extensions; |
5595 i::Handle<i::Context> env = | 5609 i::Handle<i::Context> env = |
5596 CreateEnvironment(isolate, extensions, global_template, global_object); | 5610 CreateEnvironment(isolate, extensions, global_template, global_object, |
| 5611 context_snapshot_index); |
5597 if (env.is_null()) { | 5612 if (env.is_null()) { |
5598 if (isolate->has_pending_exception()) { | 5613 if (isolate->has_pending_exception()) { |
5599 isolate->OptionalRescheduleException(true); | 5614 isolate->OptionalRescheduleException(true); |
5600 } | 5615 } |
5601 return Local<Context>(); | 5616 return Local<Context>(); |
5602 } | 5617 } |
5603 return Utils::ToLocal(scope.CloseAndEscape(env)); | 5618 return Utils::ToLocal(scope.CloseAndEscape(env)); |
5604 } | 5619 } |
5605 | 5620 |
5606 | 5621 |
(...skipping 3190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8797 Address callback_address = | 8812 Address callback_address = |
8798 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 8813 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
8799 VMState<EXTERNAL> state(isolate); | 8814 VMState<EXTERNAL> state(isolate); |
8800 ExternalCallbackScope call_scope(isolate, callback_address); | 8815 ExternalCallbackScope call_scope(isolate, callback_address); |
8801 callback(info); | 8816 callback(info); |
8802 } | 8817 } |
8803 | 8818 |
8804 | 8819 |
8805 } // namespace internal | 8820 } // namespace internal |
8806 } // namespace v8 | 8821 } // namespace v8 |
OLD | NEW |