describeNode

This commit is contained in:
sjorsdonkers
2025-04-18 11:53:53 +02:00
parent f3d8ec040c
commit 2ac63b6985
3 changed files with 71 additions and 4 deletions

View File

@@ -30,6 +30,7 @@ pub fn processMessage(cmd: anytype) !void {
getSearchResults, getSearchResults,
discardSearchResults, discardSearchResults,
resolveNode, resolveNode,
describeNode,
}, cmd.input.action) orelse return error.UnknownMethod; }, cmd.input.action) orelse return error.UnknownMethod;
switch (action) { switch (action) {
@@ -39,6 +40,7 @@ pub fn processMessage(cmd: anytype) !void {
.getSearchResults => return getSearchResults(cmd), .getSearchResults => return getSearchResults(cmd),
.discardSearchResults => return discardSearchResults(cmd), .discardSearchResults => return discardSearchResults(cmd),
.resolveNode => return resolveNode(cmd), .resolveNode => return resolveNode(cmd),
.describeNode => return describeNode(cmd),
} }
} }
@@ -151,6 +153,37 @@ fn resolveNode(cmd: anytype) !void {
} }, .{}); } }, .{});
} }
fn describeNode(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
objectId: ?[]const u8 = null,
depth: u32 = 1,
pierce: bool = false,
})) orelse return error.InvalidParams;
if (params.backendNodeId != null or params.depth != 1 or params.pierce) {
return error.NotYetImplementedParams;
}
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
if (params.nodeId != null) {
const node = bc.node_registry.lookup_by_id.get(params.nodeId.?) orelse return error.NodeNotFound;
return cmd.sendResult(.{ .node = bc.nodeWriter(node, .{}) }, .{});
} else if (params.objectId != null) {
// Retrieve the object from which ever context it is in.
const js_value = try bc.session.inspector.getValueByObjectId(cmd.arena, bc.session.executor, params.objectId.?);
const entry = js_value.taggedAnyOpaque() orelse return error.ObjectIdIsNotANode;
const subtype = entry.subtype orelse return error.ObjectIdIsNotANode;
if (subtype != .node) return error.ObjectIdIsNotANode;
const node = try bc.node_registry.register(@ptrCast(entry.ptr));
return cmd.sendResult(.{ .node = bc.nodeWriter(node, .{}) }, .{});
}
return error.MissingParams;
}
const testing = @import("../testing.zig"); const testing = @import("../testing.zig");
test "cdp.dom: getSearchResults unknown search id" { test "cdp.dom: getSearchResults unknown search id" {

View File

@@ -55,13 +55,13 @@ const Browser = struct {
if (self.session != null) { if (self.session != null) {
return error.MockBrowserSessionAlreadyExists; return error.MockBrowserSessionAlreadyExists;
} }
const arena = self.arena.allocator(); const arena = self.arena.allocator();
const executor = arena.create(Executor) catch unreachable;
self.session = try arena.create(Session); self.session = try arena.create(Session);
self.session.?.* = .{ self.session.?.* = .{
.page = null, .page = null,
.arena = arena, .arena = arena,
.executor = .{}, .executor = executor,
.inspector = .{}, .inspector = .{},
}; };
return self.session.?; return self.session.?;
@@ -78,7 +78,7 @@ const Browser = struct {
const Session = struct { const Session = struct {
page: ?Page = null, page: ?Page = null,
arena: Allocator, arena: Allocator,
executor: Executor, executor: *Executor,
inspector: Inspector, inspector: Inspector,
pub fn currentPage(self: *Session) ?*Page { pub fn currentPage(self: *Session) ?*Page {
@@ -112,7 +112,7 @@ const Executor = struct {};
const Inspector = struct { const Inspector = struct {
pub fn getRemoteObject( pub fn getRemoteObject(
self: *const Inspector, self: *const Inspector,
executor: Executor, executor: *Executor,
group: []const u8, group: []const u8,
value: anytype, value: anytype,
) !RemoteObject { ) !RemoteObject {
@@ -122,6 +122,27 @@ const Inspector = struct {
_ = value; _ = value;
return RemoteObject{}; return RemoteObject{};
} }
pub fn getValueByObjectId(self: Inspector, alloc: std.mem.Allocator, executor: *const Executor, object_id: []const u8) !Value {
_ = self;
_ = alloc;
_ = executor;
_ = object_id;
return .{};
}
};
const Value = struct {
pub fn taggedAnyOpaque(self: Value) ?*TaggedAnyOpaque {
_ = self;
return null;
}
};
const TaggedAnyOpaque = struct {
ptr: *anyopaque,
subtype: ?SubType = .node,
};
const SubType = enum {
node,
}; };
const RemoteObject = struct { const RemoteObject = struct {

View File

@@ -1369,6 +1369,15 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
generate_preview, generate_preview,
); );
} }
// Gets a value by object ID regardless of which context it is in.
// unwrapping the object also tells us the context, for now we assume it is always the default one.
// The executor argument is likely to change to somthing to allow us to find the right Executer with the given context
pub fn getValueByObjectId(self: Inspector, allocator: std.mem.Allocator, executor: *const Executor, object_id: []const u8) !Value {
const unwrapped = try self.session.unwrapObject(allocator, object_id);
// std.debug.assert(executor.context.handle == unwrapped.context.handle);
return .{ .value = unwrapped.value, .executor = executor }; // The values context and groupId are not used here
}
}; };
pub const RemoteObject = v8.RemoteObject; pub const RemoteObject = v8.RemoteObject;
@@ -1382,6 +1391,10 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
const executor = self.executor; const executor = self.executor;
return valueToString(allocator, self.value, executor.isolate, executor.context); return valueToString(allocator, self.value, executor.isolate, executor.context);
} }
pub fn taggedAnyOpaque(self: Value) ?*TaggedAnyOpaque {
return getTaggedAnyOpaque(self.value);
}
}; };
// Reverses the mapZigInstanceToJs, making sure that our TaggedAnyOpaque // Reverses the mapZigInstanceToJs, making sure that our TaggedAnyOpaque