diff --git a/src/browser/browser.zig b/src/browser/browser.zig index 0f58333e..1117c3e2 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -63,11 +63,14 @@ pub const Session = struct { env: Env = undefined, loop: Loop = undefined, + window: Window, + fn init(allocator: std.mem.Allocator, uri: []const u8) !*Session { var self = try allocator.create(Session); self.* = Session{ .uri = uri, .arena = std.heap.ArenaAllocator.init(allocator), + .window = Window.create(null), }; const aallocator = self.arena.allocator(); @@ -89,7 +92,12 @@ pub const Session = struct { } 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, loader: *Loader, 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{ .allocator = allocator, .loader = loader, .env = env, + .window = window, }; } @@ -135,27 +150,20 @@ pub const Page = struct { // document log.debug("parse html", .{}); + // TODO inject the URL to the document. const html_doc = try parser.documentHTMLParseFromStrAlloc(self.allocator, result.body.?); const doc = parser.documentHTMLToDocument(html_doc); + self.window.replaceDocument(doc); + // start JS env log.debug("start js env", .{}); try self.env.start(self.allocator, apis); // add global objects log.debug("setup global env", .{}); - const window = Window.create(doc, null); - - // 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, self.window, "window"); + try self.env.addObject(apis, self.window, "self"); try self.env.addObject(apis, doc, "document"); } }; diff --git a/src/nav/window.zig b/src/nav/window.zig index 6424a7d2..346e8e06 100644 --- a/src/nav/window.zig +++ b/src/nav/window.zig @@ -7,16 +7,19 @@ const parser = @import("../netsurf.zig"); pub const Window = struct { pub const mem_guarantied = true; - document: *parser.Document, + document: *parser.Document = undefined, target: []const u8, - pub fn create(doc: *parser.Document, target: ?[]const u8) Window { + pub fn create(target: ?[]const u8) Window { return Window{ - .document = doc, .target = target orelse "", }; } + pub fn replaceDocument(self: *Window, doc: *parser.Document) void { + self.document = doc; + } + pub fn get_window(self: *Window) *Window { return self; }