browser: window must survive between 2 pages

So window is moved to session
This commit is contained in:
Pierre Tachoire
2023-12-21 11:52:00 +01:00
parent cc5280460e
commit 7d226a43b6
2 changed files with 28 additions and 17 deletions

View File

@@ -63,11 +63,14 @@ pub const Session = struct {
env: Env = undefined, env: Env = undefined,
loop: Loop = undefined, loop: Loop = undefined,
window: Window,
fn init(allocator: std.mem.Allocator, uri: []const u8) !*Session { fn init(allocator: std.mem.Allocator, uri: []const u8) !*Session {
var self = try allocator.create(Session); var self = try allocator.create(Session);
self.* = Session{ self.* = Session{
.uri = uri, .uri = uri,
.arena = std.heap.ArenaAllocator.init(allocator), .arena = std.heap.ArenaAllocator.init(allocator),
.window = Window.create(null),
}; };
const aallocator = self.arena.allocator(); const aallocator = self.arena.allocator();
@@ -89,7 +92,12 @@ pub const Session = struct {
} }
pub fn createPage(self: *Session) !Page { pub fn createPage(self: *Session) !Page {
return Page.init(self.arena.allocator(), &self.loader, &self.env); return Page.init(
self.arena.allocator(),
&self.loader,
&self.env,
&self.window,
);
} }
}; };
@@ -100,12 +108,19 @@ pub const Page = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
loader: *Loader, loader: *Loader,
env: *Env, env: *Env,
window: *Window,
fn init(allocator: std.mem.Allocator, loader: *Loader, env: *Env) Page { fn init(
allocator: std.mem.Allocator,
loader: *Loader,
env: *Env,
window: *Window,
) Page {
return Page{ return Page{
.allocator = allocator, .allocator = allocator,
.loader = loader, .loader = loader,
.env = env, .env = env,
.window = window,
}; };
} }
@@ -135,27 +150,20 @@ pub const Page = struct {
// document // document
log.debug("parse html", .{}); log.debug("parse html", .{});
// TODO inject the URL to the document.
const html_doc = try parser.documentHTMLParseFromStrAlloc(self.allocator, result.body.?); const html_doc = try parser.documentHTMLParseFromStrAlloc(self.allocator, result.body.?);
const doc = parser.documentHTMLToDocument(html_doc); const doc = parser.documentHTMLToDocument(html_doc);
self.window.replaceDocument(doc);
// start JS env // start JS env
log.debug("start js env", .{}); log.debug("start js env", .{});
try self.env.start(self.allocator, apis); try self.env.start(self.allocator, apis);
// add global objects // add global objects
log.debug("setup global env", .{}); log.debug("setup global env", .{});
const window = Window.create(doc, null); try self.env.addObject(apis, self.window, "window");
try self.env.addObject(apis, self.window, "self");
// TODO we must share the same pointer between window and self.
// once https://github.com/lightpanda-io/jsruntime-lib/issues/171 is
// done, replace the 2 lines with:
//
// const obj = try js_env.addObject(apis, window, "window");
// try js_env.attachObject(try js_env.getGlobal(), "self", obj);
//
try self.env.addObject(apis, window, "window");
try self.env.addObject(apis, window, "self");
try self.env.addObject(apis, doc, "document"); try self.env.addObject(apis, doc, "document");
} }
}; };

View File

@@ -7,16 +7,19 @@ const parser = @import("../netsurf.zig");
pub const Window = struct { pub const Window = struct {
pub const mem_guarantied = true; pub const mem_guarantied = true;
document: *parser.Document, document: *parser.Document = undefined,
target: []const u8, target: []const u8,
pub fn create(doc: *parser.Document, target: ?[]const u8) Window { pub fn create(target: ?[]const u8) Window {
return Window{ return Window{
.document = doc,
.target = target orelse "", .target = target orelse "",
}; };
} }
pub fn replaceDocument(self: *Window, doc: *parser.Document) void {
self.document = doc;
}
pub fn get_window(self: *Window) *Window { pub fn get_window(self: *Window) *Window {
return self; return self;
} }