GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_snapshotable.h Lines: 14 14 100.0 %
Date: 2022-08-31 04:20:31 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
  V(util_weak_reference, util::WeakReference)
23
24
enum class EmbedderObjectType : uint8_t {
25
#define V(PropertyName, NativeType) k_##PropertyName,
26
  SERIALIZABLE_OBJECT_TYPES(V)
27
#undef V
28
};
29
30
typedef size_t SnapshotIndex;
31
32
// When serializing an embedder object, we'll serialize the native states
33
// into a chunk that can be mapped into a subclass of InternalFieldInfoBase,
34
// and pass it into the V8 callback as the payload of StartupData.
35
// The memory chunk looks like this:
36
//
37
// [   type   ] - EmbedderObjectType (a uint8_t)
38
// [  length  ] - a size_t
39
// [    ...   ] - custom bytes of size |length - header size|
40
struct InternalFieldInfoBase {
41
 public:
42
  EmbedderObjectType type;
43
  size_t length;
44
45
  template <typename T>
46
24
  static T* New(EmbedderObjectType type) {
47
    static_assert(std::is_base_of_v<InternalFieldInfoBase, T> ||
48
                      std::is_same_v<InternalFieldInfoBase, T>,
49
                  "Can only accept InternalFieldInfoBase subclasses");
50
24
    void* buf = ::operator new[](sizeof(T));
51
24
    T* result = new (buf) T;
52
24
    result->type = type;
53
24
    result->length = sizeof(T);
54
24
    return result;
55
  }
56
57
  template <typename T>
58
43320
  T* Copy() const {
59
    static_assert(std::is_base_of_v<InternalFieldInfoBase, T> ||
60
                      std::is_same_v<InternalFieldInfoBase, T>,
61
                  "Can only accept InternalFieldInfoBase subclasses");
62
    static_assert(std::is_trivially_copyable_v<T>,
63
                  "Can only memcpy trivially copyable class");
64
43320
    void* buf = ::operator new[](sizeof(T));
65
43320
    T* result = new (buf) T;
66
43320
    memcpy(result, this, sizeof(T));
67
43320
    return result;
68
  }
69
70
21660
  void Delete() { ::operator delete[](this); }
71
72
  InternalFieldInfoBase() = default;
73
};
74
75
// An interface for snapshotable native objects to inherit from.
76
// Use the SERIALIZABLE_OBJECT_METHODS() macro in the class to define
77
// the following methods to implement:
78
//
79
// - PrepareForSerialization(): This would be run prior to context
80
//   serialization. Use this method to e.g. release references that
81
//   can be re-initialized, or perform property store operations
82
//   that needs a V8 context.
83
// - Serialize(): This would be called during context serialization,
84
//   once for each embedder field of the object.
85
//   Allocate and construct an InternalFieldInfoBase object that contains
86
//   data that can be used to deserialize native states.
87
// - Deserialize(): This would be called after the context is
88
//   deserialized and the object graph is complete, once for each
89
//   embedder field of the object. Use this to restore native states
90
//   in the object.
91
class SnapshotableObject : public BaseObject {
92
 public:
93
  SnapshotableObject(Environment* env,
94
                     v8::Local<v8::Object> wrap,
95
                     EmbedderObjectType type);
96
  const char* GetTypeNameChars() const;
97
98
  // If returns false, the object will not be serialized.
99
  virtual bool PrepareForSerialization(v8::Local<v8::Context> context,
100
                                       v8::SnapshotCreator* creator) = 0;
101
  virtual InternalFieldInfoBase* Serialize(int index) = 0;
102
24
  bool is_snapshotable() const override { return true; }
103
  // We'll make sure that the type is set in the constructor
104
24
  EmbedderObjectType type() { return type_; }
105
106
 private:
107
  EmbedderObjectType type_;
108
};
109
110
#define SERIALIZABLE_OBJECT_METHODS()                                          \
111
  bool PrepareForSerialization(v8::Local<v8::Context> context,                 \
112
                               v8::SnapshotCreator* creator) override;         \
113
  InternalFieldInfoBase* Serialize(int index) override;                        \
114
  static void Deserialize(v8::Local<v8::Context> context,                      \
115
                          v8::Local<v8::Object> holder,                        \
116
                          int index,                                           \
117
                          InternalFieldInfoBase* info);
118
119
v8::StartupData SerializeNodeContextInternalFields(v8::Local<v8::Object> holder,
120
                                                   int index,
121
                                                   void* env);
122
void DeserializeNodeInternalFields(v8::Local<v8::Object> holder,
123
                                   int index,
124
                                   v8::StartupData payload,
125
                                   void* env);
126
void SerializeSnapshotableObjects(Environment* env,
127
                                  v8::SnapshotCreator* creator,
128
                                  EnvSerializeInfo* info);
129
}  // namespace node
130
131
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
132
133
#endif  // SRC_NODE_SNAPSHOTABLE_H_