Skip to content

Commit 0d6a38b

Browse files
Merge pull request #247 from lightpanda-io/inspector
Inspector support
2 parents 6287339 + 31568f9 commit 0d6a38b

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed

.github/actions/install/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ inputs:
1717
zig-v8:
1818
description: 'zig v8 version to install'
1919
required: false
20-
default: 'v0.1.6'
20+
default: 'v0.1.7'
2121
v8:
2222
description: 'v8 version to install'
2323
required: false

src/api.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ pub const TryCatch = Engine.TryCatch;
8383
pub const VM = Engine.VM;
8484
pub const Env = Engine.Env;
8585

86+
pub const Inspector = Engine.Inspector;
87+
pub const InspectorOnResponseFn = *const fn (ctx: *anyopaque, call_id: u32, msg: []const u8) void;
88+
pub const InspectorOnEventFn = *const fn (ctx: *anyopaque, msg: []const u8) void;
89+
8690
pub const engineType = enum {
8791
v8,
8892
};

src/engines/v8/v8.zig

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub const Env = struct {
8686
isolate_params: v8.CreateParams,
8787
hscope: v8.HandleScope,
8888
globals: v8.FunctionTemplate,
89+
inspector: ?Inspector = null,
8990

9091
js_ctx: ?v8.Context = null,
9192

@@ -152,6 +153,14 @@ pub const Env = struct {
152153
self.* = undefined;
153154
}
154155

156+
pub fn setInspector(self: *Env, inspector: Inspector) void {
157+
self.inspector = inspector;
158+
}
159+
160+
pub inline fn getInspector(self: Env) ?Inspector {
161+
return self.inspector;
162+
}
163+
155164
pub fn setUserContext(self: *Env, userctx: public.UserContext) anyerror!void {
156165
self.nat_ctx.userctx = userctx;
157166
}
@@ -220,6 +229,7 @@ pub const Env = struct {
220229
// Native context
221230
self.nat_ctx.stop();
222231
}
232+
223233
pub fn getGlobal(self: Env) anyerror!Object {
224234
if (self.js_ctx == null) {
225235
return error.EnvNotStarted;
@@ -504,3 +514,54 @@ pub fn jsExec(script: []const u8, name: ?[]const u8, isolate: v8.Isolate, js_ctx
504514
const value = scr.run(js_ctx) catch return error.JSExec;
505515
return .{ .value = value };
506516
}
517+
518+
// Inspector
519+
520+
pub const Inspector = struct {
521+
inner: *v8.Inspector,
522+
session: v8.InspectorSession,
523+
524+
pub fn init(
525+
alloc: std.mem.Allocator,
526+
env: Env,
527+
ctx: *anyopaque,
528+
onResp: public.InspectorOnResponseFn,
529+
onEvent: public.InspectorOnEventFn,
530+
) anyerror!Inspector {
531+
const inner = try alloc.create(v8.Inspector);
532+
const channel = v8.InspectorChannel.init(ctx, onResp, onEvent, env.isolate);
533+
const client = v8.InspectorClient.init();
534+
v8.Inspector.init(inner, client, channel, env.isolate);
535+
const session = inner.connect();
536+
return .{ .inner = inner, .session = session };
537+
}
538+
539+
pub fn deinit(self: Inspector, alloc: std.mem.Allocator) void {
540+
self.inner.deinit();
541+
alloc.destroy(self.inner);
542+
}
543+
544+
// From CDP docs
545+
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ExecutionContextDescription
546+
// ----
547+
// - name: Human readable name describing given context.
548+
// - origin: Execution context origin (ie. URL who initialised the request)
549+
// - auxData: Embedder-specific auxiliary data likely matching
550+
// {isDefault: boolean, type: 'default'|'isolated'|'worker', frameId: string}
551+
pub fn contextCreated(
552+
self: Inspector,
553+
env: Env,
554+
name: []const u8,
555+
origin: []const u8,
556+
auxData: ?[]const u8,
557+
) void {
558+
self.inner.contextCreated(env.js_ctx.?, name, origin, auxData);
559+
}
560+
561+
// msg should be formatted for the Inspector protocol
562+
// for v8 it's the CDP protocol https://chromedevtools.github.io/devtools-protocol/
563+
// with only some domains being relevant (mainly Runtime and Debugger)
564+
pub fn send(self: Inspector, env: Env, msg: []const u8) void {
565+
return self.session.dispatchProtocolMessage(env.isolate, msg);
566+
}
567+
};

src/interfaces.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub fn VM(comptime T: type) void {
4646

4747
pub fn Env(
4848
comptime T: type,
49+
comptime Inspector_T: type,
4950
comptime JSValue_T: type,
5051
comptime Object_T: type,
5152
) void {
@@ -64,6 +65,9 @@ pub fn Env(
6465

6566
assertDecl(T, "bindGlobal", fn (self: *T, ob: anytype) anyerror!void);
6667

68+
assertDecl(T, "setInspector", fn (self: *T, inspector: Inspector_T) void);
69+
assertDecl(T, "getInspector", fn (self: T) callconv(.Inline) ?Inspector_T);
70+
6771
assertDecl(T, "setUserContext", fn (
6872
self: *T,
6973
userctx: public.UserContext,
@@ -192,6 +196,33 @@ pub fn CallbackResult(comptime T: type) void {
192196
// TODO: how to get the result?
193197
}
194198

199+
pub fn Inspector(comptime T: type, comptime Env_T: type) void {
200+
201+
// init()
202+
assertDecl(T, "init", fn (
203+
alloc: std.mem.Allocator,
204+
env: Env_T,
205+
ctx: *anyopaque,
206+
onResp: public.InspectorOnResponseFn,
207+
onEvent: public.InspectorOnEventFn,
208+
) anyerror!T);
209+
210+
// deinit()
211+
assertDecl(T, "deinit", fn (self: T, alloc: std.mem.Allocator) void);
212+
213+
// contextCreated()
214+
assertDecl(T, "contextCreated", fn (
215+
self: T,
216+
env: Env_T,
217+
name: []const u8,
218+
origin: []const u8,
219+
auxData: ?[]const u8,
220+
) void);
221+
222+
// send()
223+
assertDecl(T, "send", fn (self: T, env: Env_T, msg: []const u8) void);
224+
}
225+
195226
// Utils
196227
// -----
197228

src/private_api.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ fn checkInterfaces(engine: anytype) void {
3434
interfaces.VM(engine.VM);
3535
interfaces.Env(
3636
engine.Env,
37+
engine.Inspector,
3738
engine.JSValue,
3839
engine.Object,
3940
);
4041

42+
interfaces.Inspector(engine.Inspector, engine.Env);
43+
4144
// private api
4245
}
4346

vendor/zig-v8

0 commit comments

Comments
 (0)