Make undefined->null safer, and apply the same trick to BrowserContext

This commit is contained in:
Karl Seguin
2025-04-29 11:28:43 +08:00
parent 9044925f32
commit 2e01fa738a
3 changed files with 14 additions and 20 deletions

View File

@@ -85,7 +85,7 @@ pub const Browser = struct {
pub fn newSession(self: *Browser, ctx: anytype) !*Session { pub fn newSession(self: *Browser, ctx: anytype) !*Session {
self.closeSession(); self.closeSession();
self.session = undefined; self.session = @as(Session, undefined);
const session = &self.session.?; const session = &self.session.?;
try Session.init(session, self, ctx); try Session.init(session, self, ctx);
return session; return session;
@@ -169,7 +169,7 @@ pub const Session = struct {
const page_arena = &self.browser.page_arena; const page_arena = &self.browser.page_arena;
_ = page_arena.reset(.{ .retain_with_limit = 1 * 1024 * 1024 }); _ = page_arena.reset(.{ .retain_with_limit = 1 * 1024 * 1024 });
self.page = undefined; self.page = @as(Page, undefined);
const page = &self.page.?; const page = &self.page.?;
try Page.init(page, page_arena.allocator(), self); try Page.init(page, page_arena.allocator(), self);

View File

@@ -62,9 +62,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
session_id_gen: SessionIdGen = .{}, session_id_gen: SessionIdGen = .{},
browser_context_id_gen: BrowserContextIdGen = .{}, browser_context_id_gen: BrowserContextIdGen = .{},
browser_context: ?*BrowserContext(Self), browser_context: ?BrowserContext(Self),
browser_context_pool: std.heap.MemoryPool(BrowserContext(Self)),
// Re-used arena for processing a message. We're assuming that we're getting // Re-used arena for processing a message. We're assuming that we're getting
// 1 message at a time. // 1 message at a time.
@@ -83,17 +81,15 @@ pub fn CDPT(comptime TypeProvider: type) type {
.allocator = allocator, .allocator = allocator,
.browser_context = null, .browser_context = null,
.message_arena = std.heap.ArenaAllocator.init(allocator), .message_arena = std.heap.ArenaAllocator.init(allocator),
.browser_context_pool = std.heap.MemoryPool(BrowserContext(Self)).init(allocator),
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
if (self.browser_context) |bc| { if (self.browser_context) |*bc| {
bc.deinit(); bc.deinit();
} }
self.browser.deinit(); self.browser.deinit();
self.message_arena.deinit(); self.message_arena.deinit();
self.browser_context_pool.deinit();
} }
pub fn handleMessage(self: *Self, msg: []const u8) bool { pub fn handleMessage(self: *Self, msg: []const u8) bool {
@@ -127,7 +123,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
.cdp = self, .cdp = self,
.arena = arena, .arena = arena,
.sender = sender, .sender = sender,
.browser_context = if (self.browser_context) |bc| bc else null, .browser_context = if (self.browser_context) |*bc| bc else null,
}; };
// See dispatchStartupCommand for more info on this. // See dispatchStartupCommand for more info on this.
@@ -222,7 +218,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
} }
fn isValidSessionId(self: *const Self, input_session_id: []const u8) bool { fn isValidSessionId(self: *const Self, input_session_id: []const u8) bool {
const browser_context = self.browser_context orelse return false; const browser_context = &(self.browser_context orelse return false);
const session_id = browser_context.session_id orelse return false; const session_id = browser_context.session_id orelse return false;
return std.mem.eql(u8, session_id, input_session_id); return std.mem.eql(u8, session_id, input_session_id);
} }
@@ -231,24 +227,22 @@ pub fn CDPT(comptime TypeProvider: type) type {
if (self.browser_context != null) { if (self.browser_context != null) {
return error.AlreadyExists; return error.AlreadyExists;
} }
const browser_context_id = self.browser_context_id_gen.next(); const id = self.browser_context_id_gen.next();
const browser_context = try self.browser_context_pool.create(); self.browser_context = @as(BrowserContext(Self), undefined);
errdefer self.browser_context_pool.destroy(browser_context); const browser_context = &self.browser_context.?;
try BrowserContext(Self).init(browser_context, browser_context_id, self); try BrowserContext(Self).init(browser_context, id, self);
self.browser_context = browser_context; return id;
return browser_context_id;
} }
pub fn disposeBrowserContext(self: *Self, browser_context_id: []const u8) bool { pub fn disposeBrowserContext(self: *Self, browser_context_id: []const u8) bool {
const bc = self.browser_context orelse return false; const bc = &(self.browser_context orelse return false);
if (std.mem.eql(u8, bc.id, browser_context_id) == false) { if (std.mem.eql(u8, bc.id, browser_context_id) == false) {
return false; return false;
} }
bc.deinit(); bc.deinit();
self.browser.closeSession(); self.browser.closeSession();
self.browser_context_pool.destroy(bc);
self.browser_context = null; self.browser_context = null;
return true; return true;
} }
@@ -563,7 +557,7 @@ pub fn Command(comptime CDP_T: type, comptime Sender: type) type {
pub fn createBrowserContext(self: *Self) !*BrowserContext(CDP_T) { pub fn createBrowserContext(self: *Self) !*BrowserContext(CDP_T) {
_ = try self.cdp.createBrowserContext(); _ = try self.cdp.createBrowserContext();
self.browser_context = self.cdp.browser_context.?; self.browser_context = &(self.cdp.browser_context.?);
return self.browser_context.?; return self.browser_context.?;
} }

View File

@@ -105,7 +105,7 @@ const TestContext = struct {
} }
_ = try c.createBrowserContext(); _ = try c.createBrowserContext();
var bc = c.browser_context.?; var bc = &c.browser_context.?;
if (opts.id) |id| { if (opts.id) |id| {
bc.id = id; bc.id = id;