browser: use *const Page with fetchModule

This commit is contained in:
Pierre Tachoire
2025-03-17 09:58:31 +01:00
parent 229844d399
commit 087a7b5f3c

View File

@@ -184,20 +184,17 @@ pub const Session = struct {
_ = referrer; _ = referrer;
const self: *Session = @ptrCast(@alignCast(ctx)); const self: *Session = @ptrCast(@alignCast(ctx));
const page = &(self.page orelse return error.NoPage);
if (self.page == null) {
return error.NoPage;
}
log.debug("fetch module: specifier: {s}", .{specifier}); log.debug("fetch module: specifier: {s}", .{specifier});
// fetchModule is called within the context of processing a page. // fetchModule is called within the context of processing a page.
// Use the page_arena for this, which has a more appropriate lifetime // Use the page_arena for this, which has a more appropriate lifetime
// and which has more retained memory between sessions and pages. // and which has more retained memory between sessions and pages.
const arena = self.browser.page_arena.allocator(); const arena = self.browser.page_arena.allocator();
const body = try self.page.?.fetchData( const body = try page.fetchData(
arena, arena,
specifier, specifier,
if (self.page.?.current_script) |s| s.src else null, if (page.current_script) |s| s.src else null,
); );
return self.env.compileModule(body, specifier); return self.env.compileModule(body, specifier);
} }
@@ -605,7 +602,7 @@ pub const Page = struct {
// It resolves src using the page's uri. // It resolves src using the page's uri.
// If a base path is given, src is resolved according to the base first. // If a base path is given, src is resolved according to the base first.
// the caller owns the returned string // the caller owns the returned string
fn fetchData(self: *Page, arena: Allocator, src: []const u8, base: ?[]const u8) ![]const u8 { fn fetchData(self: *const Page, arena: Allocator, src: []const u8, base: ?[]const u8) ![]const u8 {
log.debug("starting fetch {s}", .{src}); log.debug("starting fetch {s}", .{src});
var buffer: [1024]u8 = undefined; var buffer: [1024]u8 = undefined;
@@ -647,7 +644,7 @@ pub const Page = struct {
// fetchScript senf a GET request to the src and execute the script // fetchScript senf a GET request to the src and execute the script
// received. // received.
fn fetchScript(self: *Page, s: *const Script) !void { fn fetchScript(self: *const Page, s: *const Script) !void {
const arena = self.arena; const arena = self.arena;
const body = try self.fetchData(arena, s.src, null); const body = try self.fetchData(arena, s.src, null);