2 Commits

Author SHA1 Message Date
Pierre Tachoire
a2f20d572d wip zig-v8 2025-07-05 14:19:55 -07:00
Pierre Tachoire
72499390d8 Use isolate form snapshot_creator 2025-07-05 14:19:55 -07:00
3 changed files with 71 additions and 11 deletions

View File

@@ -12,11 +12,11 @@
.url = "https://github.com/lightpanda-io/tigerbeetle-io/archive/61d9652f1a957b7f4db723ea6aa0ce9635e840ce.tar.gz", .url = "https://github.com/lightpanda-io/tigerbeetle-io/archive/61d9652f1a957b7f4db723ea6aa0ce9635e840ce.tar.gz",
.hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd", .hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd",
}, },
.v8 = .{ //.v8 = .{
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/dd087771378ea854452bcb010309fa9ffe5a9cac.tar.gz", // .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/dd087771378ea854452bcb010309fa9ffe5a9cac.tar.gz",
.hash = "v8-0.0.0-xddH66e8AwBL3O_A8yWQYQIyfMbKHFNVQr_NqM6YjU11", // .hash = "v8-0.0.0-xddH66e8AwBL3O_A8yWQYQIyfMbKHFNVQr_NqM6YjU11",
}, //},
//.v8 = .{ .path = "../zig-v8-fork" }, .v8 = .{ .path = "../zig-v8-fork" },
//.tigerbeetle_io = .{ .path = "../tigerbeetle-io" }, //.tigerbeetle_io = .{ .path = "../tigerbeetle-io" },
}, },
} }

View File

@@ -122,6 +122,8 @@ pub const Page = struct {
// load polyfills // load polyfills
try polyfill.load(self.arena, self.main_context); try polyfill.load(self.arena, self.main_context);
_ = session.executor.env.snapshot(self.main_context);
_ = try session.browser.app.loop.timeout(1 * std.time.ns_per_ms, &self.microtask_node); _ = try session.browser.app.loop.timeout(1 * std.time.ns_per_ms, &self.microtask_node);
// message loop must run only non-test env // message loop must run only non-test env
if (comptime !builtin.is_test) { if (comptime !builtin.is_test) {

View File

@@ -158,7 +158,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
platform: ?*const Platform, platform: ?*const Platform,
snapshot_creator: v8.SnapshotCreator,
// the global isolate // the global isolate
// owned by snapshot_creator.
isolate: v8.Isolate, isolate: v8.Isolate,
// just kept around because we need to free it on deinit // 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 {
params.array_buffer_allocator = v8.createDefaultArrayBufferAllocator(); params.array_buffer_allocator = v8.createDefaultArrayBufferAllocator();
errdefer v8.destroyArrayBufferAllocator(params.array_buffer_allocator.?); errdefer v8.destroyArrayBufferAllocator(params.array_buffer_allocator.?);
var isolate = v8.Isolate.init(params); var snapshot_creator = v8.SnapshotCreator.init(params);
errdefer isolate.deinit(); errdefer snapshot_creator.deinit();
isolate.enter(); var isolate = snapshot_creator.getIsolate();
errdefer isolate.exit();
// snapshot_creator enters the isolate for us.
isolate.setHostInitializeImportMetaObjectCallback(struct { isolate.setHostInitializeImportMetaObjectCallback(struct {
fn callback(c_context: ?*v8.C_Context, c_module: ?*v8.C_Module, c_meta: ?*v8.C_Value) callconv(.C) void { 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 {
env.* = .{ env.* = .{
.platform = platform, .platform = platform,
.snapshot_creator = snapshot_creator,
.isolate = isolate, .isolate = isolate,
.templates = undefined, .templates = undefined,
.allocator = allocator, .allocator = allocator,
@@ -258,13 +263,66 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.isolate.exit(); // The snapshot_creator owns the isolate. So it exit and deinit it
self.isolate.deinit(); // for us.
self.snapshot_creator.deinit();
v8.destroyArrayBufferAllocator(self.isolate_params.array_buffer_allocator.?); v8.destroyArrayBufferAllocator(self.isolate_params.array_buffer_allocator.?);
self.allocator.destroy(self.isolate_params); self.allocator.destroy(self.isolate_params);
self.allocator.destroy(self); self.allocator.destroy(self);
} }
pub fn snapshot(self: *Self, default_ctx: *const JsContext) v8.StartupData {
self.snapshot_creator.setDefaultContextWithCallbacks(
default_ctx.v8_context,
// SerializeInternalFieldsCallback serializes internal fields
// of V8 objects that contain embedder data
.{
.callback = struct {
fn callback(holder: ?*v8.C_Object, index: c_int, data: ?*anyopaque) callconv(.C) v8.StartupData {
_ = holder;
_ = index;
_ = data;
// TODO
std.debug.print("SerializeInternalFieldsCallback\n", .{});
return .{};
}
}.callback,
.data = null,
},
// SerializeContextDataCallback serializes context-specific
// state (globals, modules, etc.)
.{
.callback = struct {
fn callback(context: ?*v8.C_Context, index: c_int, data: ?*anyopaque) callconv(.C) v8.StartupData {
_ = context;
_ = index;
_ = data;
// TODO
std.debug.print("SerializeContextDataCallback\n", .{});
return .{};
}
}.callback,
.data = null,
},
// SerializeAPIWrapperCallback serializes API wrappers that
// bridge V8 and Native objects
.{
.callback = struct {
fn callback(holder: ?*v8.C_Object, ptr: ?*anyopaque, data: ?*anyopaque) callconv(.C) v8.StartupData {
_ = holder;
_ = ptr;
_ = data;
// TODO
std.debug.print("SerializeAPIWrapperCallback\n", .{});
return .{};
}
}.callback,
.data = null,
},
);
return self.snapshot_creator.createBlob();
}
pub fn newInspector(self: *Self, arena: Allocator, ctx: anytype) !Inspector { pub fn newInspector(self: *Self, arena: Allocator, ctx: anytype) !Inspector {
return Inspector.init(arena, self.isolate, ctx); return Inspector.init(arena, self.isolate, ctx);
} }