Skip to content

Commit bfb9db2

Browse files
Basic Runtime.evaluate run
Signed-off-by: Francis Bouvier <[email protected]>
1 parent c57e50c commit bfb9db2

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/cdp/runtime.zig

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const std = @import("std");
22

3+
const jsruntime = @import("jsruntime");
4+
35
const server = @import("../server.zig");
46
const Ctx = server.Cmd;
57
const cdp = @import("cdp.zig");
@@ -108,11 +110,14 @@ fn runIfWaitingForDebugger(
108110

109111
fn evaluate(
110112
alloc: std.mem.Allocator,
111-
_: u64,
113+
id: u64,
112114
scanner: *std.json.Scanner,
113-
_: *Ctx,
115+
ctx: *Ctx,
114116
) ![]const u8 {
115117

118+
// ensure a page has been previously created
119+
if (ctx.browser.currentSession().page == null) return error.CDPNoPage;
120+
116121
// input
117122
const Params = struct {
118123
expression: []const u8,
@@ -123,7 +128,45 @@ fn evaluate(
123128
const sessionID = input.sessionID;
124129
std.debug.assert(sessionID != null);
125130

126-
std.log.debug("expr: len {d}", .{input.params.expression.len});
127-
128-
return error.CDPNormal;
131+
// save script in file at debug mode
132+
std.log.debug("script {d} length: {d}", .{ id, input.params.expression.len });
133+
if (std.log.defaultLogEnabled(.debug)) {
134+
const name = try std.fmt.allocPrint(alloc, "id_{d}.js", .{id});
135+
defer alloc.free(name);
136+
const dir = try std.fs.cwd().makeOpenPath("zig-cache/tmp", .{});
137+
const f = try dir.createFile(name, .{});
138+
defer f.close();
139+
const nb = try f.write(input.params.expression);
140+
std.debug.assert(nb == input.params.expression.len);
141+
const p = try dir.realpathAlloc(alloc, name);
142+
defer alloc.free(p);
143+
std.log.debug("Script {d} saved at {s}", .{ id, p });
144+
}
145+
146+
// evaluate the script in the context of the current page
147+
// TODO: should we use instead the allocator of the page?
148+
// the following code does not work
149+
// const page_alloc = ctx.browser.currentSession().page.?.arena.allocator();
150+
const session_alloc = ctx.browser.currentSession().alloc;
151+
var res = jsruntime.JSResult{};
152+
try ctx.browser.currentSession().env.run(session_alloc, input.params.expression, "cdp", &res, null);
153+
defer res.deinit(session_alloc);
154+
155+
if (!res.success) {
156+
std.log.err("script {d} result: {s}", .{ id, res.result });
157+
if (res.stack) |stack| {
158+
std.log.err("script {d} stack: {s}", .{ id, stack });
159+
}
160+
return error.CDPRuntimeEvaluate;
161+
}
162+
std.log.debug("script {d} result: {s}", .{ id, res.result });
163+
164+
// TODO: Resp should depends on JS result returned by the JS engine
165+
const Resp = struct {
166+
type: []const u8 = "object",
167+
className: []const u8 = "UtilityScript",
168+
description: []const u8 = "UtilityScript",
169+
objectId: []const u8 = "7481631759780215274.3.2",
170+
};
171+
return result(alloc, id, Resp, Resp{}, sessionID);
129172
}

src/server.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Error = IOError || std.fmt.ParseIntError || cdp.Error || NoError;
1919
// --------
2020

2121
const BufReadSize = 1024; // 1KB
22+
const MaxStdOutSize = 512; // ensure debug msg are not too long
2223

2324
pub const Cmd = struct {
2425

@@ -51,7 +52,8 @@ pub const Cmd = struct {
5152
// input
5253
const input = self.buf[0..size];
5354
if (std.log.defaultLogEnabled(.debug)) {
54-
std.debug.print("\ninput size: {d}, content: {s}\n", .{ size, input });
55+
const content = input[0..@min(MaxStdOutSize, size)];
56+
std.debug.print("\ninput size: {d}, content: {s}\n", .{ size, content });
5557
}
5658

5759
// close on exit command
@@ -188,7 +190,7 @@ const MsgBuffer = struct {
188190
// handle several JSON msg in 1 read
189191
const is_combined = _input.len > msg_size;
190192
msg = _input[0..msg_size];
191-
std.log.debug("msg: {s}", .{msg[0..@min(BufReadSize, msg_size)]});
193+
std.log.debug("msg: {s}", .{msg[0..@min(MaxStdOutSize, msg_size)]});
192194
if (is_combined) {
193195
_input = _input[msg_size..];
194196
}

0 commit comments

Comments
 (0)