Don't dupe StartupData, use what v8 gives us directly.

This commit is contained in:
Karl Seguin
2026-01-05 16:05:39 +08:00
parent b4f134bff6
commit 4720268426
4 changed files with 13 additions and 19 deletions

View File

@@ -86,8 +86,8 @@ pub fn init(allocator: Allocator, config: Config) !*App {
app.platform = try Platform.init(); app.platform = try Platform.init();
errdefer app.platform.deinit(); errdefer app.platform.deinit();
app.snapshot = try Snapshot.load(allocator); app.snapshot = try Snapshot.load();
errdefer app.snapshot.deinit(allocator); errdefer app.snapshot.deinit();
app.app_dir_path = getAndMakeAppDir(allocator); app.app_dir_path = getAndMakeAppDir(allocator);
@@ -112,7 +112,7 @@ pub fn deinit(self: *App) void {
self.telemetry.deinit(); self.telemetry.deinit();
self.notification.deinit(); self.notification.deinit();
self.http.deinit(); self.http.deinit();
self.snapshot.deinit(allocator); self.snapshot.deinit();
self.platform.deinit(); self.platform.deinit();
allocator.destroy(self); allocator.destroy(self);

View File

@@ -56,11 +56,11 @@ external_references: [countExternalReferences()]isize,
// If false, the data points into embedded_snapshot_blob and should not be freed // If false, the data points into embedded_snapshot_blob and should not be freed
owns_data: bool = false, owns_data: bool = false,
pub fn load(allocator: Allocator) !Snapshot { pub fn load() !Snapshot {
if (loadEmbedded()) |snapshot| { if (loadEmbedded()) |snapshot| {
return snapshot; return snapshot;
} }
return create(allocator); return create();
} }
fn loadEmbedded() ?Snapshot { fn loadEmbedded() ?Snapshot {
@@ -87,10 +87,11 @@ fn loadEmbedded() ?Snapshot {
}; };
} }
pub fn deinit(self: Snapshot, allocator: Allocator) void { pub fn deinit(self: Snapshot) void {
// Only free if we own the data (was created in-process) // Only free if we own the data (was created in-process)
if (self.owns_data) { if (self.owns_data) {
allocator.free(self.startup_data.data[0..@intCast(self.startup_data.raw_size)]); // V8 allocated this with `new char[]`, so we need to use the C++ delete[] operator
v8.v8__StartupData__DELETE(self.startup_data.data);
} }
} }
@@ -113,6 +114,7 @@ fn isValid(self: Snapshot) bool {
return v8.v8__StartupData__IsValid(self.startup_data); return v8.v8__StartupData__IsValid(self.startup_data);
} }
<<<<<<< HEAD
pub fn createGlobalTemplate(isolate: v8.Isolate, templates: []const v8.FunctionTemplate) v8.ObjectTemplate { pub fn createGlobalTemplate(isolate: v8.Isolate, templates: []const v8.FunctionTemplate) v8.ObjectTemplate {
// Set up the global template to inherit from Window's template // Set up the global template to inherit from Window's template
// This way the global object gets all Window properties through inheritance // This way the global object gets all Window properties through inheritance
@@ -127,7 +129,7 @@ pub fn createGlobalTemplate(isolate: v8.Isolate, templates: []const v8.FunctionT
return v8.c.v8__FunctionTemplate__InstanceTemplate(js_global); return v8.c.v8__FunctionTemplate__InstanceTemplate(js_global);
} }
pub fn create(allocator: Allocator) !Snapshot { pub fn create() !Snapshot {
var external_references = collectExternalReferences(); var external_references = collectExternalReferences();
var params: v8.CreateParams = undefined; var params: v8.CreateParams = undefined;
@@ -267,13 +269,12 @@ pub fn create(allocator: Allocator) !Snapshot {
} }
const blob = v8.v8__SnapshotCreator__createBlob(snapshot_creator, v8.kKeep); const blob = v8.v8__SnapshotCreator__createBlob(snapshot_creator, v8.kKeep);
const owned = try allocator.dupe(u8, blob.data[0..@intCast(blob.raw_size)]);
return .{ return .{
.owns_data = true, .owns_data = true,
.data_start = data_start, .data_start = data_start,
.external_references = external_references, .external_references = external_references,
.startup_data = .{ .data = owned.ptr, .raw_size = @intCast(owned.len) }, .startup_data = blob,
}; };
} }

View File

@@ -391,13 +391,6 @@ pub export fn v8_inspector__Client__IMPL__descriptionForValueSubtype(
return if (external_entry.subtype == null) null else ""; return if (external_entry.subtype == null) null else "";
} }
/// Enables C to allocate using the given Zig allocator
pub export fn zigAlloc(self: *anyopaque, bytes: usize) callconv(.c) ?[*]u8 {
const allocator: *Allocator = @ptrCast(@alignCast(self));
const allocated_bytes = allocator.alloc(u8, bytes) catch return null;
return allocated_bytes.ptr;
}
test "TaggedAnyOpaque" { test "TaggedAnyOpaque" {
// If we grow this, fine, but it should be a conscious decision // If we grow this, fine, but it should be a conscious decision
try std.testing.expectEqual(24, @sizeOf(TaggedAnyOpaque)); try std.testing.expectEqual(24, @sizeOf(TaggedAnyOpaque));

View File

@@ -25,8 +25,8 @@ pub fn main() !void {
var platform = try lp.js.Platform.init(); var platform = try lp.js.Platform.init();
defer platform.deinit(); defer platform.deinit();
const snapshot = try lp.js.Snapshot.create(allocator); const snapshot = try lp.js.Snapshot.create();
defer snapshot.deinit(allocator); defer snapshot.deinit();
var is_stdout = true; var is_stdout = true;
var file = std.fs.File.stdout(); var file = std.fs.File.stdout();