Skip to content

Commit 7bc7da5

Browse files
browser: back on createPage returning a Page (pointer)
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
1 parent 8e05f09 commit 7bc7da5

3 files changed

Lines changed: 15 additions & 19 deletions

File tree

src/browser/browser.zig

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub const Session = struct {
103103
window: Window,
104104
// TODO move the shed to the browser?
105105
storageShed: storage.Shed,
106-
_page: ?Page = null,
106+
page: ?Page = null,
107107
httpClient: HttpClient,
108108

109109
jstypes: [Types.len]usize = undefined,
@@ -125,7 +125,7 @@ pub const Session = struct {
125125
}
126126

127127
fn deinit(self: *Session) void {
128-
if (self._page) |*p| p.end();
128+
if (self.page) |*p| p.end();
129129

130130
if (self.inspector) |inspector| {
131131
inspector.deinit(self.alloc);
@@ -158,17 +158,14 @@ pub const Session = struct {
158158
}
159159
}
160160

161-
pub fn createPage(self: *Session) !void {
162-
if (self._page != null) return error.SessionPageExists;
161+
// NOTE: the caller is not the owner of the returned value,
162+
// the pointer on Page is just returned as a convenience
163+
pub fn createPage(self: *Session) !*Page {
164+
if (self.page != null) return error.SessionPageExists;
163165
const p: Page = undefined;
164-
self._page = p;
165-
Page.init(&self._page.?, self.alloc, self);
166-
}
167-
168-
// shortcut
169-
pub fn page(self: *Session) *Page {
170-
if (self._page) |*p| return p;
171-
@panic("No Page on this session");
166+
self.page = p;
167+
Page.init(&self.page.?, self.alloc, self);
168+
return &self.page.?;
172169
}
173170
};
174171

src/cdp/page.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ fn navigate(
341341
try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, msg.sessionID);
342342

343343
// Launch navigate
344-
try ctx.browser.session.createPage();
344+
const p = try ctx.browser.session.createPage();
345345
ctx.state.executionContextId += 1;
346346
const auxData = try std.fmt.allocPrint(
347347
alloc,
@@ -350,7 +350,7 @@ fn navigate(
350350
.{ctx.state.frameID},
351351
);
352352
defer alloc.free(auxData);
353-
try ctx.browser.session.page().navigate(params.url, auxData);
353+
try p.navigate(params.url, auxData);
354354

355355
// Events
356356

src/main_get.zig

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ pub fn main() !void {
8787
try Browser.init(&browser, allocator, &loop, vm);
8888
defer browser.deinit();
8989

90-
try browser.session.createPage();
90+
const page = try browser.session.createPage();
9191

92-
try browser.session.page().navigate(url, null);
93-
defer browser.session.page().end();
92+
try page.navigate(url, null);
9493

95-
try browser.session.page().wait();
94+
try page.wait();
9695

9796
if (dump) {
98-
try browser.session.page().dump(std.io.getStdOut());
97+
try page.dump(std.io.getStdOut());
9998
}
10099
}

0 commit comments

Comments
 (0)