GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_snapshotable.h Lines: 13 14 92.9 %
Date: 2022-04-01 04:15:00 Branches: 0 0 - %

Line Branch Exec Source
1
2
#ifndef SRC_NODE_SNAPSHOTABLE_H_
3
#define SRC_NODE_SNAPSHOTABLE_H_
4
5
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
6
7
#include "base_object.h"
8
#include "util.h"
9
10
namespace node {
11
12
class Environment;
13
struct EnvSerializeInfo;
14
struct SnapshotData;
15
16
#define SERIALIZABLE_OBJECT_TYPES(V)                                           \
17
  V(fs_binding_data, fs::BindingData)                                          \
18
  V(v8_binding_data, v8_utils::BindingData)                                    \
19
  V(blob_binding_data, BlobBindingData)                                        \
20
  V(process_binding_data, process::BindingData)
21
22
enum class EmbedderObjectType : uint8_t {
23
  k_default = 0,
24
#define V(PropertyName, NativeType) k_##PropertyName,
25
  SERIALIZABLE_OBJECT_TYPES(V)
26
#undef V
27
};
28
29
// When serializing an embedder object, we'll serialize the native states
30
// into a chunk that can be mapped into a subclass of InternalFieldInfo,
31
// and pass it into the V8 callback as the payload of StartupData.
32
// TODO(joyeecheung): the classification of types seem to be wrong.
33
// We'd need a type for each field of each class of native object.
34
// Maybe it's fine - we'll just use the type to invoke BaseObject constructors
35
// and specify that the BaseObject has only one field for us to serialize.
36
// And for non-BaseObject embedder objects, we'll use field-wise types.
37
// The memory chunk looks like this:
38
//
39
// [   type   ] - EmbedderObjectType (a uint8_t)
40
// [  length  ] - a size_t
41
// [    ...   ] - custom bytes of size |length - header size|
42
struct InternalFieldInfo {
43
  EmbedderObjectType type;
44
  size_t length;
45
46
  InternalFieldInfo() = delete;
47
48
24
  static InternalFieldInfo* New(EmbedderObjectType type) {
49
24
    return New(type, sizeof(InternalFieldInfo));
50
  }
51
52
24
  static InternalFieldInfo* New(EmbedderObjectType type, size_t length) {
53
    InternalFieldInfo* result =
54
24
        reinterpret_cast<InternalFieldInfo*>(::operator new[](length));
55
24
    result->type = type;
56
24
    result->length = length;
57
24
    return result;
58
  }
59
60
19624
  InternalFieldInfo* Copy() const {
61
    InternalFieldInfo* result =
62
19624
        reinterpret_cast<InternalFieldInfo*>(::operator new[](length));
63
19624
    memcpy(result, this, length);
64
19624
    return result;
65
  }
66
67
19624
  void Delete() { ::operator delete[](this); }
68
};
69
70
// An interface for snapshotable native objects to inherit from.
71
// Use the SERIALIZABLE_OBJECT_METHODS() macro in the class to define
72
// the following methods to implement:
73
//
74
// - PrepareForSerialization(): This would be run prior to context
75
//   serialization. Use this method to e.g. release references that
76
//   can be re-initialized, or perform property store operations
77
//   that needs a V8 context.
78
// - Serialize(): This would be called during context serialization,
79
//   once for each embedder field of the object.
80
//   Allocate and construct an InternalFieldInfo object that contains
81
//   data that can be used to deserialize native states.
82
// - Deserialize(): This would be called after the context is
83
//   deserialized and the object graph is complete, once for each
84
//   embedder field of the object. Use this to restore native states
85
//   in the object.
86
class SnapshotableObject : public BaseObject {
87
 public:
88
  SnapshotableObject(Environment* env,
89
                     v8::Local<v8::Object> wrap,
90
                     EmbedderObjectType type = EmbedderObjectType::k_default);
91
  const char* GetTypeNameChars() const;
92
93
  virtual void PrepareForSerialization(v8::Local<v8::Context> context,
94
                                       v8::SnapshotCreator* creator) = 0;
95
  virtual InternalFieldInfo* Serialize(int index) = 0;
96
  bool is_snapshotable() const override { return true; }
97
  // We'll make sure that the type is set in the constructor
98
24
  EmbedderObjectType type() { return type_; }
99
100
 private:
101
  EmbedderObjectType type_;
102
};
103
104
#define SERIALIZABLE_OBJECT_METHODS()                                          \
105
  void PrepareForSerialization(v8::Local<v8::Context> context,                 \
106
                               v8::SnapshotCreator* creator) override;         \
107
  InternalFieldInfo* Serialize(int index) override;                            \
108
  static void Deserialize(v8::Local<v8::Context> context,                      \
109
                          v8::Local<v8::Object> holder,                        \
110
                          int index,                                           \
111
                          InternalFieldInfo* info);
112
113
v8::StartupData SerializeNodeContextInternalFields(v8::Local<v8::Object> holder,
114
                                                   int index,
115
                                                   void* env);
116
void DeserializeNodeInternalFields(v8::Local<v8::Object> holder,
117
                                   int index,
118
                                   v8::StartupData payload,
119
                                   void* env);
120
void SerializeBindingData(Environment* env,
121
                          v8::SnapshotCreator* creator,
122
                          EnvSerializeInfo* info);
123
124
bool IsSnapshotableType(FastStringKey key);
125
126
class SnapshotBuilder {
127
 public:
128
  static std::string Generate(const std::vector<std::string> args,
129
                              const std::vector<std::string> exec_args);
130
131
  // Generate the snapshot into out.
132
  static void Generate(SnapshotData* out,
133
                       const std::vector<std::string> args,
134
                       const std::vector<std::string> exec_args);
135
};
136
}  // namespace node
137
138
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
139
140
#endif  // SRC_NODE_SNAPSHOTABLE_H_