| 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 #ifdef DEBUG |
| 23 bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) { |
| 24 return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() && |
| 25 !Snapshot::ExtractContextData(snapshot_blob).is_empty(); |
| 26 } |
| 27 #endif // DEBUG |
| 28 |
| 29 |
| 30 bool Snapshot::EmbedsScript() { |
| 31 if (!HaveASnapshotToStartFrom()) return false; |
| 32 const v8::StartupData blob = SnapshotBlob(); |
| 33 return ExtractMetadata(&blob).embeds_script(); |
| 34 } |
| 35 |
| 36 |
| 22 bool Snapshot::Initialize(Isolate* isolate) { | 37 bool Snapshot::Initialize(Isolate* isolate) { |
| 23 if (!HaveASnapshotToStartFrom()) return false; | 38 if (!HaveASnapshotToStartFrom()) return false; |
| 24 base::ElapsedTimer timer; | 39 base::ElapsedTimer timer; |
| 25 if (FLAG_profile_deserialization) timer.Start(); | 40 if (FLAG_profile_deserialization) timer.Start(); |
| 26 | 41 |
| 27 const v8::StartupData blob = SnapshotBlob(); | 42 const v8::StartupData blob = SnapshotBlob(); |
| 28 Vector<const byte> startup_data = ExtractStartupData(&blob); | 43 Vector<const byte> startup_data = ExtractStartupData(&blob); |
| 29 SnapshotData snapshot_data(startup_data); | 44 SnapshotData snapshot_data(startup_data); |
| 30 Deserializer deserializer(&snapshot_data); | 45 Deserializer deserializer(&snapshot_data); |
| 31 bool success = isolate->Init(&deserializer); | 46 bool success = isolate->Init(&deserializer); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 47 const v8::StartupData blob = SnapshotBlob(); | 62 const v8::StartupData blob = SnapshotBlob(); |
| 48 Vector<const byte> context_data = ExtractContextData(&blob); | 63 Vector<const byte> context_data = ExtractContextData(&blob); |
| 49 SnapshotData snapshot_data(context_data); | 64 SnapshotData snapshot_data(context_data); |
| 50 Deserializer deserializer(&snapshot_data); | 65 Deserializer deserializer(&snapshot_data); |
| 51 | 66 |
| 52 MaybeHandle<Object> maybe_context = | 67 MaybeHandle<Object> maybe_context = |
| 53 deserializer.DeserializePartial(isolate, outdated_contexts_out); | 68 deserializer.DeserializePartial(isolate, outdated_contexts_out); |
| 54 Handle<Object> result; | 69 Handle<Object> result; |
| 55 if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>(); | 70 if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>(); |
| 56 CHECK(result->IsContext()); | 71 CHECK(result->IsContext()); |
| 72 // If the snapshot does not contain a custom script, we need to update |
| 73 // the global object for exactly one context. |
| 74 CHECK(EmbedsScript() || (*outdated_contexts_out)->length() == 1); |
| 57 if (FLAG_profile_deserialization) { | 75 if (FLAG_profile_deserialization) { |
| 58 double ms = timer.Elapsed().InMillisecondsF(); | 76 double ms = timer.Elapsed().InMillisecondsF(); |
| 59 int bytes = context_data.length(); | 77 int bytes = context_data.length(); |
| 60 PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms); | 78 PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms); |
| 61 } | 79 } |
| 62 return Handle<Context>::cast(result); | 80 return Handle<Context>::cast(result); |
| 63 } | 81 } |
| 64 | 82 |
| 65 | 83 |
| 66 v8::StartupData Snapshot::CreateSnapshotBlob( | 84 v8::StartupData Snapshot::CreateSnapshotBlob( |
| 67 const Vector<const byte> startup_data, | 85 const Vector<const byte> startup_data, |
| 68 const Vector<const byte> context_data) { | 86 const Vector<const byte> context_data, Snapshot::Metadata metadata) { |
| 69 int startup_length = startup_data.length(); | 87 int startup_length = startup_data.length(); |
| 70 int context_length = context_data.length(); | 88 int context_length = context_data.length(); |
| 71 int context_offset = kIntSize + startup_length; | 89 int context_offset = ContextOffset(startup_length); |
| 90 |
| 72 int length = context_offset + context_length; | 91 int length = context_offset + context_length; |
| 73 char* data = new char[length]; | 92 char* data = new char[length]; |
| 74 | 93 |
| 75 memcpy(data, &startup_length, kIntSize); | 94 memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size); |
| 76 memcpy(data + kIntSize, startup_data.begin(), startup_length); | 95 memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size); |
| 96 memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length); |
| 77 memcpy(data + context_offset, context_data.begin(), context_length); | 97 memcpy(data + context_offset, context_data.begin(), context_length); |
| 78 v8::StartupData result = {data, length}; | 98 v8::StartupData result = {data, length}; |
| 79 return result; | 99 return result; |
| 80 } | 100 } |
| 81 | 101 |
| 82 | 102 |
| 103 Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) { |
| 104 uint32_t raw; |
| 105 memcpy(&raw, data->data + kMetadataOffset, kInt32Size); |
| 106 return Metadata(raw); |
| 107 } |
| 108 |
| 109 |
| 83 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) { | 110 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) { |
| 84 DCHECK_LT(kIntSize, data->raw_size); | 111 DCHECK_LT(kIntSize, data->raw_size); |
| 85 int startup_length; | 112 int startup_length; |
| 86 memcpy(&startup_length, data->data, kIntSize); | 113 memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size); |
| 87 DCHECK_LT(startup_length, data->raw_size); | 114 DCHECK_LT(startup_length, data->raw_size); |
| 88 const byte* startup_data = | 115 const byte* startup_data = |
| 89 reinterpret_cast<const byte*>(data->data + kIntSize); | 116 reinterpret_cast<const byte*>(data->data + kStartupDataOffset); |
| 90 return Vector<const byte>(startup_data, startup_length); | 117 return Vector<const byte>(startup_data, startup_length); |
| 91 } | 118 } |
| 92 | 119 |
| 93 | 120 |
| 94 Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) { | 121 Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) { |
| 95 DCHECK_LT(kIntSize, data->raw_size); | 122 DCHECK_LT(kIntSize, data->raw_size); |
| 96 int startup_length; | 123 int startup_length; |
| 97 memcpy(&startup_length, data->data, kIntSize); | 124 memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize); |
| 98 int context_offset = kIntSize + startup_length; | 125 int context_offset = ContextOffset(startup_length); |
| 99 const byte* context_data = | 126 const byte* context_data = |
| 100 reinterpret_cast<const byte*>(data->data + context_offset); | 127 reinterpret_cast<const byte*>(data->data + context_offset); |
| 101 DCHECK_LT(context_offset, data->raw_size); | 128 DCHECK_LT(context_offset, data->raw_size); |
| 102 int context_length = data->raw_size - context_offset; | 129 int context_length = data->raw_size - context_offset; |
| 103 return Vector<const byte>(context_data, context_length); | 130 return Vector<const byte>(context_data, context_length); |
| 104 } | 131 } |
| 105 } } // namespace v8::internal | 132 } } // namespace v8::internal |
| OLD | NEW |