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 | |
13 namespace v8 { | 19 namespace v8 { |
14 namespace internal { | 20 namespace internal { |
15 | 21 |
16 | 22 static v8::StartupData external_startup_blob = {NULL, 0}; |
vogelheim
2014/12/10 11:41:58
Hmm... does this count as a static initializer, wh
| |
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 | |
62 | 23 |
63 void SetSnapshotFromFile(StartupData* snapshot_blob) { | 24 void SetSnapshotFromFile(StartupData* snapshot_blob) { |
64 DCHECK(snapshot_blob); | 25 DCHECK(snapshot_blob); |
65 DCHECK(snapshot_blob->data); | 26 DCHECK(snapshot_blob->data); |
66 DCHECK(snapshot_blob->raw_size > 0); | 27 DCHECK(snapshot_blob->raw_size > 0); |
67 DCHECK(!snapshot_impl_); | 28 DCHECK(!external_startup_blob.data); |
29 external_startup_blob = *snapshot_blob; | |
68 | 30 |
69 snapshot_impl_ = new SnapshotImpl; | 31 // Validate snapshot blob. |
70 SnapshotByteSource source(reinterpret_cast<const byte*>(snapshot_blob->data), | 32 DCHECK(!Snapshot::StartupSnapshot().is_empty()); |
71 snapshot_blob->raw_size); | 33 DCHECK(!Snapshot::ContextSnapshot().is_empty()); |
72 bool success = source.GetBlob(&snapshot_impl_->data, | |
73 &snapshot_impl_->size); | |
74 success &= source.GetBlob(&snapshot_impl_->context_data, | |
75 &snapshot_impl_->context_size); | |
76 DCHECK(success); | |
77 } | 34 } |
78 | 35 |
36 | |
37 const v8::StartupData Snapshot::SnapshotBlob() { return external_startup_blob; } | |
79 } } // namespace v8::internal | 38 } } // namespace v8::internal |
OLD | NEW |