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