Lightpanda: the headless browser designed for AI and automation
cdp: report the node subtype only for nodes, error for DOMException
Jay Hemnanifd54505dd391
diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zigindex e5165a9fb..fd959d13e 100644--- a/src/browser/js/Env.zig+++ b/src/browser/js/Env.zig@@ -349,7 +349,7 @@ fn _createContext(self: *Env, global: anytype, params: ContextParams) !*Context.value = @ptrCast(global.window),.prototype_chain = (&Window.JsApi.Meta.prototype_chain).ptr,.prototype_len = @intCast(Window.JsApi.Meta.prototype_chain.len),- .subtype = .node,+ .subtype = null,} else switch (global._type) {.dedicated => |scope| .{.value = @ptrCast(scope),diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zigindex 70f510b32..c0b62934c 100644--- a/src/browser/js/Local.zig+++ b/src/browser/js/Local.zig@@ -310,7 +310,7 @@ pub fn mapZigInstanceToJs(self: *const Local, js_obj_handle: ?*const v8.Object,.value = resolved.ptr,.prototype_chain = resolved.prototype_chain.ptr,.prototype_len = @intCast(resolved.prototype_chain.len),- .subtype = if (@hasDecl(JsApi.Meta, "subtype")) JsApi.Meta.subype else .node,+ .subtype = inspectorSubtype(resolved.prototype_chain),};v8.v8__Object__SetAlignedPointerInInternalField(js_obj.handle, 0, tao);@@ -1655,3 +1655,15 @@ pub const Scope = struct {return self.local.toLocal(global);}};++// The subtype the inspector reports for one of our objects, as Chrome does:+// "node" for Nodes, "error" for DOMExceptions, none for everything else.+fn inspectorSubtype(prototype_chain: []const TaggedOpaque.PrototypeChainEntry) ?bridge.SubType {+ const node_index = bridge.JsApiLookup.getId(@import("../webapi/Node.zig").JsApi);+ const dom_exception_index = bridge.JsApiLookup.getId(@import("../webapi/DOMException.zig").JsApi);+ for (prototype_chain) |proto| {+ if (proto.index == node_index) return .node;+ if (proto.index == dom_exception_index) return .@"error";+ }+ return null;+}diff --git a/src/browser/js/js.zig b/src/browser/js/js.zigindex 69ee8ba4b..d5a52778c 100644--- a/src/browser/js/js.zig+++ b/src/browser/js/js.zig@@ -487,7 +487,8 @@ pub export fn v8_inspector__Client__IMPL__valueSubtype(// Same as valueSubType above, but for the optional description field.// From what I can tell, some drivers _need_ the description field to be// present, even if it's empty. So if we have a subType for the value, we'll-// put an empty description.+// put an empty description. DOMExceptions get "name: message", which drivers+// use as the error message.pub export fn v8_inspector__Client__IMPL__descriptionForValueSubtype(_: *v8.InspectorClientImpl,v8_context: *const v8.Context,@@ -498,7 +499,20 @@ pub export fn v8_inspector__Client__IMPL__descriptionForValueSubtype(// We _must_ include a non-null description in order for the subtype value// to be included. Besides that, I don't know if the value has any meaningconst external_entry = Inspector.getTaggedOpaque(c_value) orelse return null;- return if (external_entry.subtype == null) null else "";+ const subtype = external_entry.subtype orelse return null;+ if (subtype == .@"error") {+ const DOMException = @import("../webapi/DOMException.zig");+ if (external_entry.as(DOMException)) |ex| {+ // copied by the caller before we're called again+ const S = struct {+ threadlocal var buf: [512]u8 = undefined;+ };+ const description = std.fmt.bufPrintSentinel(&S.buf, "{s}: {s}", .{ ex.getName(), ex.getMessage() }, 0) catch+ std.fmt.bufPrintSentinel(&S.buf, "{s}", .{ex.getName()}, 0) catch return "";+ return description.ptr;+ }+ }+ return "";}test "TaggedAnyOpaque" {diff --git a/src/browser/webapi/DOMException.zig b/src/browser/webapi/DOMException.zigindex 41b9c79cf..755825776 100644--- a/src/browser/webapi/DOMException.zig+++ b/src/browser/webapi/DOMException.zig@@ -115,7 +115,7 @@ pub fn getName(self: *const DOMException) []const u8 {};}-fn getMessage(self: *const DOMException) []const u8 {+pub fn getMessage(self: *const DOMException) []const u8 {if (self._custom_message) |msg| {return msg;}diff --git a/src/server/cdp/domains/runtime.zig b/src/server/cdp/domains/runtime.zigindex 23a43ed1e..2a3b44042 100644--- a/src/server/cdp/domains/runtime.zig+++ b/src/server/cdp/domains/runtime.zig@@ -376,3 +376,28 @@ test "cdp.runtime: console notifications run no page JS" {try ctx.processMessage(.{ .id = 62, .method = "Runtime.evaluate", .params = .{ .expression = "6 * 7" } });try ctx.expectSentResult(.{ .result = .{ .type = "number", .value = 42 } }, .{ .id = 62 });}++test "cdp.runtime: only nodes have the node subtype, DOMExceptions are errors" {+ var ctx = try testing.context();+ defer ctx.deinit();++ _ = try ctx.loadBrowserContext(.{ .id = "BID-SUBT", .url = "hi.html", .target_id = "FID-000000SUBT".* });+ try ctx.processMessage(.{ .id = 70, .method = "Runtime.enable" });++ try ctx.processMessage(.{ .id = 71, .method = "Runtime.evaluate", .params = .{ .expression = "document.body" } });+ try ctx.expectSentResult(.{ .result = .{ .type = "object", .subtype = "node", .className = "HTMLBodyElement" } }, .{ .id = 71 });++ try ctx.processMessage(.{ .id = 72, .method = "Runtime.evaluate", .params = .{ .expression = "new DOMException('custom', 'NotFoundError')" } });+ try ctx.expectSentResult(.{ .result = .{ .type = "object", .subtype = "error", .className = "DOMException", .description = "NotFoundError: custom" } }, .{ .id = 72 });++ try ctx.processMessage(.{ .id = 73, .method = "Runtime.evaluate", .params = .{ .expression = "location" } });+ try ctx.expectSentResult(.{ .result = .{ .type = "object", .className = "Location", .description = "Location" } }, .{ .id = 73 });+ try ctx.processMessage(.{ .id = 74, .method = "Runtime.evaluate", .params = .{ .expression = "window" } });+ try ctx.expectSentResult(.{ .result = .{ .type = "object", .className = "Window" } }, .{ .id = 74 });+ for (ctx.received.items) |msg| {+ const id = msg.object.get("id") orelse continue;+ if (id.integer != 73 and id.integer != 74) continue;+ const result = msg.object.get("result").?.object.get("result").?.object;+ try testing.expectEqual(null, result.get("subtype"));+ }+}