OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 // Used for building with external snapshots. | 5 // Used for building with external snapshots. |
6 | 6 |
7 #include "src/snapshot.h" | 7 #include "src/snapshot.h" |
8 | 8 |
9 #include "src/serialize.h" | 9 #include "src/serialize.h" |
10 #include "src/snapshot-source-sink.h" | 10 #include "src/snapshot-source-sink.h" |
11 #include "src/v8.h" // for V8::Initialize | 11 #include "src/v8.h" // for V8::Initialize |
12 | 12 |
13 | |
14 #ifndef V8_USE_EXTERNAL_STARTUP_DATA | |
15 #error snapshot-external.cc is used only for the external snapshot build. | |
16 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
17 | |
18 | |
19 namespace v8 { | 13 namespace v8 { |
20 namespace internal { | 14 namespace internal { |
21 | 15 |
22 static v8::StartupData* external_startup_blob = NULL; | 16 |
| 17 struct SnapshotImpl { |
| 18 public: |
| 19 const byte* data; |
| 20 int size; |
| 21 const byte* context_data; |
| 22 int context_size; |
| 23 }; |
| 24 |
| 25 |
| 26 static SnapshotImpl* snapshot_impl_ = NULL; |
| 27 |
| 28 |
| 29 bool Snapshot::HaveASnapshotToStartFrom() { |
| 30 return snapshot_impl_ != NULL; |
| 31 } |
| 32 |
| 33 |
| 34 bool Snapshot::Initialize(Isolate* isolate) { |
| 35 if (!HaveASnapshotToStartFrom()) return false; |
| 36 base::ElapsedTimer timer; |
| 37 if (FLAG_profile_deserialization) timer.Start(); |
| 38 |
| 39 SnapshotData snapshot_data(snapshot_impl_->data, snapshot_impl_->size); |
| 40 Deserializer deserializer(&snapshot_data); |
| 41 bool success = isolate->Init(&deserializer); |
| 42 if (FLAG_profile_deserialization) { |
| 43 double ms = timer.Elapsed().InMillisecondsF(); |
| 44 PrintF("[Snapshot loading and deserialization took %0.3f ms]\n", ms); |
| 45 } |
| 46 return success; |
| 47 } |
| 48 |
| 49 |
| 50 Handle<Context> Snapshot::NewContextFromSnapshot(Isolate* isolate) { |
| 51 if (!HaveASnapshotToStartFrom()) return Handle<Context>(); |
| 52 |
| 53 SnapshotData snapshot_data(snapshot_impl_->context_data, |
| 54 snapshot_impl_->context_size); |
| 55 Deserializer deserializer(&snapshot_data); |
| 56 Object* root; |
| 57 deserializer.DeserializePartial(isolate, &root); |
| 58 CHECK(root->IsContext()); |
| 59 return Handle<Context>(Context::cast(root)); |
| 60 } |
| 61 |
23 | 62 |
24 void SetSnapshotFromFile(StartupData* snapshot_blob) { | 63 void SetSnapshotFromFile(StartupData* snapshot_blob) { |
25 DCHECK(snapshot_blob); | 64 DCHECK(snapshot_blob); |
26 DCHECK(snapshot_blob->data); | 65 DCHECK(snapshot_blob->data); |
27 DCHECK(snapshot_blob->raw_size > 0); | 66 DCHECK(snapshot_blob->raw_size > 0); |
28 DCHECK(!external_startup_blob); | 67 DCHECK(!snapshot_impl_); |
29 external_startup_blob = snapshot_blob; | |
30 | 68 |
31 // Validate snapshot blob. | 69 snapshot_impl_ = new SnapshotImpl; |
32 DCHECK(!Snapshot::StartupSnapshot().is_empty()); | 70 SnapshotByteSource source(reinterpret_cast<const byte*>(snapshot_blob->data), |
33 DCHECK(!Snapshot::ContextSnapshot().is_empty()); | 71 snapshot_blob->raw_size); |
34 } | 72 bool success = source.GetBlob(&snapshot_impl_->data, |
35 | 73 &snapshot_impl_->size); |
36 | 74 success &= source.GetBlob(&snapshot_impl_->context_data, |
37 const v8::StartupData Snapshot::SnapshotBlob() { | 75 &snapshot_impl_->context_size); |
38 return *external_startup_blob; | 76 DCHECK(success); |
39 } | 77 } |
40 | 78 |
41 } } // namespace v8::internal | 79 } } // namespace v8::internal |
OLD | NEW |