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 {
const alloc = self.arena.allocator();
var res = try self.session.env.waitTryCatch(alloc);
defer res.deinit(alloc);
if (res.success) {
log.debug("wait: {s}", .{res.result});
} else {
if (builtin.mode == .Debug and res.stack != null) {
log.info("wait: {s}", .{res.stack.?});
} else {
log.info("wait: {s}", .{res.result});
// try catch
var try_catch: jsruntime.TryCatch = undefined;
try_catch.init(self.session.env);
defer try_catch.deinit();
self.session.env.wait() catch {
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;
}
}
return;
};
log.debug("wait: OK", .{});
}
// spec reference: https://html.spec.whatwg.org/#document-lifecycle
@@ -322,7 +322,7 @@ pub const Page = struct {
// start JS env
// TODO load the js env concurrently with the HTML parsing.
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.
try self.session.env.setUserContext(.{
@@ -473,22 +473,26 @@ pub const Page = struct {
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));
if (opt_text) |text| {
// TODO handle charset attribute
var res = try self.session.env.execTryCatch(alloc, text, "");
defer res.deinit(alloc);
if (res.success) {
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});
const res = self.session.env.exec(text, "") catch {
if (try try_catch.err(alloc, self.session.env)) |msg| {
defer alloc.free(msg);
log.info("eval inline {s}: {s}", .{ text, msg });
}
}
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;
}
@@ -530,18 +534,22 @@ pub const Page = struct {
// check no body
if (body.len == 0) return FetchError.NoBody;
var res = try self.session.env.execTryCatch(alloc, body, src);
defer res.deinit(alloc);
var try_catch: jsruntime.TryCatch = undefined;
try_catch.init(self.session.env);
defer try_catch.deinit();
if (res.success) {
log.debug("eval remote {s}: {s}", .{ src, res.result });
} else {
if (builtin.mode == .Debug and res.stack != null) {
log.info("eval remote {s}: {s}", .{ src, res.stack.? });
} else {
log.info("eval remote {s}: {s}", .{ src, res.result });
const res = self.session.env.exec(body, src) catch {
if (try try_catch.err(alloc, self.session.env)) |msg| {
defer alloc.free(msg);
log.info("eval remote {s}: {s}", .{ src, msg });
}
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,
) anyerror!void {
// start JS env
try js_env.start(alloc);
try js_env.start();
defer js_env.stop();
// alias global as self and window
@@ -45,6 +45,11 @@ fn execJS(
window.replaceDocument(doc);
try js_env.bindGlobal(window);
// try catch
var try_catch: jsruntime.TryCatch = undefined;
try_catch.init(js_env.*);
defer try_catch.deinit();
while (true) {
// read cmd
@@ -57,11 +62,12 @@ fn execJS(
break;
}
const res = try js_env.execTryCatch(alloc, cmd, "cdp");
if (res.success) {
std.debug.print("-> {s}\n", .{res.result});
}
_ = try conn.stream.write(res.result);
const res = try js_env.exec(cmd, "cdp");
const res_str = try res.toString(alloc, js_env.*);
defer alloc.free(res_str);
std.debug.print("-> {s}\n", .{res_str});
_ = try conn.stream.write(res_str);
}
}

View File

@@ -38,7 +38,7 @@ fn execJS(
js_env: *jsruntime.Env,
) anyerror!void {
// start JS env
try js_env.start(alloc);
try js_env.start();
defer js_env.stop();
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);
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);
try results.append(suite);
@@ -151,9 +151,8 @@ pub fn main() !void {
failures += 1;
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);
if (out == .json) {

View File

@@ -71,7 +71,7 @@ fn testExecFn(
defer parser.deinit();
// start JS env
try js_env.start(alloc);
try js_env.start();
defer js_env.stop();
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
// tags containing javascript sources.
// 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();
try parser.init();
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);
// start JS env
try js_env.start(alloc);
try js_env.start();
defer js_env.stop();
// display console logs
defer {
var res = evalJS(js_env, alloc, "console.join('\\n');", "console") catch unreachable;
defer res.deinit(alloc);
if (res.result.len > 0) {
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res.result});
const res = evalJS(js_env, "console.join('\\n');", "console") catch unreachable;
const res_str = res.toString(alloc, js_env) catch unreachable;
if (res_str.len > 0) {
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);
// thanks to the arena, we don't need to deinit res.
var res: jsruntime.JSResult = undefined;
const init =
\\console = [];
@@ -100,11 +99,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
\\ console.push("debug", ...arguments);
\\};
;
res = try evalJS(js_env, alloc, init, "init");
if (!res.success) {
return res;
}
res.deinit(alloc);
_ = try evalJS(js_env, init, "init");
// loop hover the scripts.
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 });
}
res = try evalJS(js_env, alloc, try loader.get(path), src);
if (!res.success) {
return res;
}
res.deinit(alloc);
_ = try evalJS(js_env, try loader.get(path), src);
}
// If the script as a source text, execute it.
const src = try parser.nodeTextContent(s) orelse continue;
res = try evalJS(js_env, alloc, src, "");
// return the first failure.
if (!res.success) {
return res;
}
res.deinit(alloc);
_ = try evalJS(js_env, src, "");
}
// 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
res = try js_env.waitTryCatch(alloc);
if (!res.success) {
return res;
}
res.deinit(alloc);
_ = try js_env.wait();
// Check the final test status.
res = try evalJS(js_env, alloc, "report.status;", "teststatus");
if (!res.success) {
return res;
}
res.deinit(alloc);
_ = try evalJS(js_env, "report.status;", "teststatus");
// 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 {
return try env.execTryCatch(alloc, script, name);
fn evalJS(env: jsruntime.Env, script: []const u8, name: ?[]const u8) !jsruntime.JSValue {
return try env.exec(script, name);
}
// browse the path to find the tests list.