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/serialize.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 // Forward declarations.
14 class Isolate;
15 class PartialSerializer;
16 class StartupSerializer;
17 
18 class Snapshot : public AllStatic {
19  public:
20   class Metadata {
21    public:
data_(data)22     explicit Metadata(uint32_t data = 0) : data_(data) {}
embeds_script()23     bool embeds_script() { return EmbedsScriptBits::decode(data_); }
set_embeds_script(bool v)24     void set_embeds_script(bool v) {
25       data_ = EmbedsScriptBits::update(data_, v);
26     }
27 
RawValue()28     uint32_t& RawValue() { return data_; }
29 
30    private:
31     class EmbedsScriptBits : public BitField<bool, 0, 1> {};
32     uint32_t data_;
33   };
34 
35   // Initialize the Isolate from the internal snapshot. Returns false if no
36   // snapshot could be found.
37   static bool Initialize(Isolate* isolate);
38   // Create a new context using the internal partial snapshot.
39   static MaybeHandle<Context> NewContextFromSnapshot(
40       Isolate* isolate, Handle<JSGlobalProxy> global_proxy);
41 
42   static bool HaveASnapshotToStartFrom(Isolate* isolate);
43 
44   static bool EmbedsScript(Isolate* isolate);
45 
46   static uint32_t SizeOfFirstPage(Isolate* isolate, AllocationSpace space);
47 
48 
49   // To be implemented by the snapshot source.
50   static const v8::StartupData* DefaultSnapshotBlob();
51 
52   static v8::StartupData CreateSnapshotBlob(
53       const StartupSerializer& startup_ser,
54       const PartialSerializer& context_ser, Snapshot::Metadata metadata);
55 
56 #ifdef DEBUG
57   static bool SnapshotIsValid(v8::StartupData* snapshot_blob);
58 #endif  // DEBUG
59 
60  private:
61   static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
62   static Vector<const byte> ExtractContextData(const v8::StartupData* data);
63   static Metadata ExtractMetadata(const v8::StartupData* data);
64 
65   // Snapshot blob layout:
66   // [0] metadata
67   // [1 - 6] pre-calculated first page sizes for paged spaces
68   // [7] serialized start up data length
69   // ... serialized start up data
70   // ... serialized context data
71 
72   static const int kNumPagedSpaces = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1;
73 
74   static const int kMetadataOffset = 0;
75   static const int kFirstPageSizesOffset = kMetadataOffset + kInt32Size;
76   static const int kStartupLengthOffset =
77       kFirstPageSizesOffset + kNumPagedSpaces * kInt32Size;
78   static const int kStartupDataOffset = kStartupLengthOffset + kInt32Size;
79 
ContextOffset(int startup_length)80   static int ContextOffset(int startup_length) {
81     return kStartupDataOffset + startup_length;
82   }
83 
84   DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
85 };
86 
87 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
88 void SetSnapshotFromFile(StartupData* snapshot_blob);
89 #endif
90 
91 }  // namespace internal
92 }  // namespace v8
93 
94 #endif  // V8_SNAPSHOT_SNAPSHOT_H_
95