Adapt to js_exec changes in zig-js-runtime

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-07-08 17:42:03 +02:00
committed by Pierre Tachoire
parent f8395fec5c
commit b8ec53f708
7 changed files with 74 additions and 83 deletions

View File

@@ -198,21 +198,21 @@ pub const Page = struct {
} }
pub fn wait(self: *Page) !void { pub fn wait(self: *Page) !void {
const alloc = self.arena.allocator();
var res = try self.session.env.waitTryCatch(alloc);
defer res.deinit(alloc);
if (res.success) { // try catch
log.debug("wait: {s}", .{res.result}); var try_catch: jsruntime.TryCatch = undefined;
} else { try_catch.init(self.session.env);
if (builtin.mode == .Debug and res.stack != null) { defer try_catch.deinit();
log.info("wait: {s}", .{res.stack.?});
} else { self.session.env.wait() catch {
log.info("wait: {s}", .{res.result}); const alloc = self.arena.allocator();
if (try try_catch.err(alloc, self.session.env)) |msg| {
defer alloc.free(msg);
std.log.info("wait error: {s}", .{msg});
return;
} }
} };
log.debug("wait: OK", .{});
return;
} }
// spec reference: https://html.spec.whatwg.org/#document-lifecycle // spec reference: https://html.spec.whatwg.org/#document-lifecycle
@@ -322,7 +322,7 @@ pub const Page = struct {
// start JS env // start JS env
// TODO load the js env concurrently with the HTML parsing. // TODO load the js env concurrently with the HTML parsing.
log.debug("start js env", .{}); log.debug("start js env", .{});
try self.session.env.start(alloc); try self.session.env.start();
// replace the user context document with the new one. // replace the user context document with the new one.
try self.session.env.setUserContext(.{ try self.session.env.setUserContext(.{
@@ -473,22 +473,26 @@ pub const Page = struct {
return; return;
} }
var try_catch: jsruntime.TryCatch = undefined;
try_catch.init(self.session.env);
defer try_catch.deinit();
const opt_text = try parser.nodeTextContent(parser.elementToNode(e)); const opt_text = try parser.nodeTextContent(parser.elementToNode(e));
if (opt_text) |text| { if (opt_text) |text| {
// TODO handle charset attribute // TODO handle charset attribute
var res = try self.session.env.execTryCatch(alloc, text, ""); const res = self.session.env.exec(text, "") catch {
defer res.deinit(alloc); if (try try_catch.err(alloc, self.session.env)) |msg| {
defer alloc.free(msg);
if (res.success) { log.info("eval inline {s}: {s}", .{ text, msg });
log.debug("eval inline: {s}", .{res.result});
} else {
if (builtin.mode == .Debug and res.stack != null) {
log.info("eval inline: {s}", .{res.stack.?});
} else {
log.info("eval inline: {s}", .{res.result});
} }
} return;
};
if (builtin.mode == .Debug) {
const msg = try res.toString(alloc, self.session.env);
defer alloc.free(msg);
log.debug("eval inline {s}", .{msg});
}
return; return;
} }
@@ -530,18 +534,22 @@ pub const Page = struct {
// check no body // check no body
if (body.len == 0) return FetchError.NoBody; if (body.len == 0) return FetchError.NoBody;
var res = try self.session.env.execTryCatch(alloc, body, src); var try_catch: jsruntime.TryCatch = undefined;
defer res.deinit(alloc); try_catch.init(self.session.env);
defer try_catch.deinit();
if (res.success) { const res = self.session.env.exec(body, src) catch {
log.debug("eval remote {s}: {s}", .{ src, res.result }); if (try try_catch.err(alloc, self.session.env)) |msg| {
} else { defer alloc.free(msg);
if (builtin.mode == .Debug and res.stack != null) { log.info("eval remote {s}: {s}", .{ src, msg });
log.info("eval remote {s}: {s}", .{ src, res.stack.? });
} else {
log.info("eval remote {s}: {s}", .{ src, res.result });
} }
return FetchError.JsErr; return FetchError.JsErr;
};
if (builtin.mode == .Debug) {
const msg = try res.toString(alloc, self.session.env);
defer alloc.free(msg);
log.debug("eval remote {s}: {s}", .{ src, msg });
} }
} }

View File

@@ -37,7 +37,7 @@ fn execJS(
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
) anyerror!void { ) anyerror!void {
// start JS env // start JS env
try js_env.start(alloc); try js_env.start();
defer js_env.stop(); defer js_env.stop();
// alias global as self and window // alias global as self and window
@@ -45,6 +45,11 @@ fn execJS(
window.replaceDocument(doc); window.replaceDocument(doc);
try js_env.bindGlobal(window); try js_env.bindGlobal(window);
// try catch
var try_catch: jsruntime.TryCatch = undefined;
try_catch.init(js_env.*);
defer try_catch.deinit();
while (true) { while (true) {
// read cmd // read cmd
@@ -57,11 +62,12 @@ fn execJS(
break; break;
} }
const res = try js_env.execTryCatch(alloc, cmd, "cdp"); const res = try js_env.exec(cmd, "cdp");
if (res.success) { const res_str = try res.toString(alloc, js_env.*);
std.debug.print("-> {s}\n", .{res.result}); defer alloc.free(res_str);
} std.debug.print("-> {s}\n", .{res_str});
_ = try conn.stream.write(res.result);
_ = try conn.stream.write(res_str);
} }
} }

View File

@@ -38,7 +38,7 @@ fn execJS(
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
) anyerror!void { ) anyerror!void {
// start JS env // start JS env
try js_env.start(alloc); try js_env.start();
defer js_env.stop(); defer js_env.stop();
var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop }; var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop };

View File

@@ -141,7 +141,7 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(alloc); var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit(); defer arena.deinit();
const res = wpt.run(&arena, wpt_dir, tc, &loader) catch |err| { const result = wpt.run(&arena, wpt_dir, tc, &loader) catch |err| {
const suite = try Suite.init(alloc, tc, false, @errorName(err), null); const suite = try Suite.init(alloc, tc, false, @errorName(err), null);
try results.append(suite); try results.append(suite);
@@ -151,9 +151,8 @@ pub fn main() !void {
failures += 1; failures += 1;
continue; continue;
}; };
// no need to call res.deinit() thanks to the arena allocator.
const suite = try Suite.init(alloc, tc, res.success, res.result, res.stack); const suite = try Suite.init(alloc, tc, true, result, null);
try results.append(suite); try results.append(suite);
if (out == .json) { if (out == .json) {

View File

@@ -71,7 +71,7 @@ fn testExecFn(
defer parser.deinit(); defer parser.deinit();
// start JS env // start JS env
try js_env.start(alloc); try js_env.start();
defer js_env.stop(); defer js_env.stop();
var storageShelf = storage.Shelf.init(alloc); var storageShelf = storage.Shelf.init(alloc);

View File

@@ -36,7 +36,7 @@ const Client = @import("../async/Client.zig");
// runWPT parses the given HTML file, starts a js env and run the first script // runWPT parses the given HTML file, starts a js env and run the first script
// tags containing javascript sources. // tags containing javascript sources.
// It loads first the js libs files. // It loads first the js libs files.
pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader) !jsruntime.JSResult { pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader) ![]const u8 {
const alloc = arena.allocator(); const alloc = arena.allocator();
try parser.init(); try parser.init();
defer parser.deinit(); defer parser.deinit();
@@ -70,15 +70,15 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
try js_env.load(&js_types); try js_env.load(&js_types);
// start JS env // start JS env
try js_env.start(alloc); try js_env.start();
defer js_env.stop(); defer js_env.stop();
// display console logs // display console logs
defer { defer {
var res = evalJS(js_env, alloc, "console.join('\\n');", "console") catch unreachable; const res = evalJS(js_env, "console.join('\\n');", "console") catch unreachable;
defer res.deinit(alloc); const res_str = res.toString(alloc, js_env) catch unreachable;
if (res.result.len > 0) { if (res_str.len > 0) {
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res.result}); std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res_str});
} }
} }
@@ -89,7 +89,6 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
try js_env.bindGlobal(&window); try js_env.bindGlobal(&window);
// thanks to the arena, we don't need to deinit res. // thanks to the arena, we don't need to deinit res.
var res: jsruntime.JSResult = undefined;
const init = const init =
\\console = []; \\console = [];
@@ -100,11 +99,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
\\ console.push("debug", ...arguments); \\ console.push("debug", ...arguments);
\\}; \\};
; ;
res = try evalJS(js_env, alloc, init, "init"); _ = try evalJS(js_env, init, "init");
if (!res.success) {
return res;
}
res.deinit(alloc);
// loop hover the scripts. // loop hover the scripts.
const doc = parser.documentHTMLToDocument(html_doc); const doc = parser.documentHTMLToDocument(html_doc);
@@ -121,22 +116,12 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
path = try fspath.join(alloc, &.{ "/", dirname, path }); path = try fspath.join(alloc, &.{ "/", dirname, path });
} }
res = try evalJS(js_env, alloc, try loader.get(path), src); _ = try evalJS(js_env, try loader.get(path), src);
if (!res.success) {
return res;
}
res.deinit(alloc);
} }
// If the script as a source text, execute it. // If the script as a source text, execute it.
const src = try parser.nodeTextContent(s) orelse continue; const src = try parser.nodeTextContent(s) orelse continue;
res = try evalJS(js_env, alloc, src, ""); _ = try evalJS(js_env, src, "");
// return the first failure.
if (!res.success) {
return res;
}
res.deinit(alloc);
} }
// Mark tests as ready to run. // Mark tests as ready to run.
@@ -150,25 +135,18 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
); );
// wait for all async executions // wait for all async executions
res = try js_env.waitTryCatch(alloc); _ = try js_env.wait();
if (!res.success) {
return res;
}
res.deinit(alloc);
// Check the final test status. // Check the final test status.
res = try evalJS(js_env, alloc, "report.status;", "teststatus"); _ = try evalJS(js_env, "report.status;", "teststatus");
if (!res.success) {
return res;
}
res.deinit(alloc);
// return the detailed result. // return the detailed result.
return try evalJS(js_env, alloc, "report.log", "teststatus"); const res = try evalJS(js_env, "report.log", "teststatus");
return try res.toString(alloc, js_env);
} }
fn evalJS(env: jsruntime.Env, alloc: std.mem.Allocator, script: []const u8, name: ?[]const u8) !jsruntime.JSResult { fn evalJS(env: jsruntime.Env, script: []const u8, name: ?[]const u8) !jsruntime.JSValue {
return try env.execTryCatch(alloc, script, name); return try env.exec(script, name);
} }
// browse the path to find the tests list. // browse the path to find the tests list.