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 3 // found in the LICENSE file. 4 5 #ifndef V8_SNAPSHOT_SNAPSHOT_H_ 6 #define V8_SNAPSHOT_SNAPSHOT_H_ 7 8 #include "src/snapshot/partial-serializer.h" 9 #include "src/snapshot/startup-serializer.h" 10 11 namespace v8 { 12 namespace internal { 13 14 // Forward declarations. 15 class Isolate; 16 class PartialSerializer; 17 class StartupSerializer; 18 19 // Wrapper around reservation sizes and the serialization payload. 20 class SnapshotData : public SerializedData { 21 public: 22 // Used when producing. 23 explicit SnapshotData(const Serializer* serializer); 24 25 // Used when consuming. SnapshotData(const Vector<const byte> snapshot)26 explicit SnapshotData(const Vector<const byte> snapshot) 27 : SerializedData(const_cast<byte*>(snapshot.begin()), snapshot.length()) { 28 CHECK(IsSane()); 29 } 30 31 Vector<const Reservation> Reservations() const; 32 Vector<const byte> Payload() const; 33 RawData()34 Vector<const byte> RawData() const { 35 return Vector<const byte>(data_, size_); 36 } 37 38 private: 39 bool IsSane(); 40 41 // The data header consists of uint32_t-sized entries: 42 // [0] magic number and external reference count 43 // [1] version hash 44 // [2] number of reservation size entries 45 // [3] payload length 46 // ... reservations 47 // ... serialized payload 48 static const int kCheckSumOffset = kMagicNumberOffset + kInt32Size; 49 static const int kNumReservationsOffset = kCheckSumOffset + kInt32Size; 50 static const int kPayloadLengthOffset = kNumReservationsOffset + kInt32Size; 51 static const int kHeaderSize = kPayloadLengthOffset + kInt32Size; 52 }; 53 54 class Snapshot : public AllStatic { 55 public: 56 // Initialize the Isolate from the internal snapshot. Returns false if no 57 // snapshot could be found. 58 static bool Initialize(Isolate* isolate); 59 // Create a new context using the internal partial snapshot. 60 static MaybeHandle<Context> NewContextFromSnapshot( 61 Isolate* isolate, Handle<JSGlobalProxy> global_proxy, 62 size_t context_index); 63 64 static bool HaveASnapshotToStartFrom(Isolate* isolate); 65 66 static bool HasContextSnapshot(Isolate* isolate, size_t index); 67 68 static bool EmbedsScript(Isolate* isolate); 69 70 // To be implemented by the snapshot source. 71 static const v8::StartupData* DefaultSnapshotBlob(); 72 73 static v8::StartupData CreateSnapshotBlob( 74 const SnapshotData* startup_snapshot, 75 const List<SnapshotData*>* context_snapshots); 76 77 #ifdef DEBUG 78 static bool SnapshotIsValid(v8::StartupData* snapshot_blob); 79 #endif // DEBUG 80 81 private: 82 static int ExtractNumContexts(const v8::StartupData* data); 83 static Vector<const byte> ExtractStartupData(const v8::StartupData* data); 84 static Vector<const byte> ExtractContextData(const v8::StartupData* data, 85 int index); 86 87 // Snapshot blob layout: 88 // [0] number of contexts N 89 // [1] offset to context 0 90 // [2] offset to context 1 91 // ... 92 // ... offset to context N - 1 93 // ... startup snapshot data 94 // ... context 0 snapshot data 95 // ... context 1 snapshot data 96 97 static const int kNumberOfContextsOffset = 0; 98 static const int kFirstContextOffsetOffset = 99 kNumberOfContextsOffset + kInt32Size; 100 StartupSnapshotOffset(int num_contexts)101 static int StartupSnapshotOffset(int num_contexts) { 102 return kFirstContextOffsetOffset + num_contexts * kInt32Size; 103 } 104 ContextSnapshotOffsetOffset(int index)105 static int ContextSnapshotOffsetOffset(int index) { 106 return kFirstContextOffsetOffset + index * kInt32Size; 107 } 108 109 DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot); 110 }; 111 112 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 113 void SetSnapshotFromFile(StartupData* snapshot_blob); 114 #endif 115 116 } // namespace internal 117 } // namespace v8 118 119 #endif // V8_SNAPSHOT_SNAPSHOT_H_ 120