Skip to content

Inspector session wrapObject wrapper #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub const ModuleLoadFn = *const fn (ctx: *anyopaque, referrer: ?Module, specifie
pub const LoadFnType = @import("generate.zig").LoadFnType;
pub const loadFn = @import("generate.zig").loadFn;
const setNativeObject = @import("generate.zig").setNativeObject;
const setNativeType = @import("generate.zig").setNativeType;
const loadFunctionTemplate = @import("generate.zig").loadFunctionTemplate;
const bindObjectNativeAndJS = @import("generate.zig").bindObjectNativeAndJS;
const getTpl = @import("generate.zig").getTpl;
Expand Down Expand Up @@ -351,6 +352,29 @@ pub const Env = struct {
}
}

// Currently used for DOM nodes
// - value Note: *parser.Node should be converted to dom/node.zig.Union to get the most precise type
pub fn findOrAddValue(env: *Env, value: anytype) !v8.Value {
if (builtin.is_test) {
// std.testing.refAllDecls(@import("server.zig")); Causes `try ret.lookup(gen.Types);` to throw an error
return error.TestingNotSupported;
}
comptime var ret: refl.Type = undefined;
comptime {
@setEvalBranchQuota(150_000); // Needed when this is called with a dom/node.zig.Union
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks painful

ret = try refl.Type.reflect(@TypeOf(value), null);
try ret.lookup(gen.Types);
}
return try setNativeType(
env.nat_ctx.alloc,
&env.nat_ctx,
ret,
value,
env.js_ctx.?,
env.isolate,
);
}

// compile and run a JS script
// It doesn't wait for callbacks execution
pub fn exec(
Expand Down Expand Up @@ -719,6 +743,13 @@ pub const Inspector = struct {
pub fn send(self: Inspector, env: Env, msg: []const u8) void {
return self.session.dispatchProtocolMessage(env.isolate, msg);
}

// Retrieves the RemoteObject for a given JsValue. We may extend the interface here to include:
// backendNodeId, objectGroup, executionContextId. For a complete resolveNode implementation at this level.
pub fn getRemoteObject(self: Inspector, env: *Env, jsValue: v8.Value, groupName: []const u8) !v8.RemoteObject {
const generatePreview = false; // We do not want to expose this as a parameter for now
return self.session.wrapObject(env.isolate, env.js_ctx.?, jsValue, groupName, generatePreview);
}
};

// When we return a Zig instance to V8, we wrap it in a v8.Object. That wrapping
Expand Down