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 // The common functionality when building with or without snapshots. | 5 // The common functionality when building with or without snapshots. |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
11 #include "src/serialize.h" | 11 #include "src/serialize.h" |
12 #include "src/snapshot.h" | 12 #include "src/snapshot.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 namespace internal { | 15 namespace internal { |
16 | 16 |
17 bool Snapshot::HaveASnapshotToStartFrom() { | 17 bool Snapshot::HaveASnapshotToStartFrom() { |
18 return SnapshotBlob().data != NULL; | 18 return SnapshotBlob().data != NULL; |
19 } | 19 } |
20 | 20 |
21 | 21 |
22 const Vector<const byte> Snapshot::StartupSnapshot() { | |
23 DCHECK(HaveASnapshotToStartFrom()); | |
24 const v8::StartupData blob = SnapshotBlob(); | |
25 SnapshotByteSource source(blob.data, blob.raw_size); | |
26 const byte* data; | |
27 int length; | |
28 bool success = source.GetBlob(&data, &length); | |
29 CHECK(success); | |
30 return Vector<const byte>(data, length); | |
31 } | |
32 | |
33 | |
34 const Vector<const byte> Snapshot::ContextSnapshot() { | |
35 DCHECK(HaveASnapshotToStartFrom()); | |
36 const v8::StartupData blob = SnapshotBlob(); | |
37 SnapshotByteSource source(blob.data, blob.raw_size); | |
38 const byte* data; | |
39 int length; | |
40 bool success = source.GetBlob(&data, &length); | |
41 success &= source.GetBlob(&data, &length); | |
42 CHECK(success); | |
43 return Vector<const byte>(data, length); | |
44 } | |
45 | |
46 | |
47 bool Snapshot::Initialize(Isolate* isolate) { | 22 bool Snapshot::Initialize(Isolate* isolate) { |
48 if (!HaveASnapshotToStartFrom()) return false; | 23 if (!HaveASnapshotToStartFrom()) return false; |
49 base::ElapsedTimer timer; | 24 base::ElapsedTimer timer; |
50 if (FLAG_profile_deserialization) timer.Start(); | 25 if (FLAG_profile_deserialization) timer.Start(); |
51 | 26 |
52 SnapshotData snapshot_data(StartupSnapshot()); | 27 const v8::StartupData blob = SnapshotBlob(); |
| 28 SnapshotData snapshot_data(ExtractStartupData(&blob)); |
53 Deserializer deserializer(&snapshot_data); | 29 Deserializer deserializer(&snapshot_data); |
54 bool success = isolate->Init(&deserializer); | 30 bool success = isolate->Init(&deserializer); |
55 if (FLAG_profile_deserialization) { | 31 if (FLAG_profile_deserialization) { |
56 double ms = timer.Elapsed().InMillisecondsF(); | 32 double ms = timer.Elapsed().InMillisecondsF(); |
57 PrintF("[Snapshot loading and deserialization took %0.3f ms]\n", ms); | 33 PrintF("[Snapshot loading and deserialization took %0.3f ms]\n", ms); |
58 } | 34 } |
59 return success; | 35 return success; |
60 } | 36 } |
61 | 37 |
62 | 38 |
63 Handle<Context> Snapshot::NewContextFromSnapshot(Isolate* isolate) { | 39 Handle<Context> Snapshot::NewContextFromSnapshot(Isolate* isolate) { |
64 if (!HaveASnapshotToStartFrom()) return Handle<Context>(); | 40 if (!HaveASnapshotToStartFrom()) return Handle<Context>(); |
65 | 41 |
66 SnapshotData snapshot_data(ContextSnapshot()); | 42 const v8::StartupData blob = SnapshotBlob(); |
| 43 SnapshotData snapshot_data(ExtractContextData(&blob)); |
67 Deserializer deserializer(&snapshot_data); | 44 Deserializer deserializer(&snapshot_data); |
68 Object* root; | 45 Object* root; |
69 deserializer.DeserializePartial(isolate, &root); | 46 deserializer.DeserializePartial(isolate, &root); |
70 CHECK(root->IsContext()); | 47 CHECK(root->IsContext()); |
71 return Handle<Context>(Context::cast(root)); | 48 return Handle<Context>(Context::cast(root)); |
72 } | 49 } |
73 | 50 |
| 51 |
| 52 v8::StartupData Snapshot::CreateSnapshotBlob( |
| 53 const Vector<const byte> startup_data, |
| 54 const Vector<const byte> context_data) { |
| 55 int startup_length = startup_data.length(); |
| 56 int context_length = context_data.length(); |
| 57 int context_offset = kIntSize + startup_length; |
| 58 int length = context_offset + context_length; |
| 59 char* data = new char[length]; |
| 60 |
| 61 memcpy(data, &startup_length, kIntSize); |
| 62 memcpy(data + kIntSize, startup_data.begin(), startup_length); |
| 63 memcpy(data + context_offset, context_data.begin(), context_length); |
| 64 v8::StartupData result = {data, length}; |
| 65 return result; |
| 66 } |
| 67 |
| 68 |
| 69 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) { |
| 70 DCHECK_LT(kIntSize, data->raw_size); |
| 71 int startup_length; |
| 72 memcpy(&startup_length, data->data, kIntSize); |
| 73 DCHECK_LT(startup_length, data->raw_size); |
| 74 const byte* startup_data = |
| 75 reinterpret_cast<const byte*>(data->data + kIntSize); |
| 76 return Vector<const byte>(startup_data, startup_length); |
| 77 } |
| 78 |
| 79 |
| 80 Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) { |
| 81 DCHECK_LT(kIntSize, data->raw_size); |
| 82 int startup_length; |
| 83 memcpy(&startup_length, data->data, kIntSize); |
| 84 int context_offset = kIntSize + startup_length; |
| 85 const byte* context_data = |
| 86 reinterpret_cast<const byte*>(data->data + context_offset); |
| 87 DCHECK_LT(context_offset, data->raw_size); |
| 88 int context_length = data->raw_size - context_offset; |
| 89 return Vector<const byte>(context_data, context_length); |
| 90 } |
74 } } // namespace v8::internal | 91 } } // namespace v8::internal |
OLD | NEW |