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