Lightpanda: the headless browser designed for AI and automation
Inspector.getRemoteObject now returns Optional
Karl Seguin4870ff571088
diff --git a/src/Notification.zig b/src/Notification.zigindex a32a0c7a7..1520efd53 100644--- a/src/Notification.zig+++ b/src/Notification.zig@@ -382,6 +382,8 @@ pub const ConsoleMessage = struct {},type: ConsoleMessageType,values: []js.Value,+ // The caller's: the context the message was logged in.+ local: *const js.Local,url: ?[]const u8 = null,line: ?u32 = null,columns: ?u32 = null,diff --git a/src/browser/js/Inspector.zig b/src/browser/js/Inspector.zigindex 210dd8478..0ec295a10 100644--- a/src/browser/js/Inspector.zig+++ b/src/browser/js/Inspector.zig@@ -279,7 +279,7 @@ pub const Session = struct {local: *const js.Local,group: []const u8,value: anytype,- ) !RemoteObject {+ ) !?RemoteObject {const js_val = try local.zigValueToJs(value, .{});// We do not want to expose this as a parameter for now@@ -300,7 +300,7 @@ pub const Session = struct {val: *const v8.Value,grpname: []const u8,generatepreview: bool,- ) !RemoteObject {+ ) ?RemoteObject {const remote_object = v8.v8_inspector__Session__wrapObject(self.handle,isolate,@@ -309,7 +309,7 @@ pub const Session = struct {grpname.ptr,grpname.len,generatepreview,- ) orelse return error.ContextNotFound; // not a context the inspector knows+ ) orelse return null; // not a context the inspector knowsreturn .{ .handle = remote_object };}diff --git a/src/browser/webapi/Console.zig b/src/browser/webapi/Console.zigindex 8420be810..2af45eb63 100644--- a/src/browser/webapi/Console.zig+++ b/src/browser/webapi/Console.zig@@ -34,11 +34,13 @@ pub const init: Console = .{};fn dispatchConsoleMessage(values: []js.Value, console_type: Notification.ConsoleMessageType, exec: *js.Execution) void {const notification = exec.session.notification;const ts = lp.datetime.timestamp(.boot);+ const local = exec.js.local.?;notification.dispatch(.console_message, &.{.source = .javascript,.type = console_type,.values = values,+ .local = local,.timestamp = ts,});@@ -46,6 +48,7 @@ fn dispatchConsoleMessage(values: []js.Value, console_type: Notification.Console.source = .javascript,.type = console_type,.values = values,+ .local = local,.timestamp = ts,});}diff --git a/src/server/cdp/domains/dom.zig b/src/server/cdp/domains/dom.zigindex 8f83e821c..a61cbdddc 100644--- a/src/server/cdp/domains/dom.zig+++ b/src/server/cdp/domains/dom.zig@@ -374,7 +374,7 @@ fn resolveNode(cmd: *CDP.Command) !void {&ls.local,params.objectGroup orelse "",node.dom,- );+ ) orelse return error.ContextNotFound;defer remote_object.deinit();const arena = cmd.arena;diff --git a/src/server/cdp/domains/runtime.zig b/src/server/cdp/domains/runtime.zigindex ece1f8b5e..ca7dc4841 100644--- a/src/server/cdp/domains/runtime.zig+++ b/src/server/cdp/domains/runtime.zig@@ -138,15 +138,11 @@ pub fn consoleMessage(arena: Allocator, bc: *CDP.BrowserContext, event: *const Nframe.js.localScope(&ls);defer ls.deinit();- const context_id = bc.inspector().getContextId(&ls.local);+ const local = inspectedContext(bc, event.local, &ls.local);var args: std.ArrayList(RemoteObject) = .empty;for (event.values) |value| {- const remote_object = try inspector_session.getRemoteObject(- &ls.local,- "",- value,- );+ const remote_object = try inspector_session.getRemoteObject(local, "", value) orelse return error.ContextNotFound;defer remote_object.deinit();const arg = try args.addOne(arena);@@ -163,7 +159,7 @@ pub fn consoleMessage(arena: Allocator, bc: *CDP.BrowserContext, event: *const Nreturn bc.cdp.sendEvent("Runtime.consoleAPICalled", ConsoleMessage{.type = @tagName(event.type),.timestamp = event.timestamp,- .executionContextId = context_id,+ .executionContextId = bc.inspector().getContextId(local),.args = args.items,}, .{ .session_id = session_id });}@@ -178,6 +174,13 @@ const ExceptionDetails = struct {executionContextId: i32,};+// The context `local` belongs to (an iframe's, an isolated world's), or the+// page's when the inspector doesn't know it (a worker's): an unregistered+// context has id 0.+fn inspectedContext(bc: *CDP.BrowserContext, local: *const js.Local, page: *const js.Local) *const js.Local {+ return if (bc.inspector().getContextId(local) == 0) page else local;+}+pub fn exceptionThrown(arena: Allocator, bc: *CDP.BrowserContext, event: *const Notification.ExceptionThrown) !void {// Like consoleAPICalled: the primary session's inspector mints the handle.const session_id = bc.session_id orelse return;@@ -188,14 +191,9 @@ pub fn exceptionThrown(arena: Allocator, bc: *CDP.BrowserContext, event: *constframe.js.localScope(&ls);defer ls.deinit();- // The context the exception was thrown in (an iframe's, an isolated- // world's), or the page's when the inspector doesn't know that one.const value = event.value;- var local = value.local;- const remote_object = inspector_session.getRemoteObject(local, "", value) catch blk: {- local = &ls.local;- break :blk try inspector_session.getRemoteObject(local, "", value);- };+ const local = inspectedContext(bc, value.local, &ls.local);+ const remote_object = try inspector_session.getRemoteObject(local, "", value) orelse return error.ContextNotFound;defer remote_object.deinit();var exception = RemoteObject{@@ -527,6 +525,36 @@ test "cdp.runtime: an exception carries the id of the context it was thrown in"}, .{});}+test "cdp.runtime: consoleAPICalled carries the id of the context it was logged in" {+ testing.silenceLog(&.{.js});++ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .id = "BID-CON2", .url = "cdp/input_iframe.html", .target_id = "FID-0000000CO2".* });+ try ctx.processMessage(.{ .id = 64, .method = "Runtime.enable" });++ const frame = bc.mainFrame() orelse unreachable;+ const child = frame.child_frames.items[0];++ var page_ls: js.Local.Scope = undefined;+ frame.js.localScope(&page_ls);+ defer page_ls.deinit();+ const page_context_id = bc.inspector().getContextId(&page_ls.local);+ _ = try page_ls.local.exec("console.log('page')", null);+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .executionContextId = page_context_id }, .{});++ var child_ls: js.Local.Scope = undefined;+ child.js.localScope(&child_ls);+ defer child_ls.deinit();+ const child_context_id = bc.inspector().getContextId(&child_ls.local);+ try testing.expect(child_context_id != page_context_id);+ // no arguments: the context comes from the caller, not the values+ _ = try child_ls.local.exec("console.log('child'); console.log()", null);+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .executionContextId = child_context_id, .args = &.{.{ .value = "child" }} }, .{});+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .executionContextId = child_context_id, .args = &.{} }, .{});+}+test "cdp.runtime: consoleAPICalled type matches the console method" {testing.silenceLog(&.{.js});