Skip to content

Commit 9b87782

Browse files
Merge pull request #301 from lightpanda-io/wrapObject
Inspector session wrapObject wrapper
2 parents 40c0c7d + a490018 commit 9b87782

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/engines/v8/v8.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub const ModuleLoadFn = *const fn (ctx: *anyopaque, referrer: ?Module, specifie
3636
pub const LoadFnType = @import("generate.zig").LoadFnType;
3737
pub const loadFn = @import("generate.zig").loadFn;
3838
const setNativeObject = @import("generate.zig").setNativeObject;
39+
const setNativeType = @import("generate.zig").setNativeType;
3940
const loadFunctionTemplate = @import("generate.zig").loadFunctionTemplate;
4041
const bindObjectNativeAndJS = @import("generate.zig").bindObjectNativeAndJS;
4142
const getTpl = @import("generate.zig").getTpl;
@@ -351,6 +352,29 @@ pub const Env = struct {
351352
}
352353
}
353354

355+
// Currently used for DOM nodes
356+
// - value Note: *parser.Node should be converted to dom/node.zig.Union to get the most precise type
357+
pub fn findOrAddValue(env: *Env, value: anytype) !v8.Value {
358+
if (builtin.is_test) {
359+
// std.testing.refAllDecls(@import("server.zig")); Causes `try ret.lookup(gen.Types);` to throw an error
360+
return error.TestingNotSupported;
361+
}
362+
comptime var ret: refl.Type = undefined;
363+
comptime {
364+
@setEvalBranchQuota(150_000); // Needed when this is called with a dom/node.zig.Union
365+
ret = try refl.Type.reflect(@TypeOf(value), null);
366+
try ret.lookup(gen.Types);
367+
}
368+
return try setNativeType(
369+
env.nat_ctx.alloc,
370+
&env.nat_ctx,
371+
ret,
372+
value,
373+
env.js_ctx.?,
374+
env.isolate,
375+
);
376+
}
377+
354378
// compile and run a JS script
355379
// It doesn't wait for callbacks execution
356380
pub fn exec(
@@ -719,6 +743,13 @@ pub const Inspector = struct {
719743
pub fn send(self: Inspector, env: Env, msg: []const u8) void {
720744
return self.session.dispatchProtocolMessage(env.isolate, msg);
721745
}
746+
747+
// Retrieves the RemoteObject for a given JsValue. We may extend the interface here to include:
748+
// backendNodeId, objectGroup, executionContextId. For a complete resolveNode implementation at this level.
749+
pub fn getRemoteObject(self: Inspector, env: *Env, jsValue: v8.Value, groupName: []const u8) !v8.RemoteObject {
750+
const generatePreview = false; // We do not want to expose this as a parameter for now
751+
return self.session.wrapObject(env.isolate, env.js_ctx.?, jsValue, groupName, generatePreview);
752+
}
722753
};
723754

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

0 commit comments

Comments
 (0)