1379644fd85861e01ca12cd357799ec3675b8751
[platform/upstream/v8.git] / src / snapshot / 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/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:
22     explicit Metadata(uint32_t data = 0) : data_(data) {}
23     bool embeds_script() { return EmbedsScriptBits::decode(data_); }
24     void set_embeds_script(bool v) {
25       data_ = EmbedsScriptBits::update(data_, v);
26     }
27
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       Handle<FixedArray>* outdated_contexts_out);
42
43   static bool HaveASnapshotToStartFrom(Isolate* isolate);
44
45   static bool EmbedsScript(Isolate* isolate);
46
47   static uint32_t SizeOfFirstPage(Isolate* isolate, AllocationSpace space);
48
49
50   // To be implemented by the snapshot source.
51   static const v8::StartupData* DefaultSnapshotBlob();
52
53   static v8::StartupData CreateSnapshotBlob(
54       const StartupSerializer& startup_ser,
55       const PartialSerializer& context_ser, Snapshot::Metadata metadata);
56
57 #ifdef DEBUG
58   static bool SnapshotIsValid(v8::StartupData* snapshot_blob);
59 #endif  // DEBUG
60
61  private:
62   static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
63   static Vector<const byte> ExtractContextData(const v8::StartupData* data);
64   static Metadata ExtractMetadata(const v8::StartupData* data);
65
66   // Snapshot blob layout:
67   // [0] metadata
68   // [1 - 6] pre-calculated first page sizes for paged spaces
69   // [7] serialized start up data length
70   // ... serialized start up data
71   // ... serialized context data
72
73   static const int kNumPagedSpaces = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1;
74
75   static const int kMetadataOffset = 0;
76   static const int kFirstPageSizesOffset = kMetadataOffset + kInt32Size;
77   static const int kStartupLengthOffset =
78       kFirstPageSizesOffset + kNumPagedSpaces * kInt32Size;
79   static const int kStartupDataOffset = kStartupLengthOffset + kInt32Size;
80
81   static int ContextOffset(int startup_length) {
82     return kStartupDataOffset + startup_length;
83   }
84
85   DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
86 };
87
88 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
89 void SetSnapshotFromFile(StartupData* snapshot_blob);
90 #endif
91
92 } }  // namespace v8::internal
93
94 #endif  // V8_SNAPSHOT_SNAPSHOT_H_