@@ -158,7 +158,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
158
158
159
159
platform : ? * const Platform ,
160
160
161
+ snapshot_creator : v8.SnapshotCreator ,
162
+
161
163
// the global isolate
164
+ // owned by snapshot_creator.
162
165
isolate : v8.Isolate ,
163
166
164
167
// just kept around because we need to free it on deinit
@@ -193,11 +196,12 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
193
196
params .array_buffer_allocator = v8 .createDefaultArrayBufferAllocator ();
194
197
errdefer v8 .destroyArrayBufferAllocator (params .array_buffer_allocator .? );
195
198
196
- var isolate = v8 .Isolate .init (params );
197
- errdefer isolate .deinit ();
199
+ var snapshot_creator = v8 .SnapshotCreator .init (params );
200
+ errdefer snapshot_creator .deinit ();
201
+
202
+ var isolate = snapshot_creator .getIsolate ();
198
203
199
- isolate .enter ();
200
- errdefer isolate .exit ();
204
+ // snapshot_creator enters the isolate for us.
201
205
202
206
isolate .setHostInitializeImportMetaObjectCallback (struct {
203
207
fn callback (c_context : ? * v8.C_Context , c_module : ? * v8.C_Module , c_meta : ? * v8.C_Value ) callconv (.C ) void {
@@ -218,6 +222,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
218
222
219
223
env .* = .{
220
224
.platform = platform ,
225
+ .snapshot_creator = snapshot_creator ,
221
226
.isolate = isolate ,
222
227
.templates = undefined ,
223
228
.allocator = allocator ,
@@ -258,13 +263,66 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
258
263
}
259
264
260
265
pub fn deinit (self : * Self ) void {
261
- self .isolate .exit ();
262
- self .isolate .deinit ();
266
+ // The snapshot_creator owns the isolate. So it exit and deinit it
267
+ // for us.
268
+ self .snapshot_creator .deinit ();
263
269
v8 .destroyArrayBufferAllocator (self .isolate_params .array_buffer_allocator .? );
264
270
self .allocator .destroy (self .isolate_params );
265
271
self .allocator .destroy (self );
266
272
}
267
273
274
+ pub fn snapshot (self : * Self , default_ctx : * const JsContext ) v8.StartupData {
275
+ self .snapshot_creator .setDefaultContextWithCallbacks (
276
+ default_ctx .v8_context ,
277
+ // SerializeInternalFieldsCallback serializes internal fields
278
+ // of V8 objects that contain embedder data
279
+ .{
280
+ .callback = struct {
281
+ fn callback (holder : ? * v8.C_Object , index : c_int , data : ? * anyopaque ) callconv (.C ) v8.StartupData {
282
+ _ = holder ;
283
+ _ = index ;
284
+ _ = data ;
285
+ // TODO
286
+ std .debug .print ("SerializeInternalFieldsCallback\n " , .{});
287
+ return .{};
288
+ }
289
+ }.callback ,
290
+ .data = null ,
291
+ },
292
+ // SerializeContextDataCallback serializes context-specific
293
+ // state (globals, modules, etc.)
294
+ .{
295
+ .callback = struct {
296
+ fn callback (context : ? * v8.C_Context , index : c_int , data : ? * anyopaque ) callconv (.C ) v8.StartupData {
297
+ _ = context ;
298
+ _ = index ;
299
+ _ = data ;
300
+ // TODO
301
+ std .debug .print ("SerializeContextDataCallback\n " , .{});
302
+ return .{};
303
+ }
304
+ }.callback ,
305
+ .data = null ,
306
+ },
307
+ // SerializeAPIWrapperCallback serializes API wrappers that
308
+ // bridge V8 and Native objects
309
+ .{
310
+ .callback = struct {
311
+ fn callback (holder : ? * v8.C_Object , ptr : ? * anyopaque , data : ? * anyopaque ) callconv (.C ) v8.StartupData {
312
+ _ = holder ;
313
+ _ = ptr ;
314
+ _ = data ;
315
+ // TODO
316
+ std .debug .print ("SerializeAPIWrapperCallback\n " , .{});
317
+ return .{};
318
+ }
319
+ }.callback ,
320
+ .data = null ,
321
+ },
322
+ );
323
+ return self .snapshot_creator .createBlob ();
324
+ }
325
+
268
326
pub fn newInspector (self : * Self , arena : Allocator , ctx : anytype ) ! Inspector {
269
327
return Inspector .init (arena , self .isolate , ctx );
270
328
}
0 commit comments