/
node_snapshotable.h
141 lines (119 loc) Β· 5.4 KB
/
node_snapshotable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef SRC_NODE_SNAPSHOTABLE_H_
#define SRC_NODE_SNAPSHOTABLE_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "base_object.h"
#include "util.h"
namespace node {
class Environment;
struct RealmSerializeInfo;
struct SnapshotData;
class ExternalReferenceRegistry;
using SnapshotIndex = size_t;
struct PropInfo {
std::string name; // name for debugging
uint32_t id; // In the list - in case there are any empty entries
SnapshotIndex index; // In the snapshot
};
#define SERIALIZABLE_OBJECT_TYPES(V) \
V(fs_binding_data, fs::BindingData) \
V(v8_binding_data, v8_utils::BindingData) \
V(blob_binding_data, BlobBindingData) \
V(process_binding_data, process::BindingData) \
V(util_weak_reference, util::WeakReference)
enum class EmbedderObjectType : uint8_t {
#define V(PropertyName, NativeType) k_##PropertyName,
SERIALIZABLE_OBJECT_TYPES(V)
#undef V
};
typedef size_t SnapshotIndex;
// When serializing an embedder object, we'll serialize the native states
// into a chunk that can be mapped into a subclass of InternalFieldInfoBase,
// and pass it into the V8 callback as the payload of StartupData.
// The memory chunk looks like this:
//
// [ type ] - EmbedderObjectType (a uint8_t)
// [ length ] - a size_t
// [ ... ] - custom bytes of size |length - header size|
struct InternalFieldInfoBase {
public:
EmbedderObjectType type;
size_t length;
template <typename T>
static T* New(EmbedderObjectType type) {
static_assert(std::is_base_of_v<InternalFieldInfoBase, T> ||
std::is_same_v<InternalFieldInfoBase, T>,
"Can only accept InternalFieldInfoBase subclasses");
void* buf = ::operator new[](sizeof(T));
T* result = new (buf) T;
result->type = type;
result->length = sizeof(T);
return result;
}
template <typename T>
T* Copy() const {
static_assert(std::is_base_of_v<InternalFieldInfoBase, T> ||
std::is_same_v<InternalFieldInfoBase, T>,
"Can only accept InternalFieldInfoBase subclasses");
static_assert(std::is_trivially_copyable_v<T>,
"Can only memcpy trivially copyable class");
void* buf = ::operator new[](sizeof(T));
T* result = new (buf) T;
memcpy(result, this, sizeof(T));
return result;
}
void Delete() { ::operator delete[](this); }
InternalFieldInfoBase() = default;
};
// An interface for snapshotable native objects to inherit from.
// Use the SERIALIZABLE_OBJECT_METHODS() macro in the class to define
// the following methods to implement:
//
// - PrepareForSerialization(): This would be run prior to context
// serialization. Use this method to e.g. release references that
// can be re-initialized, or perform property store operations
// that needs a V8 context.
// - Serialize(): This would be called during context serialization,
// once for each embedder field of the object.
// Allocate and construct an InternalFieldInfoBase object that contains
// data that can be used to deserialize native states.
// - Deserialize(): This would be called after the context is
// deserialized and the object graph is complete, once for each
// embedder field of the object. Use this to restore native states
// in the object.
class SnapshotableObject : public BaseObject {
public:
SnapshotableObject(Environment* env,
v8::Local<v8::Object> wrap,
EmbedderObjectType type);
std::string_view GetTypeName() const;
// If returns false, the object will not be serialized.
virtual bool PrepareForSerialization(v8::Local<v8::Context> context,
v8::SnapshotCreator* creator) = 0;
virtual InternalFieldInfoBase* Serialize(int index) = 0;
bool is_snapshotable() const override { return true; }
// We'll make sure that the type is set in the constructor
EmbedderObjectType type() { return type_; }
private:
EmbedderObjectType type_;
};
#define SERIALIZABLE_OBJECT_METHODS() \
bool PrepareForSerialization(v8::Local<v8::Context> context, \
v8::SnapshotCreator* creator) override; \
InternalFieldInfoBase* Serialize(int index) override; \
static void Deserialize(v8::Local<v8::Context> context, \
v8::Local<v8::Object> holder, \
int index, \
InternalFieldInfoBase* info);
v8::StartupData SerializeNodeContextInternalFields(v8::Local<v8::Object> holder,
int index,
void* env);
void DeserializeNodeInternalFields(v8::Local<v8::Object> holder,
int index,
v8::StartupData payload,
void* env);
void SerializeSnapshotableObjects(Realm* realm,
v8::SnapshotCreator* creator,
RealmSerializeInfo* info);
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_SNAPSHOTABLE_H_