Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | // The common functionality when building with or without snapshots. |
| 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/api.h" |
| 10 | #include "src/base/platform/platform.h" |
| 11 | #include "src/serialize.h" |
| 12 | #include "src/snapshot.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17 | bool Snapshot::HaveASnapshotToStartFrom() { |
| 18 | return SnapshotBlob().data != NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | bool Snapshot::Initialize(Isolate* isolate) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 23 | if (!HaveASnapshotToStartFrom()) return false; |
| 24 | base::ElapsedTimer timer; |
| 25 | if (FLAG_profile_deserialization) timer.Start(); |
| 26 | |
| 27 | const v8::StartupData blob = SnapshotBlob(); |
| 28 | SnapshotData snapshot_data(ExtractStartupData(&blob)); |
| 29 | Deserializer deserializer(&snapshot_data); |
| 30 | bool success = isolate->Init(&deserializer); |
| 31 | if (FLAG_profile_deserialization) { |
| 32 | double ms = timer.Elapsed().InMillisecondsF(); |
| 33 | PrintF("[Snapshot loading and deserialization took %0.3f ms]\n", ms); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 35 | return success; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
| 39 | Handle<Context> Snapshot::NewContextFromSnapshot(Isolate* isolate) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 40 | if (!HaveASnapshotToStartFrom()) return Handle<Context>(); |
| 41 | |
| 42 | const v8::StartupData blob = SnapshotBlob(); |
| 43 | SnapshotData snapshot_data(ExtractContextData(&blob)); |
| 44 | Deserializer deserializer(&snapshot_data); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 45 | Object* root; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | deserializer.DeserializePartial(isolate, &root); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 47 | CHECK(root->IsContext()); |
| 48 | return Handle<Context>(Context::cast(root)); |
| 49 | } |
| 50 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 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]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 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 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | } } // namespace v8::internal |