V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
snapshot.h
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 #include "src/utils.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 // Forward declarations.
17 class Isolate;
18 class PartialSerializer;
19 class StartupSerializer;
20 
21 // Wrapper around reservation sizes and the serialization payload.
22 class SnapshotData : public SerializedData {
23  public:
24  // Used when producing.
25  explicit SnapshotData(const Serializer* serializer);
26 
27  // Used when consuming.
28  explicit SnapshotData(const Vector<const byte> snapshot)
29  : SerializedData(const_cast<byte*>(snapshot.begin()), snapshot.length()) {
30  }
31 
32  std::vector<Reservation> Reservations() const;
33  virtual Vector<const byte> Payload() const;
34 
35  Vector<const byte> RawData() const {
36  return Vector<const byte>(data_, size_);
37  }
38 
39  protected:
40  // The data header consists of uint32_t-sized entries:
41  // [0] magic number and (internal) external reference count
42  // [1] number of reservation size entries
43  // [2] payload length
44  // ... reservations
45  // ... serialized payload
46  static const uint32_t kNumReservationsOffset =
47  kMagicNumberOffset + kUInt32Size;
48  static const uint32_t kPayloadLengthOffset =
49  kNumReservationsOffset + kUInt32Size;
50  static const uint32_t kHeaderSize = kPayloadLengthOffset + kUInt32Size;
51 };
52 
53 class Snapshot : public AllStatic {
54  public:
55  // ---------------- Deserialization ----------------
56 
57  // Initialize the Isolate from the internal snapshot. Returns false if no
58  // snapshot could be found.
59  static bool Initialize(Isolate* isolate);
60 
61  // Create a new context using the internal partial snapshot.
62  static MaybeHandle<Context> NewContextFromSnapshot(
63  Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
64  size_t context_index,
65  v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer);
66 
67  // ---------------- Helper methods ----------------
68 
69  static bool HasContextSnapshot(Isolate* isolate, size_t index);
70  static bool EmbedsScript(Isolate* isolate);
71 
72  // To be implemented by the snapshot source.
73  static const v8::StartupData* DefaultSnapshotBlob();
74 
75  static bool VerifyChecksum(const v8::StartupData* data);
76 
77  // ---------------- Serialization ----------------
78 
79  static v8::StartupData CreateSnapshotBlob(
80  const SnapshotData* startup_snapshot,
81  const SnapshotData* read_only_snapshot,
82  const std::vector<SnapshotData*>& context_snapshots,
83  bool can_be_rehashed);
84 
85 #ifdef DEBUG
86  static bool SnapshotIsValid(const v8::StartupData* snapshot_blob);
87 #endif // DEBUG
88 
89  private:
90  static uint32_t ExtractNumContexts(const v8::StartupData* data);
91  static uint32_t ExtractContextOffset(const v8::StartupData* data,
92  uint32_t index);
93  static bool ExtractRehashability(const v8::StartupData* data);
94  static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
95  static Vector<const byte> ExtractReadOnlyData(const v8::StartupData* data);
96  static Vector<const byte> ExtractContextData(const v8::StartupData* data,
97  uint32_t index);
98 
99  static uint32_t GetHeaderValue(const v8::StartupData* data, uint32_t offset) {
100  return ReadLittleEndianValue<uint32_t>(
101  reinterpret_cast<Address>(data->data) + offset);
102  }
103  static void SetHeaderValue(char* data, uint32_t offset, uint32_t value) {
104  WriteLittleEndianValue(reinterpret_cast<Address>(data) + offset, value);
105  }
106 
107  static void CheckVersion(const v8::StartupData* data);
108 
109  // Snapshot blob layout:
110  // [0] number of contexts N
111  // [1] rehashability
112  // [2] checksum part A
113  // [3] checksum part B
114  // [4] (128 bytes) version string
115  // [5] offset to readonly
116  // [6] offset to context 0
117  // [7] offset to context 1
118  // ...
119  // ... offset to context N - 1
120  // ... startup snapshot data
121  // ... read-only snapshot data
122  // ... context 0 snapshot data
123  // ... context 1 snapshot data
124 
125  static const uint32_t kNumberOfContextsOffset = 0;
126  // TODO(yangguo): generalize rehashing, and remove this flag.
127  static const uint32_t kRehashabilityOffset =
128  kNumberOfContextsOffset + kUInt32Size;
129  static const uint32_t kChecksumPartAOffset =
130  kRehashabilityOffset + kUInt32Size;
131  static const uint32_t kChecksumPartBOffset =
132  kChecksumPartAOffset + kUInt32Size;
133  static const uint32_t kVersionStringOffset =
134  kChecksumPartBOffset + kUInt32Size;
135  static const uint32_t kVersionStringLength = 64;
136  static const uint32_t kReadOnlyOffsetOffset =
137  kVersionStringOffset + kVersionStringLength;
138  static const uint32_t kFirstContextOffsetOffset =
139  kReadOnlyOffsetOffset + kUInt32Size;
140 
141  static Vector<const byte> ChecksummedContent(const v8::StartupData* data) {
142  const uint32_t kChecksumStart = kVersionStringOffset;
143  return Vector<const byte>(
144  reinterpret_cast<const byte*>(data->data + kChecksumStart),
145  data->raw_size - kChecksumStart);
146  }
147 
148  static uint32_t StartupSnapshotOffset(int num_contexts) {
149  return POINTER_SIZE_ALIGN(kFirstContextOffsetOffset +
150  num_contexts * kInt32Size);
151  }
152 
153  static uint32_t ContextSnapshotOffsetOffset(int index) {
154  return kFirstContextOffsetOffset + index * kInt32Size;
155  }
156 
157  DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
158 };
159 
160 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
161 void SetSnapshotFromFile(StartupData* snapshot_blob);
162 #endif
163 
164 } // namespace internal
165 } // namespace v8
166 
167 #endif // V8_SNAPSHOT_SNAPSHOT_H_
Definition: libplatform.h:13