diff --git a/src/engines/v8/v8.zig b/src/engines/v8/v8.zig index 68a887e..2bbf4da 100644 --- a/src/engines/v8/v8.zig +++ b/src/engines/v8/v8.zig @@ -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; @@ -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 + 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( @@ -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