v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-snapshot.h
Go to the documentation of this file.
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
13namespace v8 {
14
15class Object;
16
17namespace internal {
18class SnapshotCreatorImpl;
19} // namespace internal
20
22 public:
23 /**
24 * Whether the data created can be rehashed and and the hash seed can be
25 * recomputed when deserialized.
26 * Only valid for StartupData returned by SnapshotCreator::CreateBlob().
27 */
28 bool CanBeRehashed() const;
29 /**
30 * Allows embedders to verify whether the data is valid for the current
31 * V8 instance.
32 */
33 bool IsValid() const;
34
35 const char* data;
37};
38
39/**
40 * Callback and supporting data used in SnapshotCreator to implement embedder
41 * logic to serialize internal fields of v8::Objects.
42 * Internal fields that directly reference V8 objects are serialized without
43 * calling this callback. Internal fields that contain aligned pointers are
44 * serialized by this callback if it returns non-zero result. Otherwise it is
45 * serialized verbatim.
46 */
48 using CallbackFunction = StartupData (*)(Local<Object> holder, int index,
49 void* data);
50 SerializeInternalFieldsCallback(CallbackFunction function = nullptr,
51 void* data_arg = nullptr)
52 : callback(function), data(data_arg) {}
53 CallbackFunction callback;
54 void* data;
55};
56
57/**
58 * Similar to SerializeInternalFieldsCallback, but works with the embedder data
59 * in a v8::Context.
60 */
62 using CallbackFunction = StartupData (*)(Local<Context> holder, int index,
63 void* data);
64 SerializeContextDataCallback(CallbackFunction function = nullptr,
65 void* data_arg = nullptr)
66 : callback(function), data(data_arg) {}
67 CallbackFunction callback;
68 void* data;
69};
70
71/**
72 * Similar to `SerializeInternalFieldsCallback`, but is used exclusively to
73 * serialize API wrappers. The pointers for API wrappers always point into the
74 * CppHeap.
75 */
77 using CallbackFunction = StartupData (*)(Local<Object> holder,
78 void* cpp_heap_pointer, void* data);
79 explicit SerializeAPIWrapperCallback(CallbackFunction function = nullptr,
80 void* data = nullptr)
81 : callback(function), data(data) {}
82
83 CallbackFunction callback;
84 void* data;
85};
86
87/**
88 * Callback and supporting data used to implement embedder logic to deserialize
89 * internal fields of v8::Objects.
90 */
92 using CallbackFunction = void (*)(Local<Object> holder, int index,
93 StartupData payload, void* data);
94 DeserializeInternalFieldsCallback(CallbackFunction function = nullptr,
95 void* data_arg = nullptr)
96 : callback(function), data(data_arg) {}
97
98 CallbackFunction callback;
99 void* data;
100};
101
102/**
103 * Similar to DeserializeInternalFieldsCallback, but works with the embedder
104 * data in a v8::Context.
105 */
107 using CallbackFunction = void (*)(Local<Context> holder, int index,
108 StartupData payload, void* data);
109 DeserializeContextDataCallback(CallbackFunction function = nullptr,
110 void* data_arg = nullptr)
111 : callback(function), data(data_arg) {}
112 CallbackFunction callback;
113 void* data;
114};
115
117 using CallbackFunction = void (*)(Local<Object> holder, StartupData payload,
118 void* data);
119 explicit DeserializeAPIWrapperCallback(CallbackFunction function = nullptr,
120 void* data = nullptr)
121 : callback(function), data(data) {}
122
123 CallbackFunction callback;
124 void* data;
125};
126
127/**
128 * Helper class to create a snapshot data blob.
129 *
130 * The Isolate used by a SnapshotCreator is owned by it, and will be entered
131 * and exited by the constructor and destructor, respectively; The destructor
132 * will also destroy the Isolate. Experimental language features, including
133 * those available by default, are not available while creating a snapshot.
134 */
136 public:
138
139 /**
140 * Initialize and enter an isolate, and set it up for serialization.
141 * The isolate is either created from scratch or from an existing snapshot.
142 * The caller keeps ownership of the argument snapshot.
143 * \param existing_blob existing snapshot from which to create this one.
144 * \param external_references a null-terminated array of external references
145 * that must be equivalent to CreateParams::external_references.
146 * \param owns_isolate whether this SnapshotCreator should call
147 * v8::Isolate::Dispose() during its destructor.
148 */
149 V8_DEPRECATE_SOON("Use the version that passes CreateParams instead.")
150 explicit SnapshotCreator(Isolate* isolate,
151 const intptr_t* external_references = nullptr,
152 const StartupData* existing_blob = nullptr,
153 bool owns_isolate = true);
154
155 /**
156 * Create and enter an isolate, and set it up for serialization.
157 * The isolate is either created from scratch or from an existing snapshot.
158 * The caller keeps ownership of the argument snapshot.
159 * \param existing_blob existing snapshot from which to create this one.
160 * \param external_references a null-terminated array of external references
161 * that must be equivalent to CreateParams::external_references.
162 */
163 V8_DEPRECATE_SOON("Use the version that passes CreateParams instead.")
164 explicit SnapshotCreator(const intptr_t* external_references = nullptr,
165 const StartupData* existing_blob = nullptr);
166
167 /**
168 * Creates an Isolate for serialization and enters it. The creator fully owns
169 * the Isolate and will invoke `v8::Isolate::Dispose()` during destruction.
170 *
171 * \param params The parameters to initialize the Isolate for. Details:
172 * - `params.external_references` are expected to be a
173 * null-terminated array of external references.
174 * - `params.existing_blob` is an optional snapshot blob from
175 * which can be used to initialize the new blob.
176 */
177 explicit SnapshotCreator(const v8::Isolate::CreateParams& params);
178
179 /**
180 * Initializes an Isolate for serialization and enters it. The creator does
181 * not own the Isolate but merely initialize it properly.
182 *
183 * \param isolate The isolate that was allocated by `Isolate::Allocate()~.
184 * \param params The parameters to initialize the Isolate for. Details:
185 * - `params.external_references` are expected to be a
186 * null-terminated array of external references.
187 * - `params.existing_blob` is an optional snapshot blob from
188 * which can be used to initialize the new blob.
189 */
191 const v8::Isolate::CreateParams& params);
192
193 /**
194 * Destroy the snapshot creator, and exit and dispose of the Isolate
195 * associated with it.
196 */
198
199 /**
200 * \returns the isolate prepared by the snapshot creator.
201 */
203
204 /**
205 * Set the default context to be included in the snapshot blob.
206 * The snapshot will not contain the global proxy, and we expect one or a
207 * global object template to create one, to be provided upon deserialization.
208 *
209 * \param internal_fields_serializer An optional callback used to serialize
210 * internal pointer fields set by
211 * v8::Object::SetAlignedPointerInInternalField().
212 *
213 * \param context_data_serializer An optional callback used to serialize
214 * context embedder data set by
215 * v8::Context::SetAlignedPointerInEmbedderData().
216 *
217 * \param api_wrapper_serializer An optional callback used to serialize API
218 * wrapper references set via `v8::Object::Wrap()`.
219 */
221 Local<Context> context,
222 SerializeInternalFieldsCallback internal_fields_serializer =
224 SerializeContextDataCallback context_data_serializer =
226 SerializeAPIWrapperCallback api_wrapper_serializer =
228
229 /**
230 * Add additional context to be included in the snapshot blob.
231 * The snapshot will include the global proxy.
232 *
233 * \param internal_fields_serializer Similar to internal_fields_serializer
234 * in SetDefaultContext() but only applies to the context being added.
235 *
236 * \param context_data_serializer Similar to context_data_serializer
237 * in SetDefaultContext() but only applies to the context being added.
238 *
239 * \param api_wrapper_serializer Similar to api_wrapper_serializer
240 * in SetDefaultContext() but only applies to the context being added.
241 */
243 SerializeInternalFieldsCallback internal_fields_serializer =
245 SerializeContextDataCallback context_data_serializer =
247 SerializeAPIWrapperCallback api_wrapper_serializer =
249
250 /**
251 * Attach arbitrary V8::Data to the context snapshot, which can be retrieved
252 * via Context::GetDataFromSnapshotOnce after deserialization. This data does
253 * not survive when a new snapshot is created from an existing snapshot.
254 * \returns the index for retrieval.
255 */
256 template <class T>
258
259 /**
260 * Attach arbitrary V8::Data to the isolate snapshot, which can be retrieved
261 * via Isolate::GetDataFromSnapshotOnce after deserialization. This data does
262 * not survive when a new snapshot is created from an existing snapshot.
263 * \returns the index for retrieval.
264 */
265 template <class T>
267
268 /**
269 * Created a snapshot data blob.
270 * This must not be called from within a handle scope.
271 * \param function_code_handling whether to include compiled function code
272 * in the snapshot.
273 * \returns { nullptr, 0 } on failure, and a startup snapshot on success. The
274 * caller acquires ownership of the data array in the return value.
275 */
277
278 // Disallow copying and assigning.
280 void operator=(const SnapshotCreator&) = delete;
281
282 private:
283 size_t AddData(Local<Context> context, internal::Address object);
284 size_t AddData(internal::Address object);
285
286 internal::SnapshotCreatorImpl* impl_;
287 friend class internal::SnapshotCreatorImpl;
288};
289
290template <class T>
292 return AddData(context, internal::ValueHelper::ValueAsAddress(*object));
293}
294
295template <class T>
297 return AddData(internal::ValueHelper::ValueAsAddress(*object));
298}
299
300} // namespace v8
301
302#endif // INCLUDE_V8_SNAPSHOT_H_
friend class Local
void operator=(const SnapshotCreator &)=delete
SnapshotCreator(v8::Isolate *isolate, const v8::Isolate::CreateParams &params)
V8_INLINE size_t AddData(Local< Context > context, Local< T > object)
SnapshotCreator(Isolate *isolate, const intptr_t *external_references=nullptr, const StartupData *existing_blob=nullptr, bool owns_isolate=true)
Isolate * GetIsolate()
StartupData CreateBlob(FunctionCodeHandling function_code_handling)
V8_INLINE size_t AddData(Local< T > object)
void SetDefaultContext(Local< Context > context, SerializeInternalFieldsCallback internal_fields_serializer=SerializeInternalFieldsCallback(), SerializeContextDataCallback context_data_serializer=SerializeContextDataCallback(), SerializeAPIWrapperCallback api_wrapper_serializer=SerializeAPIWrapperCallback())
SnapshotCreator(const intptr_t *external_references=nullptr, const StartupData *existing_blob=nullptr)
SnapshotCreator(const SnapshotCreator &)=delete
size_t AddContext(Local< Context > context, SerializeInternalFieldsCallback internal_fields_serializer=SerializeInternalFieldsCallback(), SerializeContextDataCallback context_data_serializer=SerializeContextDataCallback(), SerializeAPIWrapperCallback api_wrapper_serializer=SerializeAPIWrapperCallback())
SnapshotCreator(const v8::Isolate::CreateParams &params)
bool CanBeRehashed() const
const char * data
Definition v8-snapshot.h:35
bool IsValid() const
uintptr_t Address
Definition v8-internal.h:52
DeserializeAPIWrapperCallback(CallbackFunction function=nullptr, void *data=nullptr)
DeserializeContextDataCallback(CallbackFunction function=nullptr, void *data_arg=nullptr)
DeserializeInternalFieldsCallback(CallbackFunction function=nullptr, void *data_arg=nullptr)
Definition v8-snapshot.h:94
SerializeAPIWrapperCallback(CallbackFunction function=nullptr, void *data=nullptr)
Definition v8-snapshot.h:79
SerializeContextDataCallback(CallbackFunction function=nullptr, void *data_arg=nullptr)
Definition v8-snapshot.h:64
SerializeInternalFieldsCallback(CallbackFunction function=nullptr, void *data_arg=nullptr)
Definition v8-snapshot.h:50
#define V8_EXPORT
Definition v8config.h:860
#define V8_INLINE
Definition v8config.h:513
#define V8_DEPRECATE_SOON(message)
Definition v8config.h:627