/src/node/deps/v8/include/v8-snapshot.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2021 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 | | #ifndef INCLUDE_V8_SNAPSHOT_H_ |
6 | | #define INCLUDE_V8_SNAPSHOT_H_ |
7 | | |
8 | | #include "v8-internal.h" // NOLINT(build/include_directory) |
9 | | #include "v8-isolate.h" // NOLINT(build/include_directory) |
10 | | #include "v8-local-handle.h" // NOLINT(build/include_directory) |
11 | | #include "v8config.h" // NOLINT(build/include_directory) |
12 | | |
13 | | namespace v8 { |
14 | | |
15 | | class Object; |
16 | | |
17 | | class V8_EXPORT StartupData { |
18 | | public: |
19 | | /** |
20 | | * Whether the data created can be rehashed and and the hash seed can be |
21 | | * recomputed when deserialized. |
22 | | * Only valid for StartupData returned by SnapshotCreator::CreateBlob(). |
23 | | */ |
24 | | bool CanBeRehashed() const; |
25 | | /** |
26 | | * Allows embedders to verify whether the data is valid for the current |
27 | | * V8 instance. |
28 | | */ |
29 | | bool IsValid() const; |
30 | | |
31 | | const char* data; |
32 | | int raw_size; |
33 | | }; |
34 | | |
35 | | /** |
36 | | * Callback and supporting data used in SnapshotCreator to implement embedder |
37 | | * logic to serialize internal fields. |
38 | | * Internal fields that directly reference V8 objects are serialized without |
39 | | * calling this callback. Internal fields that contain aligned pointers are |
40 | | * serialized by this callback if it returns non-zero result. Otherwise it is |
41 | | * serialized verbatim. |
42 | | */ |
43 | | struct SerializeInternalFieldsCallback { |
44 | | using CallbackFunction = StartupData (*)(Local<Object> holder, int index, |
45 | | void* data); |
46 | | SerializeInternalFieldsCallback(CallbackFunction function = nullptr, |
47 | | void* data_arg = nullptr) |
48 | 0 | : callback(function), data(data_arg) {} |
49 | | CallbackFunction callback; |
50 | | void* data; |
51 | | }; |
52 | | // Note that these fields are called "internal fields" in the API and called |
53 | | // "embedder fields" within V8. |
54 | | using SerializeEmbedderFieldsCallback = SerializeInternalFieldsCallback; |
55 | | |
56 | | /** |
57 | | * Callback and supporting data used to implement embedder logic to deserialize |
58 | | * internal fields. |
59 | | */ |
60 | | struct DeserializeInternalFieldsCallback { |
61 | | using CallbackFunction = void (*)(Local<Object> holder, int index, |
62 | | StartupData payload, void* data); |
63 | | DeserializeInternalFieldsCallback(CallbackFunction function = nullptr, |
64 | | void* data_arg = nullptr) |
65 | 135k | : callback(function), data(data_arg) {} |
66 | | void (*callback)(Local<Object> holder, int index, StartupData payload, |
67 | | void* data); |
68 | | void* data; |
69 | | }; |
70 | | |
71 | | using DeserializeEmbedderFieldsCallback = DeserializeInternalFieldsCallback; |
72 | | |
73 | | /** |
74 | | * Helper class to create a snapshot data blob. |
75 | | * |
76 | | * The Isolate used by a SnapshotCreator is owned by it, and will be entered |
77 | | * and exited by the constructor and destructor, respectively; The destructor |
78 | | * will also destroy the Isolate. Experimental language features, including |
79 | | * those available by default, are not available while creating a snapshot. |
80 | | */ |
81 | | class V8_EXPORT SnapshotCreator { |
82 | | public: |
83 | | enum class FunctionCodeHandling { kClear, kKeep }; |
84 | | |
85 | | /** |
86 | | * Initialize and enter an isolate, and set it up for serialization. |
87 | | * The isolate is either created from scratch or from an existing snapshot. |
88 | | * The caller keeps ownership of the argument snapshot. |
89 | | * \param existing_blob existing snapshot from which to create this one. |
90 | | * \param external_references a null-terminated array of external references |
91 | | * that must be equivalent to CreateParams::external_references. |
92 | | * \param owns_isolate whether this SnapshotCreator should call |
93 | | * v8::Isolate::Dispose() during its destructor. |
94 | | */ |
95 | | V8_DEPRECATE_SOON("Use the version that passes CreateParams instead.") |
96 | | explicit SnapshotCreator(Isolate* isolate, |
97 | | const intptr_t* external_references = nullptr, |
98 | | const StartupData* existing_blob = nullptr, |
99 | | bool owns_isolate = true); |
100 | | |
101 | | /** |
102 | | * Create and enter an isolate, and set it up for serialization. |
103 | | * The isolate is either created from scratch or from an existing snapshot. |
104 | | * The caller keeps ownership of the argument snapshot. |
105 | | * \param existing_blob existing snapshot from which to create this one. |
106 | | * \param external_references a null-terminated array of external references |
107 | | * that must be equivalent to CreateParams::external_references. |
108 | | */ |
109 | | V8_DEPRECATE_SOON("Use the version that passes CreateParams instead.") |
110 | | explicit SnapshotCreator(const intptr_t* external_references = nullptr, |
111 | | const StartupData* existing_blob = nullptr); |
112 | | |
113 | | /** |
114 | | * Creates an Isolate for serialization and enters it. The creator fully owns |
115 | | * the Isolate and will invoke `v8::Isolate::Dispose()` during destruction. |
116 | | * |
117 | | * \param params The parameters to initialize the Isolate for. Details: |
118 | | * - `params.external_references` are expected to be a |
119 | | * null-terminated array of external references. |
120 | | * - `params.existing_blob` is an optional snapshot blob from |
121 | | * which can be used to initialize the new blob. |
122 | | */ |
123 | | explicit SnapshotCreator(const v8::Isolate::CreateParams& params); |
124 | | |
125 | | /** |
126 | | * Initializes an Isolate for serialization and enters it. The creator does |
127 | | * not own the Isolate but merely initialize it properly. |
128 | | * |
129 | | * \param isolate The isolate that was allocated by `Isolate::Allocate()~. |
130 | | * \param params The parameters to initialize the Isolate for. Details: |
131 | | * - `params.external_references` are expected to be a |
132 | | * null-terminated array of external references. |
133 | | * - `params.existing_blob` is an optional snapshot blob from |
134 | | * which can be used to initialize the new blob. |
135 | | */ |
136 | | SnapshotCreator(v8::Isolate* isolate, |
137 | | const v8::Isolate::CreateParams& params); |
138 | | |
139 | | /** |
140 | | * Destroy the snapshot creator, and exit and dispose of the Isolate |
141 | | * associated with it. |
142 | | */ |
143 | | ~SnapshotCreator(); |
144 | | |
145 | | /** |
146 | | * \returns the isolate prepared by the snapshot creator. |
147 | | */ |
148 | | Isolate* GetIsolate(); |
149 | | |
150 | | /** |
151 | | * Set the default context to be included in the snapshot blob. |
152 | | * The snapshot will not contain the global proxy, and we expect one or a |
153 | | * global object template to create one, to be provided upon deserialization. |
154 | | * |
155 | | * \param callback optional callback to serialize internal fields. |
156 | | */ |
157 | | void SetDefaultContext(Local<Context> context, |
158 | | SerializeInternalFieldsCallback callback = |
159 | | SerializeInternalFieldsCallback()); |
160 | | |
161 | | /** |
162 | | * Add additional context to be included in the snapshot blob. |
163 | | * The snapshot will include the global proxy. |
164 | | * |
165 | | * \param callback optional callback to serialize internal fields. |
166 | | * |
167 | | * \returns the index of the context in the snapshot blob. |
168 | | */ |
169 | | size_t AddContext(Local<Context> context, |
170 | | SerializeInternalFieldsCallback callback = |
171 | | SerializeInternalFieldsCallback()); |
172 | | |
173 | | /** |
174 | | * Attach arbitrary V8::Data to the context snapshot, which can be retrieved |
175 | | * via Context::GetDataFromSnapshotOnce after deserialization. This data does |
176 | | * not survive when a new snapshot is created from an existing snapshot. |
177 | | * \returns the index for retrieval. |
178 | | */ |
179 | | template <class T> |
180 | | V8_INLINE size_t AddData(Local<Context> context, Local<T> object); |
181 | | |
182 | | /** |
183 | | * Attach arbitrary V8::Data to the isolate snapshot, which can be retrieved |
184 | | * via Isolate::GetDataFromSnapshotOnce after deserialization. This data does |
185 | | * not survive when a new snapshot is created from an existing snapshot. |
186 | | * \returns the index for retrieval. |
187 | | */ |
188 | | template <class T> |
189 | | V8_INLINE size_t AddData(Local<T> object); |
190 | | |
191 | | /** |
192 | | * Created a snapshot data blob. |
193 | | * This must not be called from within a handle scope. |
194 | | * \param function_code_handling whether to include compiled function code |
195 | | * in the snapshot. |
196 | | * \returns { nullptr, 0 } on failure, and a startup snapshot on success. The |
197 | | * caller acquires ownership of the data array in the return value. |
198 | | */ |
199 | | StartupData CreateBlob(FunctionCodeHandling function_code_handling); |
200 | | |
201 | | // Disallow copying and assigning. |
202 | | SnapshotCreator(const SnapshotCreator&) = delete; |
203 | | void operator=(const SnapshotCreator&) = delete; |
204 | | |
205 | | private: |
206 | | size_t AddData(Local<Context> context, internal::Address object); |
207 | | size_t AddData(internal::Address object); |
208 | | |
209 | | void* data_; |
210 | | }; |
211 | | |
212 | | template <class T> |
213 | 0 | size_t SnapshotCreator::AddData(Local<Context> context, Local<T> object) { |
214 | 0 | return AddData(context, internal::ValueHelper::ValueAsAddress(*object)); |
215 | 0 | } Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Uint32Array>(v8::Local<v8::Context>, v8::Local<v8::Uint32Array>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Uint8Array>(v8::Local<v8::Context>, v8::Local<v8::Uint8Array>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Float64Array>(v8::Local<v8::Context>, v8::Local<v8::Float64Array>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Array>(v8::Local<v8::Context>, v8::Local<v8::Array>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Object>(v8::Local<v8::Context>, v8::Local<v8::Object>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Int32Array>(v8::Local<v8::Context>, v8::Local<v8::Int32Array>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::BigInt64Array>(v8::Local<v8::Context>, v8::Local<v8::BigInt64Array>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Function>(v8::Local<v8::Context>, v8::Local<v8::Function>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Context>(v8::Local<v8::Context>, v8::Local<v8::Context>) |
216 | | |
217 | | template <class T> |
218 | 0 | size_t SnapshotCreator::AddData(Local<T> object) { |
219 | 0 | return AddData(internal::ValueHelper::ValueAsAddress(*object)); |
220 | 0 | } Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Private>(v8::Local<v8::Private>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::Symbol>(v8::Local<v8::Symbol>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::String>(v8::Local<v8::String>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::FunctionTemplate>(v8::Local<v8::FunctionTemplate>) Unexecuted instantiation: unsigned long v8::SnapshotCreator::AddData<v8::ObjectTemplate>(v8::Local<v8::ObjectTemplate>) |
221 | | |
222 | | } // namespace v8 |
223 | | |
224 | | #endif // INCLUDE_V8_SNAPSHOT_H_ |