Skip to content

Commit 2e01fa7

Browse files
committed
Make undefined->null safer, and apply the same trick to BrowserContext
1 parent 9044925 commit 2e01fa7

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

src/browser/browser.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub const Browser = struct {
8585

8686
pub fn newSession(self: *Browser, ctx: anytype) !*Session {
8787
self.closeSession();
88-
self.session = undefined;
88+
self.session = @as(Session, undefined);
8989
const session = &self.session.?;
9090
try Session.init(session, self, ctx);
9191
return session;
@@ -169,7 +169,7 @@ pub const Session = struct {
169169
const page_arena = &self.browser.page_arena;
170170
_ = page_arena.reset(.{ .retain_with_limit = 1 * 1024 * 1024 });
171171

172-
self.page = undefined;
172+
self.page = @as(Page, undefined);
173173
const page = &self.page.?;
174174
try Page.init(page, page_arena.allocator(), self);
175175

src/cdp/cdp.zig

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
6262
session_id_gen: SessionIdGen = .{},
6363
browser_context_id_gen: BrowserContextIdGen = .{},
6464

65-
browser_context: ?*BrowserContext(Self),
66-
67-
browser_context_pool: std.heap.MemoryPool(BrowserContext(Self)),
65+
browser_context: ?BrowserContext(Self),
6866

6967
// Re-used arena for processing a message. We're assuming that we're getting
7068
// 1 message at a time.
@@ -83,17 +81,15 @@ pub fn CDPT(comptime TypeProvider: type) type {
8381
.allocator = allocator,
8482
.browser_context = null,
8583
.message_arena = std.heap.ArenaAllocator.init(allocator),
86-
.browser_context_pool = std.heap.MemoryPool(BrowserContext(Self)).init(allocator),
8784
};
8885
}
8986

9087
pub fn deinit(self: *Self) void {
91-
if (self.browser_context) |bc| {
88+
if (self.browser_context) |*bc| {
9289
bc.deinit();
9390
}
9491
self.browser.deinit();
9592
self.message_arena.deinit();
96-
self.browser_context_pool.deinit();
9793
}
9894

9995
pub fn handleMessage(self: *Self, msg: []const u8) bool {
@@ -127,7 +123,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
127123
.cdp = self,
128124
.arena = arena,
129125
.sender = sender,
130-
.browser_context = if (self.browser_context) |bc| bc else null,
126+
.browser_context = if (self.browser_context) |*bc| bc else null,
131127
};
132128

133129
// See dispatchStartupCommand for more info on this.
@@ -222,7 +218,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
222218
}
223219

224220
fn isValidSessionId(self: *const Self, input_session_id: []const u8) bool {
225-
const browser_context = self.browser_context orelse return false;
221+
const browser_context = &(self.browser_context orelse return false);
226222
const session_id = browser_context.session_id orelse return false;
227223
return std.mem.eql(u8, session_id, input_session_id);
228224
}
@@ -231,24 +227,22 @@ pub fn CDPT(comptime TypeProvider: type) type {
231227
if (self.browser_context != null) {
232228
return error.AlreadyExists;
233229
}
234-
const browser_context_id = self.browser_context_id_gen.next();
230+
const id = self.browser_context_id_gen.next();
235231

236-
const browser_context = try self.browser_context_pool.create();
237-
errdefer self.browser_context_pool.destroy(browser_context);
232+
self.browser_context = @as(BrowserContext(Self), undefined);
233+
const browser_context = &self.browser_context.?;
238234

239-
try BrowserContext(Self).init(browser_context, browser_context_id, self);
240-
self.browser_context = browser_context;
241-
return browser_context_id;
235+
try BrowserContext(Self).init(browser_context, id, self);
236+
return id;
242237
}
243238

244239
pub fn disposeBrowserContext(self: *Self, browser_context_id: []const u8) bool {
245-
const bc = self.browser_context orelse return false;
240+
const bc = &(self.browser_context orelse return false);
246241
if (std.mem.eql(u8, bc.id, browser_context_id) == false) {
247242
return false;
248243
}
249244
bc.deinit();
250245
self.browser.closeSession();
251-
self.browser_context_pool.destroy(bc);
252246
self.browser_context = null;
253247
return true;
254248
}
@@ -563,7 +557,7 @@ pub fn Command(comptime CDP_T: type, comptime Sender: type) type {
563557

564558
pub fn createBrowserContext(self: *Self) !*BrowserContext(CDP_T) {
565559
_ = try self.cdp.createBrowserContext();
566-
self.browser_context = self.cdp.browser_context.?;
560+
self.browser_context = &(self.cdp.browser_context.?);
567561
return self.browser_context.?;
568562
}
569563

src/cdp/testing.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const TestContext = struct {
105105
}
106106

107107
_ = try c.createBrowserContext();
108-
var bc = c.browser_context.?;
108+
var bc = &c.browser_context.?;
109109

110110
if (opts.id) |id| {
111111
bc.id = id;

0 commit comments

Comments
 (0)