From a698ff8309429b2e9c480c150ea1441817fb4d5c Mon Sep 17 00:00:00 2001 From: sjorsdonkers <72333389+sjorsdonkers@users.noreply.github.com> Date: Tue, 22 Apr 2025 13:48:13 +0200 Subject: [PATCH] describeNode feedback --- src/cdp/domains/dom.zig | 12 ++++-------- src/cdp/testing.zig | 20 ++------------------ src/runtime/js.zig | 14 +++++--------- 3 files changed, 11 insertions(+), 35 deletions(-) diff --git a/src/cdp/domains/dom.zig b/src/cdp/domains/dom.zig index 396cf530..a51a4059 100644 --- a/src/cdp/domains/dom.zig +++ b/src/cdp/domains/dom.zig @@ -170,15 +170,11 @@ fn describeNode(cmd: anytype) !void { 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) { - + } + 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)); + const parser_node = try bc.session.inspector.getNodePtr(cmd.arena, params.objectId.?); + const node = try bc.node_registry.register(@ptrCast(parser_node)); return cmd.sendResult(.{ .node = bc.nodeWriter(node, .{}) }, .{}); } return error.MissingParams; diff --git a/src/cdp/testing.zig b/src/cdp/testing.zig index 22fc9e8e..b702b923 100644 --- a/src/cdp/testing.zig +++ b/src/cdp/testing.zig @@ -122,28 +122,12 @@ const Inspector = struct { _ = value; return RemoteObject{}; } - pub fn getValueByObjectId(self: Inspector, alloc: std.mem.Allocator, executor: *const Executor, object_id: []const u8) !Value { + pub fn getNodePtr(self: Inspector, alloc: std.mem.Allocator, object_id: []const u8) !?*anyopaque { _ = self; - _ = alloc; - _ = executor; _ = object_id; - return .{}; + return try alloc.create(i32); } }; -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 { pub fn deinit(self: RemoteObject) void { diff --git a/src/runtime/js.zig b/src/runtime/js.zig index a42df1c7..81639704 100644 --- a/src/runtime/js.zig +++ b/src/runtime/js.zig @@ -1371,12 +1371,12 @@ pub fn Env(comptime S: type, comptime types: anytype) type { } // 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 { + pub fn getNodePtr(self: *const Inspector, allocator: Allocator, object_id: []const u8) !?*anyopaque { 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 + // The values context and groupId are not used here + const toa = getTaggedAnyOpaque(unwrapped.value) orelse return null; + if (toa.subtype == null or toa.subtype != .node) return error.ObjectIdIsNotANode; + return toa.ptr; } }; @@ -1391,10 +1391,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type { const executor = self.executor; 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