@@ -158,7 +158,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
158158
159159 platform : ? * const Platform ,
160160
161+ snapshot_creator : v8.SnapshotCreator ,
162+
161163 // the global isolate
164+ // owned by snapshot_creator.
162165 isolate : v8.Isolate ,
163166
164167 // 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 {
193196 params .array_buffer_allocator = v8 .createDefaultArrayBufferAllocator ();
194197 errdefer v8 .destroyArrayBufferAllocator (params .array_buffer_allocator .? );
195198
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 ();
198203
199- isolate .enter ();
200- errdefer isolate .exit ();
204+ // snapshot_creator enters the isolate for us.
201205
202206 isolate .setHostInitializeImportMetaObjectCallback (struct {
203207 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 {
218222
219223 env .* = .{
220224 .platform = platform ,
225+ .snapshot_creator = snapshot_creator ,
221226 .isolate = isolate ,
222227 .templates = undefined ,
223228 .allocator = allocator ,
@@ -258,13 +263,66 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
258263 }
259264
260265 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 ();
263269 v8 .destroyArrayBufferAllocator (self .isolate_params .array_buffer_allocator .? );
264270 self .allocator .destroy (self .isolate_params );
265271 self .allocator .destroy (self );
266272 }
267273
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+
268326 pub fn newInspector (self : * Self , arena : Allocator , ctx : anytype ) ! Inspector {
269327 return Inspector .init (arena , self .isolate , ctx );
270328 }
0 commit comments